Selenium WebDriver – 5 Minute Getting Started Guide

Selenium 2.0 is commonly called as Selenium WebDriver  as it is integrated with WebDriver API. WebDriver is designed to provide a simple and concise programming interface in addition to addressing some limitations in the Selenium-RC API. Selenium WebDriver support dynamic web pages where element of a Page may change without the page itself being reloaded. Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation.

This getting started guide introduces you to Webdriver’s Java API.

Start by downloading latest binaries and unpack them into a directory.  A safe choice is the latest version of selenium-server-standalone-2.33.0.jar (This was the latest version at the time if writing this). This folder is called $WEBDRIVER_HOME.

Now open your favorite IDE for example Eclipse.

  1. Start a new Java project
  2. Add all the JAR files under $WEBDRIVER_HOME to the CLASSPATH
  3. You are now ready to write some code. You can start with the following example. It will search the term “Selenium Web Driver” in google search and then will check the Page tile and display it.

Example 1:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
publicclass FirstTest {
  publicstaticvoid main(String[] args) {
  // Create a new instance of the html unit driver
  WebDriver driver = new HtmlUnitDriver();
  // And now use this to visit Google
  driver.get("http://www.google.com");
  // Find the text input element by its name
  WebElement element = driver.findElement(By.name("q"));
  // Enter something to search for
  element.sendKeys("Selenium WebDriver");
  // Now submit the form. WebDriver will find the form for us from the element
  element.submit();
  // Check the title of the page
  System.out.println("Page title is: " + driver.getTitle());
 }
}

Compile and run it. You should see a line “Page title is: Selenium WebDriver – Google Search”.

2013-06-24_1621-FirstTest

The above sample uses HTMLUnitDriver. This is a pure Java driver that runs entirely in-memory. Because of this, you will not see a new browser window open.

The next example uses FirefoxDriver. Make sure that you have Firefox installed in default location.

 

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
publicclass SecondTest {
  publicstaticvoid main(String[] args) throws Exception {
  // The Firefox driver supports javascript
  WebDriver driver = new FirefoxDriver();
  // Go to the Google Suggest home page 
  driver.get("http://www.google.com/webhp?complete=1&hl=en");
  // Enter the query string "Selenium WebDriver" 
  WebElement query = driver.findElement(By.name("q")); 
  query.sendKeys("Selenium WebDriver");
  // Sleep until the div we want is visible or 5 seconds is over
  long end = System.currentTimeMillis() + 5000;
  while (System.currentTimeMillis() < end) {
   WebElement resultsDiv = driver.findElement(By.className("gssb_e"));
   // If results have been returned, the results are displayed in a drop down.
   if (resultsDiv.isDisplayed()) {
    break;
   }
  }
  // And now list the suggestions
  List<WebElement> allSuggestions = driver.findElements(By.xpath("//td[@class='gssb_a gbqfsf']"));
  for (WebElement suggestion : allSuggestions) {
    System.out.println(suggestion.getText());
  }
 }
}

When you run this program, you will see the list of suggestions being printed to the console.

2013-06-24_1620-SecondTest

The above examples are in bare form. You can modify the framework using JUNIT/TestNG. The next post on selenium will cover more on this.

 

Do you have any issues? If so you can email me directly.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.