ZK Unit Testing
Luca Vix Visconti, CEO, Open Gate S.r.l.
November 25, 2008
Any version. Tested with zk 3.0.x and 3.5.x
Background
Running automated test against application is an essential part in development of enterprise applications. In this article we introduce an easy way to unit testing zk application using Selenium and JUnit. As an example we will test the Zk Demo Application
Prerequisites
We assume that you have a working Eclipse 3.x installed and Firefox.
Overview
Selenium is a unit testing suite composed by a unit test runner Selenium Remote Control, some client library for different languages ( we are interested in the java client ), a firefox plugin to record macro and an eclipse plugin for visual building test ( not covered in this small talk ). Selenium Remote Control is a server that waits for command from a client and control a browser to perform action against a target web application. In this small talk we will explain how start up the Selenium Remote Control and write a simple client in java to test some feature of the Zk demo application. Selenium IDE for Firefox is an useful plugin to test step-by-step your macros.
Obtaining the software
To download selenium visit Selenium Remote Control download page and download the latest release ( At this time 1.0 beta 1 ). Unzip the package under the directory SELENIUM_HOME For installing Selenium IDE in your Firefox browser visit the addon site and follow the normal procedure for installing Firefox addons. We need also JUnit, you can download from Sourcefoge Junit page.
Setting up the Selenium Remote Control
Go to the SELENIUM_HOME/selenium-server-1.0-beta-1/ directory and launch
java -jar selenium-server.jar
Configuring Firefox
The Selenium Remote Control act as a proxy, so, to have all the test run,you have to set up the browser proxy configuration. Create a proxy.pac as the one reported below. Go to Preferences > Advanced > Network > Settings and set automatic proxy configuration url to file:///path/to/your/proxy.pac
For windows users:In Internet Explorer you must specify the pac file with a slash less: file://path/to/your/proxy.pac
Example proxy.pac
function FindProxyForURL(url, host) {
if ( shExpMatch(url,"*selenium-server*") ) {
return "PROXY localhost:4444; DIRECT"; //The Selenium Remote Control
} else {
if ( shExpMatch(host,"localhost") ) {
return "DIRECT";
} else {
return "PROXY your.proxy.com:proxyport; DIRECT"; // or DIRECT like localhost if you have no proxy
}
}
}
Check that everithing is working smoothly visiting for example, http://google.com, and http://google.com/selenium-server. If everithing is ok you must see the following error message for the second link.
HTTP ERROR: 404 /selenium-server/ Not Found RequestURI=/selenium-server/ Powered by Jetty://
You hava also to go in about:config and set to false the browser.sessionstore.resume_from_crash property.
Creating the Test
Open eclipse and create a new Java Project. Create a lib folder and import the required libraries:
- selenium-java-client-driver.jar from SELENIUM_HOME/selenium-java-client-driver-1.0-beta-1
- junit-3.8.1.jar downloaded from sourceforge.
Than select the libraries, right-click and select Build Path > Add to Build Path
Under src, in the default package create a new class ZkDemoTest and past in the following code:
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
public class ZkDemoTest extends SeleneseTestCase {
@Override
public void setUp() throws Exception {
setUp("http://www.zkoss.org/", "*custom /usr/bin/firefox");
}
@Override
public void tearDown() throws Exception {
super.tearDown();
selenium.stop();
}
public void testNew() throws Exception {
selenium.open("/zkdemo/userguide/");
selenium.click("//div[text()='Paging']");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try {
if (selenium.isElementPresent("//h4[text()='Listboxes with paging control']")) break;
} catch (Exception e) {}
Thread.sleep(1000);
}
selenium.click("//button[@class='z-paging-next']");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try {
if ("2".equals(selenium.getValue("//input[@class='z-paging-inp']"))) break;
} catch (Exception e) {}
Thread.sleep(1000);
}
selenium.click("//button[@class='z-paging-next']");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try {
if ("3".equals(selenium.getValue("//input[@class='z-paging-inp']"))) break;
} catch (Exception e) {}
Thread.sleep(1000);
}
selenium.click("//button[@class='z-paging-next']");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try {
if ("4".equals(selenium.getValue("//input[@class='z-paging-inp']"))) break;
} catch (Exception e) {}
Thread.sleep(1000);
}
selenium.click("//div[text()='The onChanging event']");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try {
if (selenium.isElementPresent("//h4[text()='The onChanging event']")) break;
} catch (Exception e) {}
Thread.sleep(1000);
}
selenium.type("//span[text()='onChanging textbox: ']//../../..//input", "testme");
Thread.sleep(1000);
assertEquals("testme", selenium.getValue("//span[text()='instant copy: ']//../../..//input"));
}
}
The setUp method define site base url and browser. Internet Explore user can change the second parameters with IE path.
To run the test right-click on ZkDemoTest.java and select Run As > JUnite Test
Writing your own tests
To simplify the writing of your own test you can start using Selenium IDE. Using Firefox, after installing the plugin as described in the previous paragraph, go to the site you want to test and open the IDE, ( Tools > Selenium IDE ).
Insert in the base URL the one of the web application and click the record button ( the red one in top right corner ). After you have recorded some command probabily you will need to edit. Selenium, as other test suite, use the ID of the components. If you use annotate data binder remeber to fire an onBlur event ( fireEvent ) after using type command. If you want you can also modify you zk components dsp to include a z.id attibute and refer the html elements as //element[@z.id='myZkId']. Once you have finished you can go to Options > Format > Java - Selenium RC and paste the result into you java class.
Copyright © Luca Vix Visconti. This article is licensed under GNU Free Documentation License. |