Thursday, March 15, 2012

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.

No comments:

Post a Comment