0% found this document useful (0 votes)
76 views

Unit Test With Jwebunit

The document discusses how to write tests for a web application using JWebUnit. It describes initializing a test server, making requests to test pages and forms, and validating the responses. Sample test cases are provided to test pages like "HelloWorld" and forms by filling fields and asserting the response is as expected. The Java servlet code is also shown which generates a response table based on the form submission for the tests to validate.

Uploaded by

Yopp Lov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Unit Test With Jwebunit

The document discusses how to write tests for a web application using JWebUnit. It describes initializing a test server, making requests to test pages and forms, and validating the responses. Sample test cases are provided to test pages like "HelloWorld" and forms by filling fields and asserting the response is as expected. The Java servlet code is also shown which generates a response table based on the form submission for the tests to validate.

Uploaded by

Yopp Lov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Writing tests in JWebUnit

Verificao e Validao de Software!


Departamento de Informtica!
Faculdade de Cincias da Universidade de Lisboa!
!
Eduardo Marques, [email protected]

Toy application example


A toy web server configured with:
a static HTML page repository;
an echo Java servlet;

Test methodology:
JWebUnit to encode tests
Embedded HTTP server - using the Jetty server toolkit from
apache.org

Note: You may interact with the server in standalone manner. !


Start the vvs.website.WebServer program defined by method main(). !
2

Then redirect your browser to http://localhost:8089

Test initialisation and shutdown

public class WebServerTest {!


private static WebServer server = null;!
private static final int SERVER_PORT = 8088;!
!
@BeforeClass!
public static void startHttpServer() throws Exception {!
try {!
server = new WebServer(SERVER_PORT);!
server.start();!
} catch(Exception e) {!
e.printStackTrace(System.out);!
throw e;!
}!
System.out.println("Server started on port " !
+ SERVER_PORT);!
}!
!
@AfterClass!
public static void stopHttpServer() throws Exception{!
if (server != null)!
server.stop(); !
}

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

Links and link interaction


<html>!
...!
<li><div id="hello">!
<a href="HelloWorld.html">Hello world!</a>!
</div></li>!
<li><div id="js-enabled">!
<a href="JavaScriptEnabled.html">Is Javascript enabled?</a>!
</div></li>!
<li><div id="js-alert">!
<a href="JavaScriptAlert.html">Javascript alert test</a>!
</div></li>!
</html>

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");!
}

<form name="sample_form" action="echo" method="get">!


... <input
... <input
... <input
... <input
... <input
... <input
</form>

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);!
}

Java servlet code


...!
@SuppressWarnings("serial")!
public class EchoServlet extends HttpServlet {!
/**!
* Handle GET requests.!
*/!
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {!
// Obtain query string.!
String queryString = req.getQueryString().replace('+', ' ');!
// Set response fields.!
resp.setStatus(HttpServletResponse.SC_OK);!
resp.setContentType("text/html");!
// Produce HTML output, essentially a table with name "response".!
PrintWriter out = resp.getWriter();!
out.println("<html><head><title>Echo server response</title></head>\n<body>");!
out.println("<h1>Server response</h1>");!
if (queryString.length() == 0) {!
out.println("<b>No query string!</b><br/>");!
} else {!
out.printf("<b>Query string:</b>: \"%s\"<br/>\n", queryString);!
out.println("<table border=\"1\" name=\"response\">\n"!
+ "<tr><th>Attribute</th><th>Value</th></tr>");!
!
String[] attrList = queryString.split("&");!
for (String attr : attrList) {!
String[] pair = attr.split("=");!
out.printf("<tr><td>%s</td><td>%s</td></tr>\n", pair[0], pair[1]);!
}!
out.println("</table><br><b>No access granted :)</body></html>");!
}!
}

You might also like