• Jobs
  • About
  • Getting started with Selenium Remote Control (RC) February 24, 2010

    UPDATE (Dec 2011): This post was written for Selenium 1.x. If you are looking for similar information in Selenium 2 – WebDriver, check out the Selenium 2 User Guide


    Introduction

    As mentioned in the Introduction to Selenium post, Selenium is a great test automation tool for driving browsers for testing web applications. This post describes:

    • Downloading and setting up Selenium RC
    • Starting and stopping selenium server using ant.
    • Writing your first test in Java.

    The assumption here is that you have an environment setup similar to the one defined in the Creating your Java project workspace post.

    Downloading and setting up Selenium RC

    At the time of this update to the post, the latest release of Selenium RC is version 1.0.3. Follow these steps to add Selenium in your project.

    1. Download Selenium RC 1.0.3 zip file in your download directory … on unix lets assume its located at ~/Downloads
    2. Unzip the file by doing
      unzip -d selenium-rc-1.0.3 selenium-remote-control-1.0.3.zip
    3. Copy ~/Downloads/selenium-rc-1.0.3/selenium-server-1.0.3/selenium-server.jar and ~/Downloads/selenium-rc-1.0.3/selenium-java-client-driver-1.0.1/selenium-java-client-driver.jar to your project lib (testautomation/lib/.) directory.

    Before you begin programming, you should ensure your selenium server is setup correctly. You can do this by following these instructions:

    1. Run Selenium server in interactive mode. To do this run the command:
      java -jar selenium-server.jar -interactive -singleWindow
      Once the server starts in the interactive mode you will see a line that says:
      Entering interactive mode ... type selenium commands here
    2. Now you can start a new selenium browser session using the command:
      cmd=getNewBrowserSession&1=*firefox&2=http://www.google.com
      This should open a new blank Firefox browser window.
    3. Now run the following command:
      cmd=open&1=http://www.google.com
      If the browser starts and you see Google’s home page in it, your basic setup seems to be fine.
    4. Type quit to exit the interactive mode.

    Starting and stopping selenium server using ant

    Lets now setup ant to start and stop our selenium server. Edit your build.xml file and add the following:

        <target name="start-selenium" description="starts selenium server">
            <echo>Starting selenium server</echo>
            <java jar="${lib.dir}/selenium-server.jar" fork="true" spawn="true" classpathref="testautomation.classpath">
                <arg line="-port ${selenium.port} -singleWindow -trustAllSSLcertificates"/>
            </java>
        </target>
    
        <target name="stop-selenium" description="stop selenium server">
            <echo>Stopping selenium server</echo>
            <get taskname="selenium-shutdown" dest="result.txt" ignoreerrors="true"
                 src="http://localhost:${selenium.port}/selenium-server/driver/?cmd=shutDownSeleniumServer"/>
        </target>
    

    You can now start selenium using ant start-selenium and ant stop-selenium to stop the server.

    Writing your first test in Java

    Create a class org.qaautomation.examples.GoogleSeleniumTest.java

    package net.qaautomation.examples;
    
    import com.thoughtworks.selenium.HttpCommandProcessor;
    import com.thoughtworks.selenium.Selenium;
    import com.thoughtworks.selenium.DefaultSelenium;
    
    public class GoogleSeleniumTest {
        static Selenium browser;
    
        public static void main(String[] arguments) {
            HttpCommandProcessor cmdProcessor =
                    new HttpCommandProcessor("localhost", 4444, "*firefox", "http://www.google.com");
            browser = new DefaultSelenium(cmdProcessor);
            browser.start();
            browser.open("/");
    
            boolean result;
            try {
                result = searchTest();
            } catch(Exception e) {
                e.printStackTrace();
                result = false;
            } finally {
                browser.stop();
            }
    
            System.out.println("Test " + (result? "passed." : "failed."));
            if (!result) {
                System.exit(1);
            }
        }
    
        private static boolean searchTest() {
            browser.type("q", "qa automation");
            browser.mouseDown("btnG");
            browser.mouseUp("btnG");
            browser.waitForCondition("selenium.browserbot.getCurrentWindow().document.getElementById('body') === null", "30000");
    
            // sleep so that you get time to see the effect of above steps
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                //
            }
            return browser.isTextPresent("About * results");
        }
    }
    

    Do
    ant build
    and
    ant run-example -Dexample=GoogleSeleniumTest
    to run the example.

    Next Steps

    Now that you have the basic setup, the next things you should consider setting up are:

    Posted by Rahul Poonekar in : Selenium

    5 responses to “Getting started with Selenium Remote Control (RC)”

    1. saimadhu says:

      How to reuse the existing browser session, while opening a new browser instance?
      or
      How to create tab using selenium?

      This is required to automate my Single sign on (SSO) tests?

      Please share your experiences.

    2. Deepa Kiran says:

      Hi,
      I have apache log4j installed and in that build.xml file, i have pasted the script given by you to start and stop selenium server. whats the next step? how to configure this build.xml to project library in eclipse? simply running the google search example code does not start the server. I might be missing some step to integrate ant’s build.xml with selenium project
      Please help me in solving the issue

    3. Srishti says:

      Hi,
      In my test case, I have to close a tab and then open a new tab in the same test case. Can you please tell me how to do it?

    4. Rahul Poonekar says:

      Instead of closing a tab and opening a new one, could you simple close a browser session and create a new one i.e. using browser.stop() and browser.start(), browser.open()? This ensures that session data is not transferred from one portion of the test to another.

      In some cases, it might be desirable to transfer the session though. If that is the case here, after you have done all the test steps that you would do in the first tab, then instead of opening a new tab, reuse the same tab/window by simply navigating to the other url using browser.open(url2).

    5. Suresh Kumar says:

      Could you please explain how to validate a login page ?

    Leave a Reply

    Your email address will not be published. Required fields are marked *