WebDriver 5 Minute Getting Started Guide 

WebDriver is a tool for automating testing web applications, and in particular to verify that they work as expected. It aims to provide a friendly API that’s easy to explore and understand, which will help make your tests easier to read and maintain. It’s not tied to any particular test framework, so it can be used equally well with JUnit, TestNG or from a plain old “main” method. This “Getting Started” guide introduces you to WebDriver’s Java API and helps get you started becoming familiar with it.

Start by Downloading the latest binaries and unpack them into a directory. From now on, we’ll refer to that as $WEBDRIVER_HOME. Now, open your favourite IDE and:
• Start a new Java project in your favourite IDE
• Add all the JAR files under $WEBDRIVER_HOME to the CLASSPATH
You can see that WebDriver acts just as a normal Java library does: it’s entirely self-contained, and you don’t need to remember to start any additional processes or run any installers before using it. You’re now ready to write some code. An easy way to get started is this example, which searches for the term “Cheese” on Google and then outputs the result page’s title to the console. You’ll start by using the HtmlUnit Driver. This is a pure Java driver that runs entirely in-memory. Because of this, you won’t see a new browser window open.

package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Example {
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get( "http://www.google.com" );
106 Chapter 10. Selenium 2.0 and WebDriver
Selenium Documentation, Release 1.0
// Find the text input element by its name
WebElement element = driver.findElement(By.name( "q" ));
// Enter something to search for
element.sendKeys( "Cheese!" );
// 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 this. You should see a line with the title of the Google search results as output on the console.

Congratulations, you’ve managed to get started with WebDriver!
In this next example, you shall use a page that requires Javascript to work properly, such as Google Suggest. You will also be using the Firefox Driver. Make sure that Firefox is installed on your machine and is in the normal location for your OS.
Once that’s done, create a new class called GoogleSuggest, which looks like:

package org.openqa.selenium.example;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.RenderedWebElement;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GoogleSuggest {
public static void 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 "Cheese"
WebElement query = driver.findElement(By.name( "q" ));
query.sendKeys( "Cheese" );
// Sleep until the div we want is visible or 5 seconds is over
long end = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < end) {
// Browsers which render content (such as Firefox and IE)
// return "RenderedWebElements"
RenderedWebElement resultsDiv = (RenderedWebElement) driver.findElement(By.className( // If results have been returned,
// the results are displayed in a drop down.
if (resultsDiv.isDisplayed()) {
break;
10.3. The 5 Minute Getting Started Guide 107
Selenium Documentation, Release 1.0
}
}
// And now list the suggestions
List allSuggestions = driver.findElements(By.xpath( "//td[@class=’gac_for (WebElement suggestion : allSuggestions) {
System.out.println(suggestion.getText());
}
}
}

When you run this program, you’ll see the list of suggestions being printed to the console. That’s all there is to using WebDriver!

Hopefully, this will have whet your appetite for more. In the Next Steps section you will learn more about how to use WebDriver for things such as navigating forward and backward in your browser’s history, and how to use frames and windows. It also provides a more complete discussion of the examples than The 5 Minute Getting Started Guide. If you’re ready, let’s take the Next Steps!

How did this solution work for you? Do you have a better alternative? Feel free to share your thoughts in comments section below.


Google Webdriver Forum -The Online Community for Google Webdriver and Selenium RC Users and Professionals
1 response to “WebDriver 5 Minute Getting Started Guide”
  1. Payal says:

    Hello,

    We are in a mode to support multiple browsers like IE 6,7,8; Google Chrome, Mozilla versions, Safari. Will the following work:

    Write one script and that should run for all browsers ?

    If not then why?

Leave a Reply