Thursday, March 15, 2012

A simple Selenium RC example

Here is a simple example that explains how to write a test using selenium rc(selenium 1).
Follow the following steps to write your 1st test using selenium rc.
  • Open your desired Java IDE (in my case Eclipse)
  • Create a java project.
  • Create a package 'test.example' and add a new class 'GoogleTest.java' to it.
  • Create a folder namely 'lib' and place the downloaded 'selenium-java-.jar' file and add it to your project buildpath.
//GoogleTest.java
  1. package test.example;
  2. import com.thoughtworks.selenium.DefaultSelenium;
  3. import com.thoughtworks.selenium.Selenium;
  4. /**
  5.  * Search Google example.
  6.  *
  7.  * @author aksahu
  8.  */
  9. public class GoogleTest {
  10.    
  11.     static Selenium selenium;
  12.    
  13.     public static void main(String[] args) {
  14.         selenium = new DefaultSelenium("localhost"4444"*firefox""http://www.google.com");
  15.         selenium.start();
  16.         selenium.windowMaximize();
  17.        
  18.         selenium.open("/");
  19.         selenium.type("id=lst-ib""selenium");
  20.         selenium.click("name=btnK");
  21.         selenium.click("link=Selenium - Web Browser Automation");
  22.         selenium.waitForPageToLoad("30000");
  23.        
  24.         selenium.stop();       
  25.     }
  26. }
This test opens "http://www.google.com" in firefox browser and search a keyword.
Note: Make sure that you have started the selenium server before running the test.

No comments:

Post a Comment