Web| Program | Create are you looking for a way to apply automated test technology to WEB development? So no more looking! Jwebunit is an open source framework for creating test cases for WEB applications that can be easily inserted into most Java Ides. By using a sample application, the detailed steps for generating a concise test case are described, and the software engineer Amit Tuli the jwebunit.
Automated tests can save time and effort to perform the same test steps repeatedly. This article describes Jwebunit, a set of Java classes that you can use to develop test cases for WEB applications. Jwebunit is an Open-source project that can be obtained free of charge under the BSD license. I'll explain how to download the Jwebunit library, configure the Eclipse platform to develop jwebunit test cases, and build a sample test case.
Jwebunit Introduction
Jwebunit is based on HttpUnit (a Java library for WEB application automated testing) and the JUnit Unit test framework (see Resources). Jwebunit provides an advanced API for navigating WEB applications and combines a set of assertions to verify the correctness of link navigation, form entry and submission, tabular content, and other typical business Web application features. Jwebunit is provided as a JAR file and can easily be inserted into most Ides, Jwebunit also contains other necessary libraries.
To test with HttpUnit
Automatically testing a Web application means skipping a Web browser and working on a Web site through a program. First, I want to introduce how httpunit (one of the building blocks of Jwebunit) simplifies this work. HttpUnit can simulate frames, JavaScript, page redirection cookies, and so on. When you use HttpUnit with JUnit, it can quickly validate the functionality of your Web site.
Listing 1 shows a test case written with HttpUnit that tries to click the "Cookbook" link on the HttpUnit home page:
Listing 1. Click the HttpUnit code for the Cookbook link on the HttpUnit home page
1 public class Httpunittest {2 public static void Main (string[] args) {3 try {4 webconversation WC = new Webcon Versation (); 5 WebRequest request = new Getmethodwebrequest ("http://httpunit.sourceforge.net/index.html"); 6 Wc.setproxyserver ("your.proxy.com", N); 7 WebResponse response = Wc.getresponse (request); 8 Weblink http Unitlink = Response.getfirstmatchinglink (Weblink.match_contained_text, "Cookbook"); 9 response = Httpunitli Nk.click (); System.out.println ("Test successful!!");
One} catch (Exception e) {System.err.println ("Exception:" + e);}14}15}
|
The code in Listing 1 connects to the Internet with your.proxy.com (line 6th). If there is a direct Internet connection, you can comment out the statement. The statement on line 8th searches the page for Web links that contain text cookbook. The statement on line 9th is used to click the link. If a link is found, the user will see Test successful! This message.
It's easier to test with Jwebunit
The test cases in Listing 2 perform the same tasks as listing 1 using the Jwebunit API:
Listing 2. Click the Jwebunit code for the Cookbook link on the HttpUnit home page
1 public class Jwebunittest extends webtestcase{2 public static void Main (string[] args) {3 Junit.textui.TestRunner.run (New TestSuite (Jwebunittest.class)); 4 }5 public void SetUp () {6 Gettestcontext (). Setbaseurl ("Http://httpunit.sourceforge.net"); 7 Gettestcontext (). Setproxyname (" Webproxy.watson.ibm.com "); 8 Gettestcontext (). Setproxyport (8080); 9 }10 public void Testsearch () {11 Beginat ("/index.html"); Clicklinkwithtext ("Cookbook"); }14}
|
If you don't notice the JUnit-specific code in Listing 2, you can see that the test cases are now pretty neat and concise. The important lines you need to see are line 6th, line 11th, and Row 12th.
In line 6th, the base URL is set to the HttpUnit's home page. Line 11th connects the site with the relative path/index.html. Line 12th is used to click the link with text cookbook on the page. If the link is valid, JUnit will report the success, otherwise JUnit will report an exception.
Jwebunit API: Further observations
The core of each jwebunit test is the Net.sourceforge.jwebunit.WebTestCase class, which represents the test case. Each test case must be extended from this class. (The Net.sourceforge.jwebunit.WebTestCase class itself is extended from the Junit.framework.TestCase class, which represents a test case in JUnit.) Table 1 describes some of the common methods of this class:
Table 1. An important method of Net.sourceforge.jwebunit.WebTestCase class Another important class is net.sourceforge.jwebunit.TestContext. It creates a context for the test. You can use this class to handle information like cookies, sessions, and authorizations. Table 2 shows some important methods for this class:
Table 2. An important method of Net.sourceforge.jwebunit.TestContext class
Download Jwebunit, configure Jwebunit in Eclipse
Jwebunit is implemented in pure Java code, so you can get it in the form of a JAR file (see Resources for a download link). After you complete the download, use the following procedure to configure the Jwebunit library on the Eclipse platform:
Release the downloaded file jwebunit-1.2.zip to the temporary directory (assuming it is C:\temp).
Create a new Java project in Eclipse and name it jwebunit.
Right-click the Jwebunit item in the Package Explorer view and select Properties.
Click Java Build Path. Click the Add External JARs in the Libraries tab.
Browse to the C:\temp\jwebunit-1.2\lib directory and select all the JAR files in this directory.
Click OK.
You can now develop jwebunit test cases under the Jwebunit project in Eclipse. Building the sample application
You can now view the actual application of the Jwebunit API. I'll take you through a sample application to help you better understand the true power of jwebunit. This application is a test case that opens a Google search page and searches for text httpunit. The application needs to test the following scenario:
Open the Google homepage http://www.google.com.
Make sure that the page contains a form element named Q. (On Google's homepage, a text box named Q is a text box that accepts input from a user query.) The application uses this element to enter search parameters.
Enter the string HttpUnit home in the search text box and submit the form.
Get the results page and make sure that the page contains a link containing text HttpUnit home.
Click the link that contains the text HttpUnit home.
Now that the test scenario is in place, you can write Java applications and implement these requirements with Jwebunit.
The first step is to declare a class that extends from the webtestcase, as shown in Listing 3:
Listing 3. Declaring test Case classes
public class Googletest extends Webtestcase {static String searchlink = "";}
|
As I mentioned earlier, Jwebunit requires that each test case be extended from the webtestcase. Searchlink saves the incoming search parameters. This value is passed to the test case as a command-line argument.
The next step is to declare the entry point--main () method, as shown in Listing 4:
Listing 4. Main () method
public static void Main (string[] args) {searchlink = Args[0];junit.textui.testrunner.run (New TestSuite ( Googletest.class));}
|
The main () method calls Junit.textui.TestRunner.run () to execute the JTest test case. Because the googletest test case needs to be run, the test suite passed as a parameter to the run () method takes Googletest.class as an argument.
Next, JTest calls the Setup () method to set the base URL and the proxy, as shown in Listing 5:
Listing 5. Set up
public void SetUp () {Gettestcontext (). Setbaseurl ("http://www.google.com"); Gettestcontext (). Setproxyname (" Proxy.host.com "); Gettestcontext (). Setproxyport (80);}
|
Listing 5 Sets the base URL to http://www.google.com. This means that the start of the test case is relative to the URL. The following two statements set up proxy hosts and proxy ports that are connected to the Internet. If you are connecting directly to the Internet, you can ignore the proxy settings statement.
Now start browsing the site and enter search parameters. Listing 6 shows the code that accesses the Web page and then tests all the scenarios:
Listing 6. Test all scenes
public void Testsearch () {Beginat ("/"); Assertformelementpresent ("Q"); Setformelement ("Q", "HttpUnit"); Submit ("btng") ); Assertlinkpresentwithtext (Searchlink); Clicklinkwithtext (Searchlink);}
|
The code in Listing 6 connects to the base URL and browses relative to/start. It then concludes that the page contains a table cell named Q--q is the name of the query input text box on the Google home page. The Next statement sets the text box named Q with the value httpunit.
The next language submits a submit button named Btng on the form. (on Google's homepage, the button named Btng is the button labeled Google Search.) The form is submitted in this dialog, and the next page lists the search results. On the results page, the code first checks to see if there is a linked text that is HttpUnit home. If the link does not exist, then the test fails with Assertionfailederror. If the link exists, the next action that the test performs is to click the link.
Running the sample application
Now put the sample application into use:
Download the sample application J-webunitsample.jar.
Unzip the J-webunitsample.jar in a directory. For example, if you release it into the C:\temp, you put the source file and class file in the C:\temp\com\jweb\test, and Setclasspth.bat in C:\temp.
Edit Setclasspath.bat: Set Jar_base point to the directory that contains all the required JAR files. For example, if you release the Jwebunit-1.2.zip file in C:\temp, the jar_base is set to C:\temp\jwebunit-1.2\lib.
Open a command line prompt and switch to the C:\temp directory.
Perform setclasspath.bat. This sets the CLASSPATH required to execute the test case.
Run the application with the command Java com.jweb.test.GoogleTest "HttpUnit home".
Sample output
After the test case is executed, a test case report is exported at the command line. If the test fails, the report looks like the following in Listing 7:
Listing 7. Output with assertion failure
C:\temp>java com.jweb.test.GoogleTest "HttpUnit Hwee". Ftime:5.338there was 1 failure:1) Testsearch (com.jweb.test.GoogleTest) Junit.framework.AssertionFailedError:Link With the text [HttpUnit Hwee] not found in response.at net.sourceforge.jwebunit.WebTester.assertLinkPresentWithText ( webtester.java:618) at Net.sourceforge.jwebunit.WebTestCase.assertLinkPresentWithText (webtestcase.java:244) at Com.jweb.test.GoogleTest.testSearch (googletest.java:36) at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method in Sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source) at Sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source) at Com.jweb.test.GoogleTest.main (Googletest.java : Failures!!! Tests Run:1, failures:1, errors:0
|
As you can see in Listing 7, you can use HttpUnit Hwee as a parameter to execute the test case. This test case will fail if it encounters an assertion, because the resulting page does not contain a link with this text. Thus, Junit.framework.AssertionFailedError is produced.
Listing 8 is executed with HttpUnit home as an argument. The test case finds a link with this text, so the test passes:
Listing 8. Output of a successful test
C:\temp>java com.jweb.test.GoogleTest "HttpUnit Home". Time:6.991ok (1 Test)
|
Conclusion
By discussing some of the salient features of the JWEBUNIT framework and the most important classes, this article describes how to use it to create concise test cases that give you an understanding of the Jwebunit framework. Jwebunit also has more features that can be used in test cases. It supports testing the number of link lines in a Web page. You can assert whether a string, table, or form INPUT element with a specified label exists on the page. In addition, Jwebunit can handle cookies, such as asserting a cookie, deleting cookies, and so on. Test to click a link to a specific text that appears after a text. If you want to build fast and effective test cases for your WEB application, Jwebunit may be your best friend.