Bit Motif

A Zero Here, A One There

Pages

About

Photos

Test Page For Selenium Remote Control

Archives

July 2009

M | T | W | T | F | S | S
« Feb | |
| 1 | 2 | 3 | 4 | 5
6 | 7 | 8 | 9 | 10 | 11 | 12
13 | 14 | 15 | 16 | 17 | 18 | 19
20 | 21 | 22 | 23 | 24 | 25 | 26
27 | 28 | 29 | 30 | 31 |

Categories

agile (6)

development (12)

Java (6)

Selenium (4)

Uncategorized (2)

Word Press (3)

Blogs Worth Reading

Dave Thomas

Gojko Adzic

Jay Fields

Jeremy Miller

Martin Fowler

Nat Pryce

Steve Yegge

Uncle Bob

Meta

Login

Valid XHTML

XFN

WordPress

Search

« WordPress Permalinks

Selenium Remote Control For Java — A Tutorial (part 2) »

Selenium Remote Control For Java — A Tutorial

The coolest element nowadays is Selenium. You can even control it remotely.

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 called—what 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, we’ll need to get the jars. With the jars, we’ll get the server and the client we’ll 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 isn’t all that useful. We need to interact with the page. To do this, we use our selenium client’s 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. We’ll explore other actions (methods) and locator types later.

The last step is to integrate the interaction and checking in a test. In this case, we’ll 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 com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.DefaultSelenium;
import junit.framework.TestCase;
import 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.

This entry was posted on Sunday, March 18th, 2007 at 5:25 pm and is filed under Selenium, Java. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

12 Responses to “ Selenium Remote Control For Java — A Tutorial ”

P1F Says:
January 9th, 200811:23 am at
Why nobody says - where to place those java tests?
I get this file - and what next? I just can’t start it with “javac …” and “java …” (
Disappointed.
And what about using selenium-java-client-driver.jar? Does it REALLY required for the selenium java tests?

pjberry Says:
January 11th, 20087:22 am at
@P1F:

Regarding the selenium-java-client-driver.jar, it is need. As the name states, it’s where we get the client that talks to the selenium server.

Where to put the file, how to compile it, and run it are a really outside of what I wanted to talk about. You should be able to put the file in your favorite IDE or do it all at the command line if you want. Be sure to note the the package structure in the example (com.bitmotif.seleniumexamples).

Claucia Says:
April 3rd, 200810:09 am at
Olá gostaria de saber como resolver o seguinte problema:

java.lang.RuntimeException: Could not contact Selenium Server; have you started it?
Catch body broken: IOException from cmd=getNewBrowserSession&1=*firefox&2=http%3A%2F%2Fwww.google.com -> java.net.ConnectException: Connection refused: connect
at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:70)
at triton.testeSelenium.PrimeiroTeste.setUp(Unknown Source)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

pois estou tentando criar o teste no eclipse numa classe java e não consigo executar o teste.

Por favor, me ajudem.

perogi Says:
April 3rd, 20081:43 pm at
P1F,

I would recommend using an IDE such as Eclipse and placing tests inside a test source folder. Using JUnit, you can run these tests in quite a few different ways. Any basic JUnit tutorial can help you with this.

pjberry,

Excellent article. Well done.
perogi.

Angelo Says:
June 17th, 200812:44 am at
thanks for this tutorial… I’m new in selenium and i want to explore more on it… keep up the good work…

Ankush Says:
July 2nd, 20089:17 am at
It’s a good tutorial, good easy language and good subject ( relevant )..
However how do you write the long testing code for sites, i mean manually coding selenium.click(link), verify…etc. So do you always open the website, save the links and text to test and then code them in your code? Isn’t their a better way to do..

I try to record my tests using IDE, and then export them as Java classes where I can copy the selenium click/verify etc. commands into my java code, do you think that’s the best way?

Also do you have to create a new file for each test? If so, if you want to run them together (assume 10 classes, ExampleTest1, ExampleTest2,…,ExampleTest10 ), how do you do so ??

Thanks

Zuhaib Ahmed Says:
July 22nd, 20081:32 am at
Well its quite nicely described, Thumbs Up !!!! i would suggest you to please demonstrate some more scenarios with the help of examples showing regularly used features of Selenium.

Good Luck.

Anonymous Lee Says:
August 29th, 20084:30 am at
QUICK AND DIRTY for beginners:

Use selenium from jUnit Create a AllTests class that extends TestCase from jUnit framework.

Then make a wrapper that does the GLOBAL setup for all the tests (starts the browser).

Do not start the server from your java code if you run the remote control on another machine, else start it in your global setup

Organize your test cases in to test suites and feed the suites to the framewrok.

Note that you can do setUp() and tearDown() on different levels, such as GLOBAL (see example below), per test suite and per test case.

