Thursday, March 15, 2012

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.

No comments:

Post a Comment