Professional Documents
Culture Documents
The QuickStart contains sample code and guidance to get you started with jWebUnit.
Creating a TestCase
jWebUnit uses two approaches for creating test cases: inheritance and delegation.
The simplest is to inherit from WebTestCase rather than
junit.framework.TestCase.
import net.sourceforge.jwebunit.WebTestCase;
import junit.framework.TestCase;
import net.sourceforge.jwebunit.WebTester;
Now that the TestCase is pointed at your application, you can navigate to a
particular resource.
For example, if you have a html page, info.html, located under your
application root, then you can access it in a test method with the following
code:
public void testInfoPage() {
beginAt("/info.html");
}
You can also navigate through links either by text contained in the link or by the link's id:
You can also check for the presence of submit buttons on the form and submit
with a specific button.
public void testFormSubmission() {
beginAt("/info.html");
assertFormPresent("expectedFormName");
assertSubmitButtonPresent("Add");
assertSubmitButtonPresent("Modify");
setFormElement("field1", "value1");
submit("Modify");
}
For pages with more than one form, jWebUnit will establish which form is being
worked with implicitly from the form elements checked or set.
You can also set the form explicitly by form id or name:
In the above examples, we are working with submit buttons and form elements by their
name. For form elements, it is possible to work with them by label (string followed by
colon before input element). Before using this approach, consider that the display label
may change, but the submission name of the element will probably remain static.
Once you have navigated to the page you wish to test, you can call the assertions
provided by jWebUnit to verify it's correctness.
The below test validates against this html table (the table id attribute is "ageTable"):
Name Age
Jim 30ish
Wilkes 20ish
If you need to validate non-blank table cells that span more than a single column,
a set of classes are provided which represent expected tables, rows, and cells.
Age Table
Name Age
Subhajit 25
Hari 23
public void testAgeTable() {
beginAt("/agePage");
ExpectedTable ageTable = new ExpectedTable(new Object[][] {
{new ExpectedCell("Age Table", 2},
{"Name", "Age"},
{"Subhajit", "25"},
{"Hari", "23"}
});
assertTableEquals("ageTable", expectedAgeTable);
}
jWebUnit allows you to check for the presence of any html element by its id, or
for text in an element.
This can be a useful trick to check for the presence of some logical part of the
page.
In the case of free floating text, a span element can be used to surround the text
and give it a logical id:
One useful testing trick is to use property files for your expected values rather
than hard-coded strings.
This is especially useful if your application uses property files as a source for
static text and messages.
The same files can be used to provide the expected values for the tests.
For convenience, most of the jWebUnit assertions that take strings for the
expected have an equivalent assertion that will take a property file key.
TestContext is used to set the bundle to be used as well as the Locale (default is
Locale.getDefault()).
Whether to check for a given logical chunk of text by hard-coded string, property file
lookup, or by use of an element id is up to you.