Web development , php , ajax , symfony, framework, zend, SEO, User Interface
As developers we can create great software. Unfortunately, we usually introduce a few bugs along the way. Using a testing tool can ensure we catch the bugs and resolve them quickly. There are many different approaches to testing. Some advocate writing the tests within the application, others suggest a separate testing environment entirely. There are merits to both these approaches. For this blog post, I’ve focused our attention on Selenium as both Ext JS and Ext GWT and benifit from this “black box” testing methodology. Selenium provides a powerful mechanism to test your Ext applications. Selenium works by executing tests against your running application within the browser of your choice. Selenium tests emulate the way a user would interact with your application by executing JavaScript to simulate user actions. Selenium tests run as a form of “integration” tests as they execute against your running application. Both Ext JS and Ext GWT applications can benefit from Selenium tests. In fact, with few exceptions, the tests created for one product should be interchangeable as both products produce the same DOM structure. With GXT applications, GWT provides built in JUnit support. This provides a great way to test your application. However, these tests run only in host mode. Being able to test your compiled application in multiple browsers is important as some issues only appear within your compiled application. In general, you create Selenium tests and then execute them in a variety of ways. This tutorial will demonstrate creating tests with Selenium IDE, a Firefox plugin, and creating tests within Java. Tests will be loaded and executed within the Selenium IDE, and Java JUnit tests will be executed using Selenium Remote Control.
Selenium IDE is a Firefox extension that allows you to create, edit, and execute your tests. Tests can either be created manually, or by “recording” your actions. Recording can help you get a feel on how the Selenium commands are generated, but in most cases you will want to tweak the generated commands. A Selenium test is a list of commands. Commands can be seen as actions, such as “click this element”, “type into this field”, and “assert an element exists”.
First, you will need to install the plugin which can be found here. Once installed you can choose or from the Firefox application menu.
Spend some time playing around with Selenium IDE. You can use the record button (red circle) to have the tool record your actions or you can enter commands manually. Most commands require a locator string. The locater string is repsonsible for identifying elements. There are various “types” of locator strings which are covered later. Using Selenium IDE you can save and load tests. Also, notice the source tab. This tab allows you to view the test source. Selenium tests are saved as HTML files and can be exported to multiple server-side languages PHP, Ruby, Java, C#, Perl, and Python.
In this example, we will be testing both an Ext JS and Ext GWT form and it’s fields. First, let us take a look at the example code we will be testing. You can find the source code here:
Rather than creating a test from scratch, we will load an existing test file (listed above). Download this either the Ext JS or Ext GWT file to your file system. Then open the test in the Selenium IDE by selecting . Once loaded, Selenium IDE should look this this: Notice the list of commands. Take a look at each command to get a feel of what the test is doing. If you opened Selenium IDE as seperate window, close it. Then open Selenium IDE using . This will place selenium in the sidebar making it easier to run and monitor tests.
You can execute the test by clicking the first icon with the green arrow in the tool bar. Notice that you can control execution speed, use break points, execute individual commands, etc. After running your test, you screen should look like this:

