JWebUnit Quickstart
This quickstart contains sample code and guidance to get you started with JWebUnit. To see all of the methods available, consult the Javadocs - particularly the WebTestCase class for full documentation.
Creating a JUnit 4 TestCase
JWebUnit uses two approaches for creating JUnit 4 test cases: static import and delegation. The simplest is to statically import all methods of net.sourceforge.jwebunit.junit.JWebUnit.
import static net.sourceforge.jwebunit.junit.JWebUnit.*; public class ExampleWebTestCase { @Before public void prepare() { setBaseUrl("http://localhost:8080/test"); } @Test public void test1() { beginAt("home.xhtml"); //Open the browser on http://localhost:8080/test/home.xhtml clickLink("login"); assertTitleEquals("Login"); setTextField("username", "test"); setTextField("password", "test123"); submit(); assertTitleEquals("Welcome, test!"); } }
An alternative is to include an instance of the WebTester class in your TestCase and delegate navigation and assertions to it. This is provided in case you need or prefer delegation.
import net.sourceforge.jwebunit.junit.WebTester; public class ExampleWebTestCase { private WebTester tester; @Before public void prepare() { tester = new WebTester(); tester.setBaseUrl("http://localhost:8080/test"); } @Test public void test1() { tester.beginAt("home.xhtml"); //Open the browser on http://localhost:8080/test/home.xhtml tester.clickLink("login"); tester.assertTitleEquals("Login"); tester.setTextField("username", "test"); tester.setTextField("password", "test123"); tester.submit(); tester.assertTitleEquals("Welcome, test!"); } }
In the following samples, JUnit 4 and static import will be used.
Creating a JUnit 3 TestCase (deprecated)
JWebUnit uses two approaches for creating JUnit 3 test cases: inheritance and delegation. The simplest is to inherit from WebTestCase rather than junit.framework.TestCase.
import net.sourceforge.jwebunit.junit.WebTestCase; public class ExampleWebTestCase extends WebTestCase { public void setUp() { super.setUp(); setBaseUrl("http://localhost:8080/test"); } public void test1() { beginAt("home.xhtml"); //Open the browser on http://localhost:8080/test/home.xhtml clickLink("login"); assertTitleEquals("Login"); setTextField("username", "test"); setTextField("password", "test123"); submit(); assertTitleEquals("Welcome, test!"); } }
An alternative is to include an instance of the WebTester class in your TestCase and delegate navigation and assertions to it. This is provided in case you need or prefer delegation.
WARNING: WebTester was migrated to JUnit 4. As a result all assertXX will throw java.lang.AssertionError instead of old junit.framework.AssertionFailedError.
import junit.framework.TestCase; import net.sourceforge.jwebunit.junit.WebTester; public class ExampleWebTestCase extends TestCase { private WebTester tester; public void setUp() { super.setUp(); tester = new WebTester(); tester.setBaseUrl("http://localhost:8080/test"); } public void test1() { tester.beginAt("home.xhtml"); //Open the browser on http://localhost:8080/test/home.xhtml tester.clickLink("login"); tester.assertTitleEquals("Login"); tester.setTextField("username", "test"); tester.setTextField("password", "test123"); tester.submit(); tester.assertTitleEquals("Welcome, test!"); } }
Comments
(No subject)