
16 www.agilerecord.com
6WHSVWRHIÀFLHQW*8,
test automation using
Selenium RC with Java
by Lars Trachsler and Ulrich Freyer-Hirtz
Abstract
Selenium is a popular open source framework for GUI test auto-
mation of web applications. With its capture-replay mechanism
it lets us get quickly started with GUI test automation. With Se-
lenium RC we get the power to script our tests in a language of
our choice. As with all GUI test automation tools, it is just a tool
that needs be used intelligently. Writing automated tests is pro-
gramming, so the same rules are valid for tests and code to write
UREXVWÁH[LEOHUHXVDEOH DQG PHDQLQJIXO FRGH 'XULQJ ZRUN LQ
VHYHUDOSURMHFWVZH LQGHQWLÀHGUXOHVIRUZULWLQJSRZHUIXOWHVW
automation suites. In this article, we explain how we implement-
ed them with Selenium RC, Java and jUnit, and how they helped
us to minimize our effort for monkey testing, generating time for
more complex test cases.
Introduction
One part of the Selenium Suite is Selenium IDE, which provides
capture-replay functionality in a simple, easy-to-use GUI. It is
implemented as a Firefox plugin. The capture-replay mechanism
allows a fast start into test automation with quick wins. Using
Selenium we can capture scripts alongside the usual manual or
exploratory testing. So what’s the problem? As we will see, these
VFULSWV DUH QRW ÁH[LEOH DQG PDLQWDLQDEOH )RU H[DPSOH DGGLQJ
test cases or switching environments has to be realized using the
copy & paste pattern, with all its problems. Changing the GUI is
a continuous threat (or “a bitch of its own”, as a colleague once
said), which we can’t get under control using capture and replay.
Help comes with Selenium RC, another part of the Selenium
Suite, which allows scripting and enables us to use all the tech-
niques of an object-oriented programming language. Selenium
RC “just” offers automated execution, cross-browser testing and
the integration of scripts into a programming language. It doesn’t
provide any restrictions how to use these features.
7RNHHSWKHWHVWVÁH[LEOHPDLQWDLQDEOHUREXVWDQGUHXVDEOH
ZHKDYHLGHQWLÀHGGHVLJQUXOHV
1. Manage a central repository for the GUI element IDs.
2. Manage environment variables at a central place.
3. Build functional methods with “speaking” names.
4. Separate data from scripts.
5. Use a model for your test objects.
6. Establish architecture.
7. Generate readable test reports.
These rules and the implementations described in this article are
the outcome of working with Selenium/Java on several projects, and
they have been proven in these projects. The rules help you write
the test right. To do this follow the rule to automate only the “stupid”
cases to free up more time for complex testing. The risk is of becom-
ing infected by writing automated tests, which may lead you to try to
automate everything, just because it’s possible. When writing the
automated tests hinders your testing, something has gone wrong.
Manage a central repository for the GUI element IDs
To understand the problem lets look at a captured script. The fol-
lowing test shows how Selenium IDE looks:
Figure 1: The script goes to a page, clicks some links and does
two searches by typing a search string and then clicks “Go”.
)URPWKLV,'(ZHFDQH[SRUWWKHWHVWLQWRVRPHVSHFLÀFSURJUDP-
ming languages. We choose the export into a Java-jUnit test case.
The script now looks like this:
selenium.click("topnav6");
selenium.type("//input[@name='search']", name);
selenium.click("//img[@alt='GO Search']");
7KLVIRUPDWFRQWDLQVWZRSUREOHPV
1 Unreadable names - which link is “topnav6”?
2 Reusing these scriplets by using copy and paste distributes
© Mikhail Surkov - Fotolia.com