JUnit testcases
You can use the commands also in JUnit testcases. The easiest way is to run a testcase. First you need to instantiate the WebTester and then add all commands using the parseLine method. The syntax is the same as it is in the script files. Then you can call the runTest() method and get back a WebTestResult.
Here are two example for a testcase that will succeed and one that will fail.
public class WebTesterTest extends TestCase {
public void testSimpleTestWithError() {
WebTester tester = new WebTester("Google Test");
tester.parseLine("get_html | http://www.google.de");
tester.parseLine("check_text | Gugle Toolbar");
WebTestResult result = tester.runTest();
assertTrue(!result.isSuccess());
assertNotNull(result.getErrorNode());
assertEquals("The text:'Gugle Toolbar' was not found",result.getErrorNode().getErrorMessage());
}
public void testSimpleTestWithSubmit() {
WebTester tester = new WebTester("Google Test");
tester.parseLine("get_html | http://www.google.de");
tester.parseLine("select_form | 0 ");
tester.parseLine("set_textfield | q = jConfig");
tester.parseLine("submit_form | btnG");
tester.parseLine("print_html");
WebTestResult result = tester.runTest();
assertTrue(result.isSuccess());
assertNull(result.getErrorNode());
}
}