# Selenium - a free tool for test automation

Each tester in his profession during work with large projects meets with concepts such as functional testing and regression testing. Each new functionality increase of the application also increases the area that requires constant monitoring and supervision by the tester. One of the methods to assist in such situations can be automated testing.

In the following article, I will present one of the free solutions for test automation that you can use in testing web applications. The tool is available for free and constantly improved.

Selenium is a toolset for automation testing of web applications. One of the main features of the system is to work with various programming languages and testing frameworks. Depending on our needs system allows us to select the specific tool depending on the objectives.

If you need tests which will help you in bug reproduction or you just need a simple set of smoke tests to check the basic features of the system, you should decide on the Selenium IDE.

## Selenium IDE

Selenium IDE is a plugin for Firefox which allows you to record and playback user interaction with the browser. One of the important features of Selenium IDE is a possibility to add additional components for an increase of the potential of the tool.

Deciding to install Selenium IDE you should think about additional plug-ins such as:

• Test Results – saving tests results
• Selenium IDE Flow Control - adding a flow control to tests script
• File Logging - record tests logs

Other interesting extensions:

• ScreenShot on Fail
• Selenium Expert
• Highlight Elements
• Selenium IDE: Java formatters

source of the extensions - blog.reallysimplethoughts.com

## Selenium RC (Selenium 1)

However, if you intend to write more extensive regression testing, or want to run your tests on different environments you should decide to use the Selenium Remote Control or WebDriver API.

Selenium Remote Control allows you to write and run tests on different browsers and environments. Selenium RC consists of two components. The first is the Selenium Server that runs all the premade tests. The second component is client libraries which offering an interface between a programming language and the Selenium RC Server services.

## Selenium + WebDriver (Selenium 2)

A completely different approach is characterized by a solution which is a combination of Selenium and external library WebDriver.

Selenium WebDriver is the external standalone library *.jar, which combines a driver for the browser and the Selenium server. WebDriver offers the API which is allowing you to interact with the browser. The supplied interface is minimalist and very simple to use.

Selenium 2 is the result of work with Selenium and WebDriver library. More information about the WebDriver will appears in the section getting started with Selenium RC and WebDriver.

## Simple usage of Selenium IDE

As you already know Selenium IDE is an extension for Firefox that enables the efficient creation of test cases. Using Selenium IDE, we have access to such things as:

• automatic generation of assertions
• export test scripts to HTML or other programming languages
• creation and management of a test set (test suite)

After installing the plugin and restarting the browser the default action when you run the Selenium IDE is recording user actions. From this point, all activities performed in the browser will be recorded and presented in the form of commands (right panel application).

Most of the instructions that appear in the Selenium IDE needs to define additional attributes.

These attributes can be replenished automatically while you work with your browser or can be defined manually.

Examples of attributes include:

Target - the target element to which the earlier caused the command (Command) and

Value - the value for the command. This could be the text that we introduce to the previously specified field.

Example of Selenium IDE window with uploaded test set

There are three types of commands that can appear in Selenium right pane: Actions, Accessors and Assertions.

Actions - appear when the application state changes, eg. open a new link, or select a new option. If after the interaction we have to wait for next test step the body of the command can containing AndWait phrase which means that Selenium will wait for loading a specific page or state.

Accessor - responsible for the examination of the application or store the results in variables, eg. StoreEval for writing the variables that are numerical values.

Assertions - are used to verify the status of the application is consistent with the expected state for example VerrifyText. The object states we can check using the commands with prefix assert or verify. The difference in application is when the assertion fails the test is interrupted, however, when it fails to verify the test will continue, but the error message will be recorded in the Selenium log.

Another kind of commands included in the assertion and associated with the result of the instructions is commands which contain WaitFor prefix. In this case, the user waits until the condition is satisfied, if not, an error message appears and the test will be immediately terminated. You can define the time frame for all Selenium IDE commands with Wait prefix by using SetTimeout instruction.

Depending on our needs at the time we can easily build any test set by adding and pulling each test case in the left panel of the Selenium IDE. If we want to use some test set very often (for example smoke test plan) we can save our work as a new Test Suite.

## Getting started with Selenium RC and WebDriver

Making a test suites is also necessary when we running tests in other browsers. To run the test in a different browser we use for example the Selenium RC. Each test run is starting by running Selenium Server with the several parameters.

-htmlSuite browser_name browser_page test_suite_name output_log_name


As you seen in this case, the four parameters described below.

browser_name - eg. "*firefox"
test_suite_name - eg. "c:\absolute\path\to\my\HTMLSuite.html"
output_log_name - eg. "c:\absolute\path\to\my\results.html"


If we already have some test set, and we want to examine it on various browsers it is very helpful to prepare for example a batch file that will contain full command of starting the test on selenium RC.

Diagram represents the process of running tests created by Selenium IDE under the control of any browser using Selenium RC (see two constituent components of Selenium RC).

If you are going to do something more than just record and play simple test cases, you can export your Selenium tests to any programming language, and then modify it.

There are two differents ways to export the tests in Selenium IDE. First is export and work under Selenium 1 framework. To do that choose 'File - Export Test Case to Java/JUnit4/Remote Control' under Selenium IDE window. Another way is to decide to work with WebDriver API (Selenium 2). Use export test to 'Java/JUnit4/WebDriver' to make sure your tests will run under WebDriver.

