jWebUnit framework for testing Web applications becomes easy

Are you looking for an automatic testing technique applied to Web development methods? Well, do not find the! jWebUnit applications for the Web to create an open source framework for test cases, it can be easily inserted into the majority in the Java IDE. Through the use of an example application, described the simple test case to generate detailed steps, a software engineer Amit Tuli introduction of a jWebUnit.
Automated test can save steps to duplicate the same test of time and energy. This article will introduce jWebUnit, This is a set of Java class, for Web application development for the test case. jWebUnit is an open source project can be obtained free of charge under the BSD license. I will introduce how to download jWebUnit database, configure Eclipse platform to develop test cases jWebUnit, as well as an example of how to construct test cases.

About jWebUnit
jWebUnit to HttpUnit (a Web application for automated testing of Java library) and the JUnit unit testing framework (see references). Web navigation jWebUnit provides advanced application API, and a set of combinations that use them to verify that the link navigation, form entry and submission, the form content, as well as other typical characteristics of commercial Web application is correct. jWebUnit in the form of JAR files, you can easily insert it in the IDE most, jWebUnit also includes other necessary libraries.

Testing with HttpUnit
On the Web application automatically skip the test means that the Web browser, through the procedures to deal with Web sites. First of all, I would like to introduce HttpUnit (JWebUnit one building block) is how to simplify this work. HttpUnit can simulate frames, JavaScript, the page redirects cookie, and so on. HttpUnit in for JUnit, it can be quickly and functional Web site to verify.

1 shows a list with the HttpUnit test cases to prepare, it try to click on the HttpUnit Homepage "Cookbook" Link:

1. HttpUnit Homepage click on the link HttpUnit code Cookbook