GLOBAL setup is nice when you are testing as a user that is “logged in” to your web app
(You do not want to stop / start a browser and login in and out if you are running a session where your “user” has to be logged in.)

##########################################################
## GLOBAL SET UP EXAMPLE

package com.alexandern.selenium;

import com.thoughtworks.selenium.Selenium;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

/**
* @author Alexander N
*/
public class AllTests extends TestCase {

public static Test suite() {

TestSuite suite = new TestSuite();

suite.addTestSuite(TestCaseOne.class); // your classes that extend jUnit TestCase
suite.addTestSuite(TestCaseTwo.class);
suite.addTestSuite(TestCaseThree.class);

TestSetup wrapper = new TestSetup(suite) {

protected void setUp() {
oneTimeSetUp();
}

private void oneTimeSetUp() {
Settings.loadProperties();
PrepareDatabase.blowAwayDatabase();
RC.initBrowser();
}

private void oneTimeTearDown() {
RC.stopBrowser();
}

protected void tearDown() {
oneTimeTearDown();
}
};

return wrapper;
}

public static void main(String arg[]) {

TestRunner.run(suite());
}
}

##########################################################
## a simple test case example

This test case is checking the menu that user see when
logged in to my web app. It clicks on admin menu links and
verifies that it got to the correct administration page.

You can extrapolate from that.

package com.tome.selenium.mytests

// custom RemoteController class stores objects used by all
// tests, err such as browser instance…
import com.tome.selenium.RC;

import com.thoughtworks.selenium.Selenium;
import junit.framework.TestCase;
import org.testng.annotations.BeforeTest;

public class TestAdminMenu extends TestCase {

Selenium selenium;

// MAKE SURE YOU NAME YOUR METHODS STARTING WITH WORD test
// like: testSomething if you want to use jUnit framework

@BeforeTest
public void setUp() {
// grab the browser you have started in the
// global setup and stored in the RC class you created
// see RC class below
selenium = RC.getBrowser();
}

public void testAdminResumeLink() {
selenium.click(”link=Resume”);
assertTrue(selenium.isTextPresent(”ES_RESUME”));
}

public void testAdminApplyChangesLink() {
selenium.click(”link=Apply Changes”);
assertTrue(selenium.isTextPresent(”Applying your changes…”));
}

public void testAdminSignOutLink() {
selenium.click(”link=Sign Out”);
assertTrue(selenium.isTextPresent(”Your session is closed”));
Conditions.setIsLoggedIn(false);
}
}

#############################################################
## RC (RemoteController) class, store stuff you will use in
## all your tests. Your browser instance is a good example.
## (Else you will be starting and stopping brwser for each
## test case

// custom settings class, read in some configs for your
// tests from a properties file blah blah blah use your
// imagination
import com.tome.selenium.setup.Settings;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class RC extends Settings {

private static Selenium browser;

public synchronized static void setBrowser(Selenium _browser) {
browser = _browser;
}

public synchronized static Selenium getBrowser() {
return browser;
}

ppublic synchronized static void stopBrowser() {
browser.stop();
}

public synchronized static void initBrowser() {

if (browser == null) {
browser = new DefaultSelenium(”localhost”, 4444, EXPLORER, APP_URL);
browser.start();
browser.setSpeed(EXECUTION_SPEED_NORMAL);
}
}
}

If you do not like jUnit (you must be mad! jUnit is awesome) you can jar up your
tests and just just all them from command line as any command line app.

Of cause if you see:
java.lang.RuntimeException: Could not contact Selenium Server; have you started it?
this means you have not started the selenium server.
you can start it from the command line: like this:

java -jar selenium-server.jar

(provided you have your JAVA_HOME and the rest of the stuff setup properly)

Cong Nguyen Says:
October 16th, 20084:06 am at
Hi all,
I am using selenium-rc,
No i want to config and starting selenium-java-client-driver. but i tried in many time but failed/
Please give me step by step if you had done it. Thanks

Anwarul Kabir Says:
November 6th, 20084:35 pm at
thanks for this tutorial… Can you put some examples using CSS.

Thanks in advance

Kevin St. Clair Says:
February 2nd, 20093:27 pm at
You might want to mention to your users that the JBoss server uses port 4444 for its its “JRMPInvoker” mbean. Anybody trying your steps on JBoss 4.2.3GA would have to adjust this port to avoid conlicting with the selenium-server port.

afam Says:
February 5th, 20092:58 pm at
Thanks a lot for the effort. I am familiar w/ selenium. Run into your blog while searching for the right xpath to click an image. thanks again. Folks like you make the world better

Leave a Reply

Name (required)

Mail (will not be published) (required)

Website

Bit Motif is proudly powered by WordPress
Entries (RSS) and Comments (RSS). Template : Jillij