Selenium IDE allows you to export tests for many programming languages and works with many testing frameworks.

Below you will find a comparison of the sample test recorded in Selenium IDE and then exported for Selenium RC and another for WebDriver.

## Java/ JUnit4/ Remote Control (SELENIUM RC)

selenium.setTimeout(„60000″);
selenium.click(„id=reset_route”);
selenium.click(„css=#win_confirmResetRoute > div.btnsBg > div.btns > a.btn.green > div”);
selenium.click(„id=new_middle_point”);
selenium.setSpeed(„500″);
selenium.click(„css=div.rp_point_name”);
selenium.type(„id=rp_point_name0″, „Budapeszt”);
selenium.keyDown(„id=rp_point_name0″, „\\9″);
selenium.type(„id=rp_point_name1″, „Ljubljana”);
selenium.keyDown(„id=rp_point_name1″, „\\9″);
selenium.type(„id=rp_point_name2″, „Monachium”);
selenium.keyDown(„id=rp_point_name2″, „\\13″);
for (int second = 0;; second++) {
if (second >= 60) fail(„timeout”);


## Java/ JUnit4/ WebDriver (WEBDRIVER)

// ERROR: Caught exception [ERROR: Unsupported command [setTimeout | 60000 | ]]
driver.findElement(By.id(„reset_route”)).click();
driver.findElement(By.cssSelector(„#win_confirmResetRoute > div.btnsBg > div.btns > a.btn.green > div”)).click();
driver.findElement(By.id(„new_middle_point”)).click();
// ERROR: Caught exception [ERROR: Unsupported command [setSpeed | 500 | ]]
driver.findElement(By.cssSelector(„div.rp_point_name”)).click();
driver.findElement(By.id(„rp_point_name0″)).clear();
driver.findElement(By.id(„rp_point_name0″)).sendKeys(„Budapeszt”);
// ERROR: Caught exception [ERROR: Unsupported command [keyDown | id=rp_point_name0 | \9]]
driver.findElement(By.id(„rp_point_name1″)).clear();
driver.findElement(By.id(„rp_point_name1″)).sendKeys(„Ljubljana”);
// ERROR: Caught exception [ERROR: Unsupported command [keyDown | id=rp_point_name1 | \9]]
driver.findElement(By.id(„rp_point_name2″)).clear();
driver.findElement(By.id(„rp_point_name2″)).sendKeys(„Monachium”);
// ERROR: Caught exception [ERROR: Unsupported command [keyDown | id=rp_point_name2 | \13]]
// ERROR: Caught exception [Error: locator strategy either id or name must be specified explicitly.]
for (int second = 0;; second++) {
if (second >= 60) fail(„timeout”);
try { if (driver.findElement(By.id(„rp_alternative”)).isDisplayed()) break; } catch (Exception e) {}


As you seen after export to WebDriver you might sometimes need to improve the export script because there may be errors. WebDriver does not recognize some commands from Selenium IDE.

## Locating elements (Selenium IDE / WebDriver)

One of the most important thing in both of Selenium and WebDriver library is locating elements. You could say that the key to the successful creation of the script is the right selection of the proper components from the tested functionality.

When working with Selenium IDE most of the elements are automatically localized after user interaction in the browser. However, there is a possibility that some of our recorded actions will not work when we start playback the test. In that case, we must found locators manually.

Path of locator we can find in two ways. The first is to register the activity and then in the right pane of the Selenium IDE choice one from available locations identified by the system from the menu 'Target'

Finding the best locator from Target menu in Selenium IDE

The second option is more time-consuming method is to use the Firebug plugin in order to accurately search element on the page. In this case, we have access to most of the information about the item because we have direct insight into the application code on Firebug window.

Selenium IDE allows to locate items using:

• Id
• through a CSS style sheet
• through XPath
• through text or part of the text in the hyperlink
• through class name

## WebDriver

If you don’t use the Selenium IDE extension, location elements under WebDriver is available by using instance WebDriver or WebElement. The locator is an instance of the object ‘By’.

WebElement element = driver.findElement (By.name ("TestInput"));
element.sendKeys ("Test");


WebDriver offers us the following methods of search elements on the page:

• based on a unique identifier (ID)
• a specific CSS class
• CSS3 selectors
• the name of an HTML element
• the name of the attribute name
• text or fragment in a given element

The two main interfaces that are most commonly used when working with WebDriver is WebDriver and WebElement.

WebElement allows you to interact with the elements available on the page, among other things, it allows us to click on the selected item, enter a value in a text box, send the form contents or check any of the attributes of the HTML element.

WebDriver interface allows you to perform common tasks for your browser, such as loading the page, switch between any of the windows, refresh the page content, or retrieve data stored in cookies.

## Summary

As I previously mentioned Selenium provides us a tools set with different modules. Every solution has some limitations in creating automated tests. Selenium IDE its only a plugin for Firefox which allows you to quickly and easily write test cases. But when we put in together with Selenium RC or WebDriver it can be a powerful tool for automating web applications.

• free and extremely lightweight tool
• the ability to run tests in different environments
• easy of use (Selenium IDE)
• allows you to export the recorded test for different programming languages
• the possibility of expansion with new features and tools (Selenium IDE)
• good documentation
• good social support
• possible integration with systems such as TargetProcess and Jenkins