1 public class HttpUnitTest (
2 public static void main (String [] args) (
3 try (
4 WebConversation wc = new WebConversation ();
5 WebRequest request =
new GetMethodWebRequest ( "http://httpunit.sourceforge.net/index.html");
6 wc.setProxyServer ( "your.proxy.com", 80);
7 WebResponse response = wc.getResponse (request);
8 WebLink httpunitLink =
response.getFirstMatchingLink (WebLink.MATCH_CONTAINED_TEXT, "Cookbook");
9 response = httpunitLink.click ();
10 System.out.println ( "Test successful !!");
11) catch (Exception e) (
12 System.err.println ( "Exception:" + e);

One of the code list used your.proxy.com (line 6) to connect Internet. If there is a direct Internet connection, you can comment out this statement. Article 8 of the statement line in search of the page that contains the text of the Web link Cookbook. Article 9 of the statement line for click this link. If you find a link, the user will see the Test Successful!! This message.

JWebUnit test with a more easy
2 list the test cases jWebUnit API implementation and use one the same list of tasks:

List 2. HttpUnit Homepage click on the link jWebUnit code Cookbook

1 public class JWebUnitTest extends WebTestCase (
2 public static void main (String [] args) (
3 junit.textui.TestRunner.run (new TestSuite (JWebUnitTest.class));
5 public void setUp () (
6 getTestContext (). SetBaseUrl ( "http://httpunit.sourceforge.net");
7 getTestContext (). SetProxyName ( "webproxy.watson.ibm.com");
8 getTestContext (). SetProxyPort (8080);
10 public void testSearch () (
11 beginAt ( "/ index.html");
12 clickLinkWithText ( "Cookbook");

Note 2 If there is no list of specific code in JUnit, you can see that the test case has now become very clean, concise. The essential need to see the line 6 is the first line, 11 line and 12 lines. At line 6, the basic URL is set to the Home page of HttpUnit. No. 11 line with the relative path / index.html Connect site. Line 12 for the first page click on the link with the text Cookbook. If the link valid, then the success of JUnit report; otherwise, JUnit will report abnormal.

jWebUnit API: further observation
JWebUnit the core of each test are net.sourceforge.jwebunit.WebTestCase category, it represents the test case. Each test case must be coming from this type of expansion. (Net.sourceforge.jwebunit.WebTestCase Class junit.framework.TestCase itself from the Class are extended from it on behalf of JUnit test cases.) Table 1 describes some of the categories commonly used methods:

Table 1. Net.sourceforge.jwebunit.WebTestCase Ways important category
public TestContext getTestContext () get the context of test cases. Can use it to visit the region as the basic URL and cookie projects like
public void beginAt (String relativeURL) in relation to the basic URL in the URL start dialogue
public void setWorkingForm (String nameOrId) beginning with the designated interactive form. If the current form there is only one page, you do not need to call this method
protected void submit () submit the form - equivalent to the form click the submit button
public void gotoFrame (String frameName) named activation frame

Another important category is net.sourceforge.jwebunit.TestContext. Create a context for the test. This class can be used to deal with, like cookie, session and authorization information, such as. Table 2 shows some of the important ways in category:

Table 2. Net.sourceforge.jwebunit.TestContext Ways important category
public void addCookie (String name, String value) to add context to the test cookie. At the beginning of HttpUnitDialog, add the cookie to be set up WebConversation
public void setResourceBundleName (String name) for testing the context of the resources to set up a bind. WebTester used in accordance with the expectations of the search key value
public void setProxyName (String proxyName) for testing the context of the name of the proxy server settings
public void setBaseUrl (String url) Set the context for testing the basic URL

Download jWebUnit, configure Eclipse at jWebUnit
jWebUnit is the implementation of pure Java code, so can the form of JAR files to obtain it (please refer to reference materials, get download link). After completion of download, please follow the steps below to configure the Eclipse platform jWebUnit Library:

Download the file put jwebunit-1.2.zip released into the temporary directory (assuming it is C: \ temp).
In Eclipse create a new Java project named jWebUnit.
Right-click in the Package Explorer view jWebUnit item, and then select Properties.
Click Java Build Path. Libraries tab click Add External JARs.
Browse to C: \ temp \ jwebunit-1.2 \ lib directory, select the directory of all the JAR files.
Click OK.
Eclipse is now available at jWebUnit item in the development of a test case jWebUnit.

Examples of applications to build
Now jWebUnit API can see the practical application of the. I will take you on a sample application to help you better understand the true power of jWebUnit. This application is a test case for open a Google search page and search the text of HttpUnit. Applications need to test the following scenarios:

Open the Google home page http://www.google.com.

To determine the page contains a form called the q element. (At the Home Page on Google called q are the text box to accept user input query text box.) Application to use this element enter your search parameters.

In the search string text box HttpUnit Home, and submit the form.

Results pages, and that the page contains links to the text contained in HttpUnit Home.

HttpUnit Home contains the text click the link.
Test scenario is now in place, can write Java applications, with implementation of these jWebUnit needs.

The first step is to declare a WebTestCase expansion comes from the category list such as 3 follows:

List 3. Statement Test category

public class GoogleTest extends WebTestCase (
static String searchLink = "";

As I mentioned earlier, jWebUnit are required for each test case comes from the expansion of the Medium WebTestCase. Save searchLink incoming search parameters. This value to the form of command-line parameters passed to the test case.

The next step is the point of entry statement - main () methods, such as a list of 4 follows:

The list 4. Main () method

public static void main (String [] args) (
searchLink = args [0];
junit.textui.TestRunner.run (new TestSuite (GoogleTest.class));

main () method call junit.textui.TestRunner.run () implementation JTest test cases. Since GoogleTest need to run the test case, therefore, as a parameter passed to the run () method of the test suite GoogleTest.class used as a parameter.

Next, JTest called setUp () method to set the URL and the basic agent, such as a list of 5 below:

5 list. Settings

public void setUp () (
getTestContext (). setBaseUrl ( "http://www.google.com");
getTestContext (). setProxyName ( "proxy.host.com");
getTestContext (). setProxyPort (80);

A list of five basic URL is set to http://www.google.com. This means that test cases are compared with the start of the URL. The following two statements set the agent to connect to the Internet host and port agent. If you are directly connected to the Internet, then the proxy settings can be ignored statements.

Now start browsing the site and enter your search parameters. 6 shows the list of visited Web pages, then test all the scenes of the code:

List 6. Test all scenes

public void testSearch () (
beginAt ("/");
assertFormElementPresent ( "q");
setFormElement ( "q", "HttpUnit");
submit ( "btnG");
assertLinkPresentWithText (searchLink);
clickLinkWithText (searchLink);

6 list the code to connect to the basic URL, and vis-à-vis / start browsing. And it is established that the page contains a form called the q element - q is a query on Google Home Page text box enter the name. A statement by the next set, called q value of HttpUnit text box. And then submit the form of a language called btnG on the submit button. (In Google's Home Page on the button called btnG are the tags for the Google Search button.) Forms are submitted in this dialogue, the next page are listed in search results. At results page, the code first checks whether there is a link to the text are HttpUnit Home. If the link does not exist, then the test failed on AssertionFailedError. If the links exist, then testing the implementation of the next operation is click on the link.

Running sample applications
Examples of applications now in use were:

Sample application download j-webunitsample.jar (see download).

In a directory extract j-webunitsample.jar. For example, if you put it into the C: \ temp, then on to the source file and class file on the C: \ temp \ com \ jweb \ test in the setclasspth.bat on the C: \ temp Medium.

Edit setclasspath.bat: point JAR_BASE set contains all the necessary JAR files. For example, if the C: \ temp in the release of jwebunit-1.2.zip file, it will JAR_BASE set to C: \ temp \ jwebunit-1.2 \ lib.

Open a command line prompt, switch to the C: \ temp directory.

The implementation of setclasspath.bat. This will set the implementation of test cases required CLASSPATH.

Use the command java com.jweb.test.GoogleTest "HttpUnit Home" to run applications.
Example Output
After the implementation of the test cases, at the command line will output a test case report. If the test fails, the report such as a list of 7 look like this:

List of 7. With the assertion failure output

C: \ temp> java com.jweb.test.GoogleTest "HttpUnit Hwee"
. F
Time: 5.338
There was 1 failure:
1) testSearch (com.jweb.test.GoogleTest) junit.framework.AssertionFailedError: Link
with 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)
at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source)
at com.jweb.test.GoogleTest.main (GoogleTest.java: 19)

Tests run: 1, Failures: 1, Errors: 0

7 in the list as can be seen, and can be used HttpUnit Hwee as a parameter to the implementation of test cases. Encountered in the test cases that the place will fail, because the results page does not contain a link with this text. This also resulted in junit.framework.AssertionFailedError.

8 the implementation of a list of HttpUnit Home used as a parameter. Test cases to find a link with this text, so the test passed:

8 list. Successful test output

C: \ temp> java com.jweb.test.GoogleTest "HttpUnit Home"
Time: 6.991

OK (1 test)

Concluding remarks
JWebUnit the framework of this paper to discuss some of the outstanding features and the most important category, describes how to use it to create a simple test case, so you have an understanding of the framework jWebUnit. jWebUnit have more features can be used in a test case in. It supports testing Web page, the number of rows of links. Can string, table or form with the specified tag exists in the input elements on the page for that. In addition, jWebUnit can also deal with cookie (such as an existence that cookie, delete cookie, etc..) Tests after the text of a specific text to click the link. If you want to build applications for the Web quick and effective test cases, jWebUnit are probably your best friend.


Original link: http://www-128.ibm.com/developerworks/cn/java/j-jwebunit/

分类:Development 时间:2009-05-15 人气:1892
blog comments powered by Disqus


iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.035 (s). 13 q(s)