Hello World Selenium

Selenium Webdriver 

Selenium is open source automated testing tool. Here is a simple Hello World example to start off using Selenium Webdriver. In this simple example, it shows how to search Hello world in google.

I have attached the complete eclipse project to help to directly import into your eclipse and start working....





 package sudhi.selenium;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 public class HelloWorld {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) throws InterruptedException{  
           String url = "http://www.google.com";          //Make sure we add http:// , otherwise it doesn't work.  
           WebDriver webDriver = new FirefoxDriver();     //launches FireFox browser,   
           //u can use Chrome or IE by using corresponding selenium-chrome-driver.jar or selenium-ie-driver.jar  
           webDriver.get(url);                                   //Load the website   
           Thread.sleep(5000);                                   //Make the thread wait for 5 seconds, just to make sure the website loads   
                                                                  //completely  
           //For getting web elements ids, names, css, xpath... u can use firefox's plugin firebug.  
           WebElement googleSearchBox = webDriver.findElement(By.id("gbqfq"));     //Find the search box, google search box has id gbqfq  
                                                                                                // means <input type="text" id="gbqfq" />  
           googleSearchBox.sendKeys("Hello World");                                   // Type Hello World in search box.  
           WebElement googleSearchButton = webDriver.findElement(By.id("gbqfb"));     //find search button  
           googleSearchButton.click();                                                            //Click on search button  
           Thread.sleep(5000);                                                                      //wait for 5 seconds to see the results  
           //Next step is doing validations using Junit's Asserts or using manual validation, its left to implementation.  
           webDriver.quit();                                   //Close the firefox   
      }  
 }  





You can download Eclipse project Here,   File---> Download





Selenium with JUnit

Below code will show you example to integrate with JUnit test framework,

Please download latest Junit.jar and copy to above project, and add it into build path. For below example i used Junit-4.8.2.jar.

Once the class is copied to above project, right click on the class, click Run as --> Junit class.




 package sudhi.selenium;  
 import org.junit.After;  
 import org.junit.AfterClass;  
 import org.junit.Before;  
 import org.junit.BeforeClass;  
 import org.junit.Test;  
 import org.junit.runner.RunWith;  
 import org.junit.runners.JUnit4;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 @RunWith(JUnit4.class)  
 public class HelloWorldSeleniumJUnit {  
      private static WebDriver webDriver = null;       
      private static String url = "http://www.google.com";  
      /**  
       * Called only once when the class is loaded into Junit framework  
       */  
      @BeforeClass  
      public static void beforeClass(){  
           webDriver = new FirefoxDriver();     //launches FireFox browser,   
      }  
      /**  
       * called every time before testcases i.e., methods which are marked with @Test annotation  
       */  
      @Before  
      public void beforeTestCaseMethod() throws InterruptedException{  
           webDriver.get(url);                                   //Load the website  
           Thread.sleep(5000);  
      }  
      /**  
       * Test case to search hello world text in google.  
       */  
      @Test  
      public void testHelloWorldSearch() throws InterruptedException{  
           WebElement googleSearchBox = webDriver.findElement(By.id("gbqfq"));  
           googleSearchBox.sendKeys("Hello World");  
           WebElement googleSearchButton = webDriver.findElement(By.id("gbqfb")); //sometimes Search button id will be "gbqfba"  
           googleSearchButton.click();       
           Thread.sleep(5000);  
      }  
      /**  
       * called every time before testcases i.e., methods which are marked with @Test annotation  
       */  
      @After  
      public void afterTestCaseMethod() throws InterruptedException{  
           webDriver.get(url);                                   //Optional, revert to page like before testcase.  
           Thread.sleep(2000);  
      }  
      /**  
       * Called only once when the class is unloaded from Junit framework  
       */  
      @AfterClass  
      public static void afterClass(){  
           webDriver.quit();                                   //Close the firefox   
      }  
 }  


Scenario:
Selenium Multiple Junit Tests cases :

Make sure firefox driver is created in each class and reset to page where individual test can run independent of other testcases in the class. This is because, Junit testcases when run by build tools like Maven, Ant, .. other than eclipse, the testcases are not run in order of the classes/methods. These build tools will consider each method as independent testcases i.e., not dependent on other class or testcase method. Maven can create multiple threads to execute testcases. A typical example, testcases to search customer information in company portal  would be like


class ......

@BeforeClass
public static void beforeClass{
// Open firefox
}

@Before
public void beforeTestCaseMethod(

//load login page

)

@Test
public void testLogin(){

//Selenium logic to test login

}

@Test
public void testCustomerSearch{

// Selenium logic to test login------> Because it is possible that testCustomerSearch can execute before                   testLogin as i told each testcase is considered as independent testcase

// Selenium logic to search a customer

}



@After
public void AfterTestCaseMethod(

//logout
//load login page ----> to make sure other testcases expect to start properly

)



@AfterClass
public static void AfterClass{
// close firefox
}

Above approach will work properly irrespective of the order of testcase method execution. This is right approach as per JUnit methodology but here there is overhead of duplication of login. This approach is only if you are using build tools to execute testcases. You can maintain state by removing logout if u are using eclipse to run the JUnit testcases as eclipse will execute testcases in the order they are found in the class.


Next-->







Comments