0% found this document useful (0 votes)
68 views

Selenium Remote Control For Java - A Tutorial

Selenium Remote Control allows testing web applications through a browser using various programming languages like Java. It works by starting a Selenium Server that manipulates browsers and acts as a proxy between the browser and website. Tests can interact with pages by using locator strings with Selenium client methods to perform actions like clicking and checking page content. JUnit can integrate the interactions and assertions to form automated tests.

Uploaded by

shantana4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Selenium Remote Control For Java - A Tutorial

Selenium Remote Control allows testing web applications through a browser using various programming languages like Java. It works by starting a Selenium Server that manipulates browsers and acts as a proxy between the browser and website. Tests can interact with pages by using locator strings with Selenium client methods to perform actions like clicking and checking page content. JUnit can integrate the interactions and assertions to form automated tests.

Uploaded by

shantana4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Selenium Remote Control For Java A

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();

With the server running, we may now use a client:


Selenium selenium = new DefaultSelenium( String seleniumServerHost,
int seleniumServerPort,
String browserType,
String baseURL);
selenium.open("http://www.somesite.com/somePage.html");
selenium.stop();

An explanation of the variables in the DefaultSelenium constructor:

seleniumServerHost is the where the Selenium Server is running. Typically, it is


localhost.
seleniumServerPort is the port on which the Selenium Server is listening. The default is
4444.

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;

public class ExampleTest


extends TestCase
{
private static final String MAX_WAIT_TIME_IN_MS = "60000";
private static final String BASE_URL = "http://www.bitmotif.com";
private Selenium selenium = new DefaultSelenium( "localhost",
4444,
"*firefox",
BASE_URL);
private SeleniumServer seleniumServer;
public void setUp()
throws Exception
{
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}
public void tearDown()
throws Exception
{
selenium.stop();
seleniumServer.stop();
}
public void testClickingLink()
throws Exception
{
selenium.open(BASE_URL);
selenium.click("link=Test Page For Selenium Remote Control");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
String expectedTitle = "Bit Motif Test Page For Selenium Remote
Control";
assertEquals(expectedTitle, selenium.getTitle());
}
}

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.

You might also like