Selenium Remote Control For Java - A Tutorial
Selenium Remote Control For Java - A Tutorial
Tutorial
What is Selenium?
Selenium is a testing tool for web applications that uses JavaScript and Iframes to run tests
directly in a browser. There are several test tools based on this technology: Selenium Core,
Selenium IDE, Selenium Remote Control, and Selenium on Rails.
The aforementioned Selenium Remote Control has become the tool I reach for when testing web
applications. In fact, it is so useful that I hardly use HttpUnit any more.
What is Selenium Remote Control?
It is the remote control for Selenium. Duh. Ok, it really is that. A better, less obnoxious
description is that Selenium Remote Control is a tool that allows us to test against the browser
using our programming language of choice (in this case, Java).
How does Selenium Remote Control Work?
Selenium Remote Control depends on a server calledwhat else?Selenium Server. The
Selenium Server is capable of manipulating supported browsers and acts as a client-configured
proxy between a browser and a website. This allows it to run JavaScript against a site.
How do I use Selenium Remote Control?
First, well need to get the jars. With the jars, well get the server and the client well use for
testing. (For this tutorial, the selenium-remote-control-0.9.1-SNAPSHOT nightly was used.) In
the folder structure of the download, there are two jars that we want: selenium-server.jar in the
server folder and selenium-java-client-driver.jar in the java folder.
We need to be able to start and stop the Selenium Server. We can do this at the command line:
Start:
java -jar selenium-server.jar
Stop:
http://localhost:4444/selenium-server/driver/?cmd=shutDown
To limit configuration and external dependencies, we can do this in our test code using the
SeleniumServer class:
SeleniumServer server = new SeleniumServer();
server.start();
...
server.stop();
browserType is the type of browser you want to use for testing. Common browser types
are *firefox, *iexplore, *opera.
baseURL is the base URL for the site you are testing. To adhere to the Same Origin
Policy , the Selenium object created is tied to that particular URL and can only be used
for that URL.
Now, just opening a page isnt all that useful. We need to interact with the page. To do this, we
use our selenium clients methods with locators. For example:
selnium.click("link=Text For Some Link");
The string link=Text For Some Link is a locator. Most of operations involve locators that tie a
Selenium command to elements in an HTML document.
Locators take the form of
locatorType=argument
A locator type can be an element id, an element name, an xpath expression, link text, and more.
A few examples:
selenium.click(id=idOfThing);
//an id locator
selenium.click(name=nameOfThing);
//a name locator
selenium.click(xpath=//img[@alt='The image alt text']); //an xpath locator
selenium.click(dom=document.images[56] );
//a DOM locator
selenium.click(link=Test Page For Selenium);
//a link locator
selenium.click(css=span#firstChild);
//a css locator
Of course, there is more to Selenium than just clicking. But, the above gives you an idea of the
types of locators, and how they are typically used with a method on the selenium object. Well
explore other actions (methods) and locator types later.
The last step is to integrate the interaction and checking in a test. In this case, well use JUnit to
check the results of clicking a link. Note that the code below actually refers to a real link and
page.
package com.bitmotif.seleniumexamples;
import
import
import
import
com.thoughtworks.selenium.Selenium;
com.thoughtworks.selenium.DefaultSelenium;
junit.framework.TestCase;
org.openqa.selenium.server.SeleniumServer;
The only thing new in the code above is the waitForPageToLoad method. Sometimes an action
such as clicking a link or submitting a form results in a page load. In order to make sure the new
page is loaded before we attempt to do anything else, we use this method.
With that, we now have an example of using Selenium Remote Control as part of a test.