First Selenium application in Java

The following shows you step-by-step how to run your first Selenium RC application in Java.

  1. It is assumed that you have JDK installed and set in your computer. If you are getting the version number(e.g javac 1.6.0_06) by running the following command in the Command Prompt, then your Java Compiler is installed and set correctly.
    javac -version
  2. Download Selenium RC.
  3. Decompress Selenium RC. In my case, I decompress it to C:\.
  4. From the Command Prompt, go to the directory where selenium-server.jar is located.
    cd C:\selenium-remote-control-1.0-beta-1\selenium-server-1.0-beta-1\
    Then execute the following command to launch Selenium Server:
    java -jar selenium-server.jar -interactive
    The previous command will start Selenium Server. It should look similar to the following:
    15:55:51.004 INFO - Java: Sun Microsystems Inc. 10.0-b22
    15:55:51.004 INFO - OS: Windows XP 5.1 x86
    15:55:51.004 INFO - v1.0-beta-1 [2201], with Core v1.0-beta-1 [1994]
    15:55:51.084 INFO - Version Jetty/5.1.x
    15:55:51.084 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
    15:55:51.084 INFO - Started HttpContext[/selenium-server,/selenium-server]
    15:55:51.084 INFO - Started HttpContext[/,/]
    15:55:51.104 INFO - Started SocketListener on 0.0.0.0:4444
    15:55:51.104 INFO - Started org.mortbay.jetty.Server@13e205f
    Entering interactive mode... type Selenium commands here (e.g: cmd=open&1=http://www.yahoo.com)
  5. The Google.java file containing the Java code below is our first Selenium RC application. What the code does is to launch Internet Explorer, open Google webpage and then search for Selenium RC.
    /**
     * Google.java
     * Open Google webpage and search for "Selenium RC".
     * @Author: Xuan Ngo
     */
    import com.thoughtworks.selenium.DefaultSelenium;
     
    public class Google
    {
      public static void main(String[] args)
      {
        final String sServerHost  = "localhost";
        final int iServerPort     = 4444;
        final String sBrowserType = "*iexplore"; // For Firefox, use *firefox
        final String sBaseUrl     = "http://www.google.ca/";
     
        DefaultSelenium oDefaultSelenium = new DefaultSelenium(sServerHost, iServerPort, sBrowserType, sBaseUrl);
        oDefaultSelenium.start();           // Start Selenium.
        oDefaultSelenium.setSpeed("5000");  // Wait 5 seconds for every instructions so that you can see what Selenium is doing.
     
        // Open the main google webpage.
        oDefaultSelenium.open("http://www.google.ca/index.html"); 
     
        // Type "Selenium RC" into the search input field.
        oDefaultSelenium.type("name=q", "Selenium RC"); // Use name locator to identify the search input field.
     
        // Click on "Google Search" button
        oDefaultSelenium.click("xpath=//input[@name='btnG']");
     
        // Close the browser.
        oDefaultSelenium.stop();
      }
    }
    Execute the command below to compile the Java code.
    javac -classpath C:\selenium-remote-control-1.0-beta-1\selenium-java-client-driver-1.0-beta-1\selenium-java-client-driver.jar Google.java
     
    Note: You have to change C:\selenium-remote-control-1.0-beta-1\selenium-java-client-driver-1.0-beta-1\selenium-java-client-driver.jar to match the path where you put your selenium-java-client-driver.jar.
  6. Execute the command below to run our application.
    java -classpath C:\selenium-remote-control-1.0-beta-1\selenium-java-client-driver-1.0-beta-1\selenium-java-client-driver.jar;. Google
     
    Note: You have to change C:\selenium-remote-control-1.0-beta-1\selenium-java-client-driver-1.0-beta-1\selenium-java-client-driver.jar to match the path where you put your selenium-java-client-driver.jar.
AttachmentSize
File Google.java1.14 KB