Friday, September 4, 2009

Run your first selenium test 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. 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\

5. Then execute the following command to launch Selenium Server:
java -jar selenium-server.jar -interactive

6. 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".
*/
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.com/";

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.com/");

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


7.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.
8. 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


No comments:

Post a Comment