Notice the form fields have been filled out. Examine the commands closely to see what actions were taken and what assertions where made. This test only touches the surface of things you can accomplish with commands. Note: When running the Ext JS test file, the radios and check boxes will not show the checked state, however, the true state will be correct, and the tests will run successfully.
Many Selenium commands require a locator to be specified. A locator is a way to identify an element in the page (the DOM to be specific). There are various types of locators including id, name, dom, xpath, link, and css. For this tutorial, XPath expressions where used. XPath expressions provide a powerful mechanism to identify elements. Example expressions look like this:
//input[@name='name'] |
//input[@name='name' and contains(@class, 'x-form-invalid')] |
//input[@name='company']/following-sibling::img |
//div[@id='Apple'] |
//div[@class='x-combo-list-item'][3] |
Here are a few examples using CSS selectors:
css=html:root |
css=div#structuralPseudo :nth-last-child(2) |
css=a[class~="class2"] |
Selenium IDE provides a great way to create your tests and execute them in Firefox. Tests can only be run by manually opening Firefox and executing tests. What if you want to run you want to automate your tests and run them in other browsers? This is where Selenium Remote Control comes into play. From the Selenium website: “Selenium Remote Control (RC) is a test tool that allows you to write automated web application UI tests in any programming language against any HTTP website using any mainstream JavaScript-enabled browser. Selenium RC comes in two parts. 1. A server which automatically launches and kills browsers, and acts as a HTTP proxy for web requests from them. 2. Client libraries for your favorite computer language.” 
In addition to Selenium IDE, Selenium RC must be downloaded and can be found here. After downloading, unzip the file to your file system.
The first step in using Selenium is to start the server. The server is responsible for executing your tests. The server will open browser instances, run tests, and close browsers. The server is written in Java and can be started by executing this command:
java -jar selenium-server.jar
The command should be executed from the folder where the selenium-server.jar is located. To run your tests in Java, you must reference the Java client library. See selenium-java-client-driver.jar in the Selenium RC download.
Selenium RC supports various languages, we will focus on Java. Rather than creating the Java test from scratch, Selenium IDE can be used to create the base Java code needed to create the test. From Selenium IDE, select the source tab, then choose Options / Format / Java. You will see the test is converted form HTML to Java. You can then copy and past the Java code to be used in your Java test. You can use the same steps to create code in other languages such as PHP and Ruby. Here is the Java code that was created using the template code from Selenium IDE:
public class SeleniumTestCase extends SeleneseTestCase { private Selenium selenium; public void setUp() { selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://extjs.com"); selenium.start(); } public void testForm() { selenium.open("http://extjs.com/playpen/gxt/selenium/"); pause(500); selenium.type("//input[@name='name']", "John"); selenium.fireEvent("//input[@name='name']", "blur"); assertTrue(selenium.isElementPresent("//input[@name='name' and contains(@class, 'x-form-invalid')]")); selenium.type("//input[@name='name']", "Darrell"); selenium.fireEvent("//input[@name='name']", "blur"); assertFalse(selenium.isElementPresent("//input[@name='name' and contains(@class, 'x-form-invalid')]")); selenium.type("//input[@name='email']", "darrell@foo.com"); assertEquals("Darrell", selenium.getValue("//input[@name='name']")); assertEquals("darrell@foo.com", selenium.getValue("//input[@name='email']")); selenium.focus("//input[@name='company']"); selenium.click("//input[@name='company']/following-sibling::img"); assertEquals("43", selenium.getXpathCount("//div[@class='x-combo-list-item']")); selenium.click("//div[@id='Apple']"); assertEquals("Apple Inc.", selenium.getValue("//input[@name='company']")); selenium.click("//input[@name='birthday']/following-sibling::img"); selenium.click("//button[contains(text(), \"Today\")]"); assertTrue(selenium.getValue("//input[@name='birthday']").matches("^[\\s\\S]*$")); selenium.check("//input[@value='Classical']"); assertEquals("on", selenium.getValue("//input[@value='Classical']")); selenium.check("//input[@value='Blue']"); assertEquals("on", selenium.getValue("//input[@value='Blue']")); } public void tearDown() { selenium.stop(); } }
Notice the use of “pause” after opening the application. This is needed to allow the GWT application to load. Other than that, the test code is a direct paste from the code generated in Selenium IDE. Notice that the test is using “*explorer” to execute the test. Firefox, Opera, Safari, Chrome are also supported. Keep in mind that the browser must be installed on the same maching where the Selnium server is running. The new JUnit test can be executed just like any other JUnit test. The test can be executed with Eclipse. 
Although it is not used in the tutorial, Selenium Grid is worth mentioning. From the website: “Based on the excellent Selenium web testing tool, Selenium Grid allows you to run multiple instances of Selenium Remote Control in parallel. Even better, it makes all these Selenium Remote Controls appear as a single one, so your tests do not have to worry about the actual infrastructure. Selenium Grid cuts down on the time required to run a Selenium test suite to a fraction of the time that a single instance of Selenium instance would take to run.”
The tutorial demonstrated how load and run tests within Selenium IDE. The test code was ported to Java where it was run using Selenium Remote Control in a JUnit. This tutorial demonstrated the major moving parts involved in Selenium tests. It is recommended to take a look at the Selenium documentation and tutorials for more information. Many thanks go out to the Selenium Team. There is a slight learning curve getting familiar with the product but I found it straightforward. The time was well spent as it opens up a new approach to testing web applications.
Recent Comments