Thursday, March 15, 2012

Selenium RC with TestNG

Here is an example explaining how to use TestNG with Selenium RC i.e using TestNG  to run selenium rc tests.

//GoogleTestTestNG.java
  1. package test.example;
  2. import org.testng.annotations.AfterClass;
  3. import org.testng.annotations.BeforeClass;
  4. import org.testng.annotations.Test;
  5. import com.thoughtworks.selenium.DefaultSelenium;
  6. import com.thoughtworks.selenium.Selenium;
  7. /**
  8.  * Search Google example.
  9.  *
  10.  * @author aksahu
  11.  */
  12. public class GoogleTestTestNg {
  13.    
  14.     Selenium selenium;
  15.    
  16.     @BeforeClass
  17.     public void startSelenium(){
  18.         selenium = new DefaultSelenium("localhost"4444"*firefox""http://www.google.com");
  19.         selenium.start();
  20.         selenium.windowMaximize();
  21.     }
  22.    
  23.     @Test
  24.     public void testGoogleSearch(){    
  25.         selenium.open("/");
  26.         selenium.type("id=lst-ib""selenium");
  27.         selenium.click("name=btnK");
  28.         selenium.click("link=Selenium - Web Browser Automation");
  29.         selenium.waitForPageToLoad("30000");
  30.     }
  31.    
  32.     @AfterClass
  33.     public void stopSelenium(){    
  34.         selenium.stop();
  35.     }
  36. }

          This test launches firefox browser and executes the google search test.The selenium session is started in startSelenium() method which is called before any test run in the test class and ends with stopSelenium() method which 'll call after the tests run.

Selenium RC with JUnit4

Here is an example explaining how to use JUnit4 with Selenium RC i.e using JUnit4 to run selenium rc tests.

//GoogleTestJunit4.java
  1. package test.example;
  2. import org.junit.AfterClass;
  3. import org.junit.BeforeClass;
  4. import org.junit.Test;
  5. import com.thoughtworks.selenium.DefaultSelenium;
  6. import com.thoughtworks.selenium.Selenium;
  7. /**
  8.  * Search Google example.
  9.  *
  10.  * @author aksahu
  11.  */
  12. public class GoogleTestJUnit4 {
  13.    
  14.     Selenium selenium;
  15.    
  16.     @BeforeClass
  17.     public void startSelenium(){
  18.         selenium = new DefaultSelenium("localhost"4444"*firefox""http://www.google.com");
  19.         selenium.start();
  20.         selenium.windowMaximize();
  21.     }
  22.    
  23.     @Test
  24.     public void testGoogleSearch(){    
  25.         selenium.open("/");
  26.         selenium.type("id=lst-ib""selenium");
  27.         selenium.click("name=btnK");
  28.         selenium.click("link=Selenium - Web Browser Automation");
  29.         selenium.waitForPageToLoad("30000");
  30.     }
  31.    
  32.     @AfterClass
  33.     public void stopSelenium(){    
  34.         selenium.stop();
  35.     }
  36. }

This test launches firefox browser and executes the google search test.The selenium session is started in startSelenium() method which is called before any test run in the test class and ends with stopSelenium() method which 'll call after the tests run.

Selenium RC with JUnit3

Here is an example explaining how to use JUnit3 with Selenium i.e using JUnit3 to run selenium test.

//GoogleTestJunit3.java
  1. package test.example;
  2. import junit.framework.TestCase;
  3. import com.thoughtworks.selenium.DefaultSelenium;
  4. import com.thoughtworks.selenium.Selenium;
  5. /**
  6.  * Search Google example.
  7.  *
  8.  * @author aksahu
  9.  */
  10. public class GoogleTestJUnit3 extends TestCase{
  11.    
  12.     Selenium selenium;
  13.    
  14.     public void setUp(){
  15.         selenium = new DefaultSelenium("localhost"4444"*firefox""http://www.google.com");
  16.         selenium.start();
  17.         selenium.windowMaximize();
  18.     }
  19.    
  20.     public void testGoogleSearch(){      
  21.         selenium.open("/");
  22.         selenium.type("id=lst-ib""selenium");
  23.         selenium.click("name=btnK");
  24.         selenium.click("link=Selenium - Web Browser Automation");
  25.         selenium.waitForPageToLoad("30000");
  26.     }
  27.    
  28.     public void tearDown(){    
  29.         selenium.stop();       
  30.     }
  31. }

            This test launches firefox browser to run the google search test.The selenium session is started in setUp() method which is called before every test run and ends with tearDown() method which is called after every test run.Since setUp() and tearDown() are called before and after every test run so we can use only one test method to be run in each class if you want to start and stop selenium sessions with setUp() and tearDown() methods.Multiple tests with this fashion is a drawback i.e if you want to write multiple tests in a same class selenium session 'll start and stop for every test run.

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.