Professional Documents
Culture Documents
com/developerworks/library/wa-selenium-ajax/
Level: Intermediate
20 Dec 2005
Acceptance, or functional, testing is designed to put manual tasks through their paces, but testing
these tasks by hand can be time consuming and prone to human error. In this article, the author shows
architects, developers, and testers how to use the Selenium testing tools to automate acceptance tests;
automating the tests saves times and helps eliminate tester mistakes. You also are provided with an
example of how to apply Selenium in a real-world project using Ruby on Rails and Ajax.
Acceptance testing of Web applications usually involves manual tasks like opening a browser and performing
actions described in a test case. Manually performed tasks are prone to operator error and are time consuming. It is,
therefore, a good practice to automate these tasks whenever possible to remove the human factor. This is where test
tools like Selenium come into the picture. Selenium helps you automate your acceptance tests and allows you to
build better tested and, hence, more reliable and maintainable software.
Acceptance testing, also known as black-box and functional testing, is a way of testing and verifying that an
application works according to functional, non-functional, and other important stakeholder requirements.
Acceptance tests complement unit and integration tests, which are usually written using xUnit frameworks.
Acceptance tests can also be written using a programming language, but Selenium and similar tools like Fitnesse
also support tests written using a tool-specific document format.
Acceptance tests differ from unit and integration tests in the following ways:
The application is tested as a whole, end-to-end entity, not just one class or a set of classes as it is with unit
and integration tests.
Acceptance tests are performed against the user interface using, for example, a browser against a Web
application interface.
The person writing the test cases does not necessarily know about the internals of the application, hence the
designation of black-box testing. Non-technical users can write acceptance tests.
A bit of background
Before going further into Selenium, I'd like to offer some background on the following three topics, since they are
relevant to this article even though they're not the central theme:
Continuous integration
Ajax
Ruby/Ruby on Rails
Continuous integration
Continuous integration aims at automating the build and test processes so that they run automatically once or many
times a day instead of, say, manually once a month. The biggest benefit of using continuous integration is that code
changes are integrated automatically on a regular basis. And if the system is broken and does not build
successfully, continuous integration tools like Apache Continuum and Luntbuild can automatically notify the team
by sending an e-mail (see Resources).
Ajax
Ajax stands for Asynchronous JavaScript and XML; it is a newly coined term for relatively old technology. The
main idea behind Ajax is that the Web application responds more quickly to user actions because only parts of the
page need to be updated instead of the whole page.
1 of 10 2007-8-30 3:59
Automate acceptance tests with Selenium http://www-128.ibm.com/developerworks/library/wa-selenium-ajax/
Ajax introduces more complexity to Web applications that is also reflected in testing. This is because Ajax, as the
name implies, uses JavaScript and asynchronous HTTP requests to update the page contents. Each browser
contains small differences in implementation compared to others. Selenium makes a perfect tool for testing and
detecting these differences since it runs inside the most popular browsers.
Ruby/Ruby on Rails
Ruby is an open-source interpreted scripting language for quick and easy object-oriented programming; it features
a wide range of libraries and is simple, straight-forward, extensible, and portable. The language was created by
Yukihiro "Matz" Matsumoto and was designed to make programmers happy by allowing them to concentrate more
on the task at hand than syntax.
Rails is a full-stack, open source Ruby Web framework created by David Heinemeier Hansson. Rails is designed to
make writing real world applications less code-intensive and easier than J2EE and XML, for example. All the
layers are built to work seamlessly together so you can use a single language to write everything from templates to
control flow to business logic. Rails uses YAML instead of XML configuration files and reflection and runtime
extensions in favor of annotations. And there is no compilation phase -- you make a change and watch it work.
What is Selenium?
Selenium is an acceptance testing tool written by ThoughtWorks specifically for Web applications. The Selenium
home page says the following is the biggest benefit of using Selenium when compared with other test tools:
Selenium tests run directly in a browser, just as real users do. And they run in Internet Explorer,
Mozilla, and Firefox on Windows, Linux, and Macintosh. No other test tool covers such a wide array
of platforms.
Note that IBM's Rational Functional Tester tools perform these tests as well, but at a much more complex and
wide-reaching level, and come with IBM support. Selenium, on the other hand, prioritizes one task -- a Web
application -- and can perform with more agility.
Additional benefits of using Selenium and running tests inside a browser are many. Here are two main ones:
You test the application from the end user's perspective by writing Selenium test scripts that mimic user
actions.
You can identify browser incompatibilities more easily by running the tests in different browsers.
The core of Selenium, also referred to as the browser bot, is written in JavaScript. This allows test scripts to run
inside supported browsers. The browser bot is responsible for executing commands received from test scripts
which are written in either HTML using a table layout or a supported programming language.
Selenium commands
With Selenium commands, Selenium and the Rational Functional Tester
the script writer describes Companies or organizations with bigger projects typically need a standard product
the actions that the browser with many features and reliable support. All projects within the organisation then
bot is to perform in the
2 of 10 2007-8-30 3:59
Automate acceptance tests with Selenium http://www-128.ibm.com/developerworks/library/wa-selenium-ajax/
browser. You can split use this product. One solution is the Rational Functional Tester, rather than
these commands into two Selenium, which is purposely not standardized and is limited in its design to
groups -- actions and address much smaller tasks with ease.
assertions:
Rational Functional Tester is used with such applications as .NET and Java GUIs,
Actions simulate the
Siebel, Terminal-based applications, and, of course, Web applications. The Tester
user's interaction
contains thousands of features not available in Selenium, including Test Manager
with the Web
and ScriptAssure. It's integrated with version control and has tools and components
application. Clicking
that extend its use to many areas, and is used by all projects within the same
a button and filling
organization.
out a form are both
examples of Get more specs on this advanced test automation at
common user http://www3.software.ibm.com/ibmdl/pub/software/rational/web/datasheets/rft.pdf.
actions that you can
automate with
Selenium
commands.
Assertions verify the expected outcome of a command. Common assertions include verifying that the page
content or that the current location is correct.
You can find a complete list of available commands on the Selenium Web site (see Resources).
Selenium modes
You can use Selenium in two modes: test runner and driven. The two modes differ in complexity and the way they
are written. Driven test scripts tend to be more complex to write since they are written in a programming language.
The difference in complexity is minimal if you use a high-level dynamic programming language like Python or
Ruby.
The biggest difference between the two modes is that the tests are partly run from outside the browser if using
driven scripts in which test runner scripts are run completely inside the browser.
Both test runner and driven test cases can be integrated with continuous integration tools.
<table border="1">
<tr>
<td>First command</td>
<td>Target</td>
<td>Value</td>
</tr>
<tr>
<td>Second command</td>
<td>Target</td>
<td>Value</td>
</tr>
</table>
Test runner scripts are usually deployed on the same server as the application under test (AUT). This is because the
browser bot uses JavaScripts to emulate user actions. These scripts run in a restricted sandbox environment. If you
3 of 10 2007-8-30 3:59
Automate acceptance tests with Selenium http://www-128.ibm.com/developerworks/library/wa-selenium-ajax/
Test runner scripts use the same concepts of test suite and test case as xUnit frameworks. Both test cases and
commands execute sequentially in the order they appear in test suites and test cases. In Listing 1:
The first column contains the command or assertion.
The second column contains the target for the command or assertion. Specify the target with one of the
many supported element locators. Usually the ID or name of the element is used, but also XPath and DOM
locators are supported.
The third column contains the value which specifies parameters for commands or assertions. This can be the
desired value for a text field when using the type command.
Test runner scripts are easy to read and write even for non-technical people. The example in Listing 1 will, when
opened in a browser, look like this table:
<table>
<tr>
<td>open</td>
<td>/change_address_form.html</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>address_field</td>
<td>Betelgeuse state prison</td>
</tr>
<tr>
<td>clickAndWait</td>
<td>//input[@name='Submit']</td>
<td></td>
</tr>
<tr>
<td>verifyTextPresent</td>
<td>Address change successful</td>
<td></td>
</tr>
</table>
Test suites
Acquiring full test coverage of an application usually requires more than one test case. This is why Selenium uses
the concept of test suites. Test suites are used to group a set of test cases with similar functionality together so that
they can be run in sequential order.
Test suites are written in the same way as the test cases, using simple HTML tables. The default test suite executed
by Selenium is named TestSuite.html. Listing 3 shows a test suite that tests an application in the same way a
typical user would. Note that test suites use a single-column table and that each row points to a file containing a
test case.
4 of 10 2007-8-30 3:59
Automate acceptance tests with Selenium http://www-128.ibm.com/developerworks/library/wa-selenium-ajax/
<table>
<tr>
<td>Test suite for the whole application</td>
</tr>
<tr>
<td><a href="test_main_page.html">Access main page</a></td>
</tr>
<tr>
<td><a href="test_login.html">Login to application</a></td>
</tr>
<tr>
<td><a href="test_address_change.html">Change address</a></td>
</tr>
<tr>
<td><a href="test_logout.html">Logout from application</a></td>
</tr>
</table>
Driven mode
Driven Selenium scripts are written in one of the supported programming languages -- currently Java, Ruby, and
Python drivers are available. The scripts run in a separate process outside of the browser. The driver's task is to
execute the test script and drive the browser by communicating with the browser bot which runs inside the
browser. The communication between the driver and the browser bot uses a simple Selenium-specific wire
language called Selenese.
Driven scripts are more powerful and flexible than test runner scripts and you can integrate them with xUnit
frameworks. The downside to driven scripts (when compared with test runner scripts) is that they are more
complex to deploy and write. This is because the driver has to perform the following tasks:
Start the server.
Deploy application under test (AUT).
Deploy test scripts.
Start the browser.
Send commands to the browser bot.
Verify the result of commands executed by the browser bot.
Driven scripts are also more dependant on the application runtime environment. For example, the Java driver uses
an embedded Jetty or Tomcat instance to deploy the application under test. Currently, an effort is in progress to
integrate Selenium into Ruby on Rails, but at the time of writing it was not yet released.
Listing 4 is an excerpt from a driven test script that uses the Ruby driver. Note that I left out the steps for
instantiating the server and browser and that the test script code is almost as simple as a test runner script.
.
.
puts selenium.open('/logout.html')
puts selenium.verify_location('/index.html')
.
.
Real-life requirements
5 of 10 2007-8-30 3:59
Automate acceptance tests with Selenium http://www-128.ibm.com/developerworks/library/wa-selenium-ajax/
In the next two major sections (Real-life requirements and Real-world use cases), I describe how to use Selenium
in a real-world scenario and write Selenium test cases for a simple stock quotes viewer application using Ruby on
Rails and a bit of Ajax technology. Although the application is written using Ruby on Rails, you can apply the
example to any Web application since the test scripts are written in HTML for the test runner mode. The sample
application will probably work with older or later versions, but it was tested with Ruby 1.8.3 and Ruby on Rails
0.14.2.
Ruby is usually included in the distribution if you have Linux. Run ruby -v from a command prompt to check
what version you have. You can find a Ruby distribution for most platforms at http://www.ruby-lang.org/.
Your next step is to install Ruby on Rails through the RubyGems packaging system. To do this, simply execute
gem install rails --include-dependencies. On some platforms you have to perform some extra
steps, so check the Ruby on Rails Web site for more details.
The current Selenium version available, at the time of writing, is 0.6. I've already integrated it into the sample
application (see Download) by downloading the Selenium Core package from http://selenium.thoughtworks.com/
and copying the folder named selenium to the folder that serves static content. In Ruby on Rails applications, the
name of this folder public. In a J2EE Web application, you put it in the root of the Web application or WAR
archive file.
Your last step is to download the example application; access the package from Download. Extract the application
and open a command prompt. Then change directory to where you extracted the application. To start the
application, run ruby script/server. You should see that Rails started without errors as shown in Figure 1.
6 of 10 2007-8-30 3:59
Automate acceptance tests with Selenium http://www-128.ibm.com/developerworks/library/wa-selenium-ajax/
4. Enter password.
5. Press login button.
6. Verify that login was successful.
Figure 2 shows the Selenium test case for these requirements. Note that I took the screenshot after running the test.
The green rows indicate assertions that were successfully verified.
Note the pause commands: These are required to allow the asynchronous request to finish and update the page
content. Without the 500 millisecond pause, the test fails (shown in Figure 4).
7 of 10 2007-8-30 3:59
Automate acceptance tests with Selenium http://www-128.ibm.com/developerworks/library/wa-selenium-ajax/
The pause command also tests the non-functional requirements for the Ajax functionality. 500 milliseconds is a
good value for the pause command since Ajax requests should execute and finish quickly. You can try to remove
the pause commands and see what happens. If the tests fail on your machine, try to increase the value to 1000
milliseconds.
All tests are added to the default test suite which is shown to the left of Figure 6.
You can also walk or step through the test suite which means Selenium will execute the test suite slowly enough
for you to see each step of the test suite as it executes in the browser.
Conclusion
Selenium is a useful and important addition to the toolbox of software engineers, designers, and testers. Together
8 of 10 2007-8-30 3:59
Automate acceptance tests with Selenium http://www-128.ibm.com/developerworks/library/wa-selenium-ajax/
with a continuous integration tool, it allows teams to automate acceptance tests and build better software as they
find bugs easier, earlier, and more often. An added benefit of Selenium is that it saves time and allows the team to
concentrate on more valuable activities by freeing developers and testers from manual tasks which can and should
be automated.
Download
Description Name Size Download method
Sample application wa-selenium-ajax-example.zip 286KB HTTP
Resources
Learn
Review a complete list of available Selenium commands.
Discover how a Selenium integration into Ruby on Rails looks in this brainstorming session.
Review the archived Diagnosing Java code series (developerWorks, 2001) includes automating testing tasks
as a matter of best programming and debugging practices.
Explore all Ruby and Ruby on Rails articles at developerWorks, including these gems:
The tutorial Sockets programming in Ruby teaches Ruby basics plus the most important classes to
develop sockets-based networking applications using the Ruby language (October 2005).
Fast-track your Web apps with Ruby on Rails (June 2005) describes a framework for rapid
development using an MVC pattern.
Ruby on Rails and J2EE: Is there room for both? (July 2005) compares Ruby on Rails and J2EE.
Get the white paper on Advancing Toward Test Automation through Effective Manual Testing as well as
the specifications for the Rational Functional Tester.
9 of 10 2007-8-30 3:59
Automate acceptance tests with Selenium http://www-128.ibm.com/developerworks/library/wa-selenium-ajax/
Visit the Ruby on Rails homepage and the Ruby language homepage for everything you need to get up to
speed with these technologies.
Check out Continuum, a continuous integration server for building Java-based projects.
Explore Luntbuild, a powerful build automation and management tool designed to automate builds and act
as a build management system.
Get Selenium Recorder, a Firefox extension that allows you to record Selenium test scripts.
Try out the Rational Functional Tester with this free 30-day trial download.
Discuss
Participate in the discussion forum.
10 of 10 2007-8-30 3:59