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.

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

  1. P1F Says:

    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?

  2. pjberry Says:

    @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).

  3. Claucia Says:

    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.

  4. perogi Says:

    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.

  5. Angelo Says:

    thanks for this tutorial… I’m new in selenium and i want to explore more on it… keep up the good work…

  6. Ankush Says:

    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

  7. Zuhaib Ahmed Says:

    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.

  8. Anonymous Lee Says:

    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)

  9. Cong Nguyen Says:

    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

  10. Anwarul Kabir Says:

    thanks for this tutorial… Can you put some examples using CSS.

    Thanks in advance

Leave a Reply