JWebUnit TestCases
|
|
|
|
| Articles Reviews Java | |
| Written by Larry Gomez | |
| Wednesday, 30 May 2007 | |
|
Because JWebUnit is based on JUnit, the concepts of test cases, suites, and fixtures are the same. Let’s create tests for a couple of the pages of the eMotherEarth application as an example. One of the setup items common to all the test cases in JWebUnit is the BaseURL. All the other URLs in the test case are based on this URL.
Instead of replicating the same setup code across multiple test cases, let’s create a base test case that handles this setup chore. The BaseWebTestCase appears like in the next listing: package com.nealford.art.emotherearth.test; import net.sourceforge.jwebunit.WebTestCase; public class BaseWebTestCase extends WebTestCase { Once the base test case is established, we can subclass it to create tests for pages in the web application. The first test is for the logon page. We want to ensure that the proper elements appear on the page and that it forwards successfully to the catalog page. The TestLogonPage test case is shown in the next listing: package com.nealford.art.emotherearth.test; HttpUnit has extended the standard assert methods in JUnit to include webspecific assertions. The testLogonElements() method checks to see if the required elements are on the page. The framework also includes methods that allow the developer to programmatically interact with the application. The testForwardToCatalog() method lets the developer fill in form values, “click” buttons, and otherwise interact with the web application. In other words, the test runner will not automatically spawn the web application. Testing complex elements JWebUnit contains methods for testing sophisticated HTML elements such as tables. The next listing shows a test case that tests some of the table properties of the catalog page. package com.nealford.art.emotherearth.test; import java.io.File; import java.io.FileNotFoundException; The testCatalog() method of the test case first issues two beginAt() method invocations. We cannot create a test case that goes directly to the catalog page because the welcome page executes code that establishes connection pools and other global resources. To solve this problem, we issue a beginAt() command that invokes the welcome page and then immediately moves to the catalog page, passing the parameter normally supplied by the welcome page. JWebUnit includes methods that ensure the presence of a table and that check the contents of individual rows. The assertTextInTable() method verifies that the header row contains the correct elements. The contents of the catalogText file appear in the next listing. catalogTable: Even the simple tests defined in this chapter begin to show the power of automating tests against web applications. Although the generated output doesn’t look like the original table, it does contain the same data. If you run this test as a regression test, you can compare the contents from one run to another using a diff utility. Powered by jReviews |
|
| Last Updated ( Sunday, 07 September 2008 ) | |
| < Prev | Next > |
|---|







