Unit Test With Jwebunit
Unit Test With Jwebunit
Test methodology:
JWebUnit to encode tests
Embedded HTTP server - using the Jetty server toolkit from
apache.org
Hello world!
<html>!
<head>!
<title>Hello world!</title>!
</head>!
<body>!
<h1>Hello world!</h1>!
</body>!
</html>
HelloWorld.html
@Before!
public final void setup() {!
setBaseUrl("http://127.0.0.1:" + SERVER_PORT); // Base URL!
setScriptingEnabled(true); // Enable Javascript!
}!
!
@Test!
public final void testHelloworldPage() {!
beginAt("/HelloWorld.html");!
assertTitleEquals("Hello world!");!
assertTextPresent("Hello world!");!
}
4
index.html
@Test !
public final void testLinksInStartPage() {!
beginAt("/index.html");!
assertLinkPresentWithText("Hello world!"); // by text!
assertLinkPresent("js-enabled"); // by id of <div> section!
assertLinkPresent("js-alert");
// by id of <div> section!
// ... etc ...!
}!
@Test!
public final void testClickToJavascriptEnabled() {!
beginAt("index.html");!
clickLinkWithExactText("Is Javascript enabled?");!
assertTitleEquals("Is Javascript enabled?");!
assertTextPresent("Javascript is enabled.");!
}
Tables
<html>!
<head> !
<title>HTML table</title>!
</head>!
<body>!
<h1>Sample HTML table</h1>!
<table id="sample_table" border="1">!
<tr>!
<td>1.1</td> <td>1.2</td> <td>1.3</td>!
</tr>!
<tr>!
<td>2.1</td> <td>2.2</td> <td>2.3</td>!
</tr>!
</table>!
</body>!
</html>
Table.html
@Test !
public final void testTable() {!
beginAt("Table.html");!
String[][] tableContents = !
{ { "1.1", "1.2", "1.3"}, { "2.1", "2.2", "2.3"}};!
assertTablePresent("sample_table");!
assertTableEquals("sample_table", tableContents);!
}
Forms
Form.html
<form name="sample_form" action="echo" method="get">!
...!
... <input type="text" name="user"/></td>!
... <input type="password" name="password"></td>!
... <input type="radio" name="access" value="god" checked="yes"/> !
... <input type="radio" name="access" value="root"/> !
... <input type="radio" name="access" value="regular"/> !
... <input type="submit" name="login" value="Login"/> !
...!
</form>
@Test !
public final void testFormInitialContents() {!
beginAt("Form.html");!
assertFormPresent("sample_form");!
assertTextFieldEquals("user", "");!
assertTextFieldEquals("password", "");!
assertRadioOptionPresent("access", "god");!
assertRadioOptionPresent("access", "root");!
assertRadioOptionPresent("access", "regular");!
assertRadioOptionSelected("access", "god");!
assertSubmitButtonPresent("login");!
}
type="text" name="user"/></td>!
type="password" name="password"></td>!
Form.html
type="radio" name="access" value="god" checked="yes"> !
type="radio" name="access" value="root"/>
!
echo
action using HTTP GET!
type="radio" name="access" value="regular"/> !
(will!trigger a Java servlet on the
type="submit" name="login" value="Login"/>
server side)
@Test !
public final void testFormInteraction() {!
String userName = "God himself";!
String password = "Stairway to heaven";!
String accessType = "god";!
beginAt("Form.html");!
// Fill the form and submit it!
setWorkingForm("sample_form");!
setTextField("user", userName);!
setTextField("password", password);!
clickRadioOption("access", accessType);!
submit();!
// Assert server response in the form of an HTML table!
String[][] tableContents = { !
{ "Attribute",
"Value" },!
{ "user",
userName },!
{ "password", password},!
validates HTML table
{ "access",
accessType },!
{ "login",
"Login" }!
generated by Java servlet
};!
assertTablePresent("response");!
assertTableEquals("response", tableContents);!
}