Revision: 874 http://jwebunit.svn.sourceforge.net/jwebunit/?rev=874&view=rev Author: henryju Date: 2010-10-20 13:10:11 +0000 (Wed, 20 Oct 2010)
Log Message: ----------- JUnit 4 migration part 6: updated documentation. Modified Paths: -------------- trunk/jwebunit-htmlunit-plugin/src/site/xdoc/index.xml trunk/jwebunit-selenium-plugin/src/site/xdoc/index.xml trunk/src/site/xdoc/articles.xml trunk/src/site/xdoc/building-maven.xml trunk/src/site/xdoc/how-to-contribute.xml trunk/src/site/xdoc/index.xml trunk/src/site/xdoc/installation.xml trunk/src/site/xdoc/quickstart.xml Modified: trunk/jwebunit-htmlunit-plugin/src/site/xdoc/index.xml =================================================================== --- trunk/jwebunit-htmlunit-plugin/src/site/xdoc/index.xml 2010-10-20 12:19:47 UTC (rev 873) +++ trunk/jwebunit-htmlunit-plugin/src/site/xdoc/index.xml 2010-10-20 13:10:11 UTC (rev 874) @@ -53,10 +53,11 @@ import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; import com.gargoylesoftware.htmlunit.html.HtmlTextInput; -import junit.framework.TestCase; +import org.junit.Test; -public class SearchExample extends TestCase { +public class SearchExample { + @Test public void testSearch() throws Exception { final WebClient webClient = new WebClient(); final URL url = new URL("http://www.google.com"); @@ -76,17 +77,20 @@ </td> <td valign="top" nowrap="nowrap"> <source> -import net.sourceforge.jwebunit.util.TestingEngineRegistry; -import net.sourceforge.jwebunit.junit.WebTestCase; +import org.junit.Before; +import org.junit.Test; -public class SearchExample extends WebTestCase { +import static net.sourceforge.jwebunit.junit.JWebUnit.*; + +public class SearchExample { - public void setUp() { - setTestingEngineKey(TestingEngineRegistry.TESTING_ENGINE_HTMLUNIT); - getTestContext().setBaseUrl("http://www.google.com"); - } + @Before + public void prepare() { + setBaseUrl("http://www.google.com"); + } - public void testSearch() throws Exception { + @Test + public void testSearch() { beginAt("/"); setTextField("q", "htmlunit"); submit("btnG"); @@ -111,7 +115,7 @@ <dependency> <groupId>net.sourceforge.jwebunit</groupId> <artifactId>jwebunit-htmlunit-plugin</artifactId> - <version>2.0</version> + <version>3.0</version> </dependency> ... </dependencies> Modified: trunk/jwebunit-selenium-plugin/src/site/xdoc/index.xml =================================================================== --- trunk/jwebunit-selenium-plugin/src/site/xdoc/index.xml 2010-10-20 12:19:47 UTC (rev 873) +++ trunk/jwebunit-selenium-plugin/src/site/xdoc/index.xml 2010-10-20 13:10:11 UTC (rev 874) @@ -49,7 +49,7 @@ <dependency> <groupId>net.sourceforge.jwebunit</groupId> <artifactId>jwebunit-selenium-plugin</artifactId> - <version>2.0</version> + <version>3.0</version> </dependency> ... </dependencies> Modified: trunk/src/site/xdoc/articles.xml =================================================================== --- trunk/src/site/xdoc/articles.xml 2010-10-20 12:19:47 UTC (rev 873) +++ trunk/src/site/xdoc/articles.xml 2010-10-20 13:10:11 UTC (rev 874) @@ -30,9 +30,11 @@ <p> <ul> - <li><a href="http://www.brodwall.com/johannes/blog/2006/12/10/in-process-web-integration-tests-with-jetty-and-jwebunit/">In-process Web Integration Tests with Jetty and JWebUnit</a></li> + <li><a href="http://johannesbrodwall.com/2006/12/10/in-process-web-integration-tests-with-jetty-and-jwebunit/">In-process Web Integration Tests with Jetty and JWebUnit</a></li> <li><a href="http://portletwork.blogspot.com/2007/08/testing-portlets-with-jetty-pluto-and.html">Testing Portlets with Jetty, Pluto and JWebUnit</a></li> <li><a href="http://blog.ippon.fr/2008/11/14/testez-vos-applications-graphiques-web">Testez vos applications graphiques web (FR)</a></li> + <li><a href="http://www.wakaleo.com/component/content/article/183">Web testing BDD-style with JWebUnit and Easyb</a></li> + <li><a href="http://cvalcarcel.wordpress.com/2009/12/13/and-now-by-popular-demand-jwebunit/">Eclipse development, Software Development, Testing > And Now By Popular Demand: jWebUnit</a></li> </ul> </p> Modified: trunk/src/site/xdoc/building-maven.xml =================================================================== --- trunk/src/site/xdoc/building-maven.xml 2010-10-20 12:19:47 UTC (rev 873) +++ trunk/src/site/xdoc/building-maven.xml 2010-10-20 13:10:11 UTC (rev 874) @@ -26,18 +26,12 @@ <title>Building JWebUnit</title> </properties> <body class="default"> - <section name="Building JWebUnit with Maven 2"> + <section name="Building JWebUnit"> <p> - Maven is, just like ant, a build tool. However, where - you have to tell ant what it needs to do and how, you - only have to tell Maven what you need, if you adhere to - the Maven standards. - </p> - <p> - In order to use Maven to build JWebUnit, you need to - have Maven 2 installed. This should not be any more + In order to build JWebUnit, you need to + have Maven installed. This should not be any more difficult than installing ant. We've included a best - practise installation and configuration for Maven. + practice installation and configuration for Maven. </p> <subsection name="Installing Sun JDK 1.5"> <p> @@ -46,35 +40,35 @@ <p> You need to download and install latest Sun JDK 1.5 for your platform. Let's say the JDK location is: - <source>/opt/jdk1.5.0_15</source> + <source>/opt/jdk1.5.0_22</source> </p> </subsection> <subsection name="Installing Maven"> <p> - First you need to download the latest Maven 2, which + First you need to download the latest Maven, which currently is <a href="http://maven.apache.org"> - <tt>Maven-2.2.1</tt> + <tt>Maven-3.0</tt> </a> , make sure you download the binary archive - (e.g. apache-maven-2.2.1-bin.tar.bz2). + (e.g. apache-maven-3.0-bin.tar.bz2). </p> <p> - Installing Maven should be easy: unzip the file to + Installing Maven should be easy: extract the file to any directory you like, let's say: - <source>/opt/apache-maven-2.2.1</source> + <source>/opt/apache-maven-3.0</source> </p> <p> Next you need to do 2 things: <ul> <li> - add an environment variable MAVEN_HOME which + add an environment variable M2_HOME which points to the install directory of Maven, - i.e. /opt/apache-maven-2.2.1 + i.e. /opt/apache-maven-3.0 </li> <li> add - <tt>$MAVEN_HOME/bin</tt> + <tt>$M2_HOME/bin</tt> (Linux) or <tt>%M2_HOME%\bin</tt> (Windows) to your path (using the variable you can switch more easily when a new version arrives) @@ -89,7 +83,7 @@ <subsection name="Configure Maven toolchains"> <p> You can run Maven with any JDK (let's say JDK 1.6) but JWebUnit should be compiled - with JDK 1.5. To achieve this we are using Maven toolchains mecanism. + with JDK 1.5. To achieve this we are using <a href="http://maven.apache.org/guides/mini/guide-using-toolchains.html">Maven toolchains mecanism</a>. </p> <p> Create a file <tt>~/.m2/toolchains.xml</tt> with the given content: @@ -105,7 +99,7 @@ <id>1.5</id> </provides> <configuration> - <jdkHome>/opt/jdk1.5.0_15</jdkHome> + <jdkHome>/opt/jdk1.5.0_22</jdkHome> </configuration> </toolchain> </toolchains>]]> Modified: trunk/src/site/xdoc/how-to-contribute.xml =================================================================== --- trunk/src/site/xdoc/how-to-contribute.xml 2010-10-20 12:19:47 UTC (rev 873) +++ trunk/src/site/xdoc/how-to-contribute.xml 2010-10-20 13:10:11 UTC (rev 874) @@ -41,7 +41,7 @@ Improve Web 2.0 support (AJAX, drag&drop, focus, mouse clicks, keypress, ...). Will need additions/modifications of the API. </li> <li> - Take advantage of Java 5 (migrate to JUnit 4, use generics, ...) + Take advantage of Java 5 (use generics, ...) </li> <li> Create a record tool like SeleniumIDE or WebTestRecorder. Modified: trunk/src/site/xdoc/index.xml =================================================================== --- trunk/src/site/xdoc/index.xml 2010-10-20 12:19:47 UTC (rev 873) +++ trunk/src/site/xdoc/index.xml 2010-10-20 13:10:11 UTC (rev 874) @@ -55,7 +55,7 @@ </p> <p> - The current version of JWebUnit is 2.4. This is the "stable" version of + The current version of JWebUnit is 3.0. This is the "stable" version of JWebUnit, and requires Java 1.5. </p> </section> Modified: trunk/src/site/xdoc/installation.xml =================================================================== --- trunk/src/site/xdoc/installation.xml 2010-10-20 12:19:47 UTC (rev 873) +++ trunk/src/site/xdoc/installation.xml 2010-10-20 13:10:11 UTC (rev 874) @@ -29,7 +29,7 @@ <section name="JWebUnit Installation"> <subsection name="Using Maven"> <p> - If your project use Maven 2, it's very simple. Just add the plugin you want as dependency: + If your project use Maven, it's very simple. Just add the plugin you want as dependency: </p> <source><![CDATA[ <?xml version="1.0" encoding="UTF-8"?> @@ -43,7 +43,7 @@ <dependency> <groupId>net.sourceforge.jwebunit</groupId> <artifactId>jwebunit-htmlunit-plugin</artifactId> - <version>2.2</version> + <version>3.0</version> <scope>test</scope> </dependency> </dependencies> Modified: trunk/src/site/xdoc/quickstart.xml =================================================================== --- trunk/src/site/xdoc/quickstart.xml 2010-10-20 12:19:47 UTC (rev 873) +++ trunk/src/site/xdoc/quickstart.xml 2010-10-20 13:10:11 UTC (rev 874) @@ -32,9 +32,67 @@ of the methods available, consult the <a href="apidocs/index.html">Javadocs</a> - particularly the WebTestCase class for full documentation. </p> -<subsection name="Creating a TestCase"> +<subsection name="Creating a JUnit 4 TestCase"> <p> - JWebUnit uses two approaches for creating test cases: inheritance and delegation. The simplest is + 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. + + <source> +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!"); + } +} + </source> + 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. + + <source> +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!"); + } +} + </source> +In the following samples, JUnit 4 and static import will be used. +</p> +</subsection> + +<subsection name="Creating a JUnit 3 TestCase (deprecated)"> +<p> + 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. <source> @@ -48,7 +106,7 @@ } public void test1() { - beginAt("/home"); + beginAt("home.xhtml"); //Open the browser on http://localhost:8080/test/home.xhtml clickLink("login"); assertTitleEquals("Login"); setTextField("username", "test"); @@ -60,7 +118,8 @@ </source> 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. - + <b>WARNING: WebTester was migrated to JUnit 4. As a result all assertXX will throw java.lang.AssertionError + instead of old junit.framework.AssertionFailedError.</b> <source> import junit.framework.TestCase; import net.sourceforge.jwebunit.junit.WebTester; @@ -71,10 +130,11 @@ public void setUp() { super.setUp(); tester = new WebTester(); + tester.setBaseUrl("http://localhost:8080/test"); } public void test1() { - tester.beginAt("/home"); + tester.beginAt("home.xhtml"); //Open the browser on http://localhost:8080/test/home.xhtml tester.clickLink("login"); tester.assertTitleEquals("Login"); tester.setTextField("username", "test"); @@ -84,7 +144,6 @@ } } </source> -In the following samples, inheritance will be used. </p> </subsection> @@ -92,12 +151,15 @@ <p> JWebUnit can use different plugins to execute the tests you write. Only one line makes the difference: <source> -import net.sourceforge.jwebunit.junit.WebTestCase; import net.sourceforge.jwebunit.util.TestingEngineRegistry; +import org.junit.Before; -public class ExampleWebTestCase extends WebTestCase { - public void setUp() { - super.setUp(); +import static net.sourceforge.jwebunit.junit.JWebUnit.*; + +public class ExampleWebTestCase { + + @Before + public void prepare() { setTestingEngineKey(TestingEngineRegistry.TESTING_ENGINE_HTMLUNIT); <i>// use HtmlUnit</i> setTestingEngineKey(TestingEngineRegistry.TESTING_ENGINE_SELENIUM); <i>// use Selenium</i> } @@ -113,11 +175,11 @@ The primary way that JWebUnit allows you to test your web application is through navigation of the application itself. You can consider each test case as a use case through the application itself. The first step is to point where the testable application is hosted - so that it may be accessed by JWebUnit. + so that it may be accessed by JWebUnit. It can generally be done in a test fixture. <source> - public void setUp() throws Exception { - super.setUp(); + @Before + public void prepare() { setBaseUrl("http://myserver:8080/myapp"); } </source> @@ -135,8 +197,9 @@ under "Login", you may test these assertions through the following code: <source> + @Test public void testIndexLogin() { - beginAt("/index.html"); <i>// start at index.html</i> + beginAt("index.html"); <i>// start at index.html</i> assertTitleEquals("Home"); <i>// the home page should be titled "Home"</i> assertLinkPresent("Login"); <i>// there should be a "Login" link</i> clickLink("Login"); <i>// click the link</i> @@ -155,8 +218,9 @@ that it works as expected. <source> + @Test public void testFormSubmission() { - beginAt("/login.html"); + beginAt("login.html"); assertTitleEquals("Login"); <i>// we should be on the login page</i> // fill out the form @@ -186,16 +250,18 @@ For pages with more than one form, JWebUnit will usually establish which form is being worked with implicitly from the form elements being accessed. You can also set the form explicitly by form ID or name: <source> + @Test public void testBottomFormSubmission() { - beginAt("/twoForm.html"); + beginAt("twoForm.html"); setWorkingForm("bottomForm"); submit(); } </source> You can work with non-submit (type='button') buttons as well: <source> + @Test public void testPopupButton() { - beginAt("/info.html"); + beginAt("info.html"); assertButtonPresent("popupButtonId"); <i>// clickButton() will also check this</i> clickButton("popupButtonId"); assertWindowPresent("popupWindow"); @@ -212,8 +278,9 @@ as follows: </p> <source> + @Test public void testPopupWindow() { - beginAt("/rootPage.html"); + beginAt("rootPage.html"); clickLink("popupLink"); assertWindowPresent("popupWindow): <i>// optional - gotoWindow will</i> <i>// also perform this assertion.</i> @@ -226,8 +293,9 @@ You can work with frames in a similar manner: </p> <source> + @Test public void testFrame() { - beginAt("/info.html"); + beginAt("info.html"); assertFramePresent("contentFrame"); gotoFrame("contentFrame"); ... @@ -241,8 +309,9 @@ verify it's correctness. </p> <source> + @Test public void testCorrectness() { - beginAt("/mainPage"); + beginAt("mainPage.xhtml"); assertTitleEquals("Main Page"); assertLinkPresentWithText("Add Widget"); clickLinkWithText("Add Widget"); @@ -271,8 +340,9 @@ </table> </p> <source> + @Test public void testAgeTable() { - beginAt("/agePage"); + beginAt("agePage.html"); <i>// check that table is present</i> assertTablePresent("ageTable"); @@ -303,8 +373,9 @@ </table> </p> <source> + @Test public void testAgeTable() { - beginAt("/agePage"); + beginAt("agePage.html"); ExpectedTable ageTable = new Table(new Object[][] { {new Cell("Age Table", 2, 1)}, {"Name", "Age"}, @@ -327,8 +398,9 @@ <p><![CDATA[<span id="welcomeMessage">Welcome, Joe User!</span>]]> </p> <source> + @Test public void testWelcomeMessage() { - beginAt("/mainPage"); + beginAt("mainPage.xhtml"); <i>// check for presence of welcome message by text</i> assertTextPresent("Welcome, Joe User!"); @@ -353,20 +425,21 @@ Locale (default is Locale.getDefault()). </p> <source> - public void setUp() throws Exception { - super.setUp(); - getTestContext().setbaseUrl("http://myserver:8080/myapp"); + @Before + public void prepare() { + setbaseUrl("http://myserver:8080/myapp"); getTestContext().setResourceBundleName("ApplicationResources"); } + @Test public void testMainPage() { - beginAt("/mainPage"); + beginAt("mainPage.html"); assertTitleEqualsKey("title.mainPage"); assertKeyPresent("message.welcome"); assertKeyInTable("mainPageTable", "header.mainPageTable"); } </source> - There is also a getMessage() method on WebTester and WebTestCase that can be used to access + There is also a getMessage() method on WebTester, WebTestCase and JWebUnit that can be used to access resource bundle messages directly in your tests. </p> <p> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Download new Adobe(R) Flash(R) Builder(TM) 4 The new Adobe(R) Flex(R) 4 and Flash(R) Builder(TM) 4 (formerly Flex(R) Builder(TM)) enable the development of rich applications that run across multiple browsers and platforms. Download your free trials today! http://p.sf.net/sfu/adobe-dev2dev _______________________________________________ JWebUnit-development mailing list JWebUnit-development@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jwebunit-development