My favorites | Sign in
Project Logo
       
Search
for
Updated Jan 26, 2009 by josephg
Labels: Featured
GettingStarted  
Getting started with WebDriver

The 5 Minute Getting Started Guide

Before you start either:

Assuming that you've built everything and the source is located at $WEBDRIVER_HOME, and we're only going to use the HtmlUnitDriver to start with:

  • Start a new Java project in your favourite IDE
  • Add $WEBDRIVER_HOME/common/build/webdriver-common.jar to the CLASSPATH
  • Add $WEBDRIVER_HOME/htmlunit/build/webdriver-htmlunit.jar to the CLASSPATH
  • Add all the Jar files under $WEBDRIVER_HOME/htmlunit/lib/runtime to the CLASSPATH

If you have downloaded the binaries, then the steps you need to follow are:

  • Start a new Java project in your favourite IDE
  • Unzip the HtmlUnitDriver binary and copy all the JAR files to the CLASSPATH

Whichever way you chose, 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.

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.

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");

        // 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 it. You should see a line with the title of the Google search results as output. Congratulations, you've managed to get started with WebDriver!

In this next example, let's use a page that requires Javascript to work properly, such as Google Suggest. You will also be using the FirefoxDriver. The easiest way to get hold of this is to download the binary package from the website, unzip it and copy the JAR files on to your CLASSPATH. Alternatively, you can build from source (using rake firefox), copying the built JARs from firefox/build, firefox/lib/runtime and common/build into your CLASSPATH

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("gac_m"));

            // 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='gac_c']"));
        
        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 NextSteps page, you will learn how more about how to use WebDriver. It covers aspects such as navigating forward and backward in your browser's history, how to use frames and windows and it provides a more complete discussion of the examples than has been done as you've been GettingStarted. If you're ready, let's take the NextSteps!


Comment by ale.mesias, Oct 31, 2007

Have some stuff to input in this driver. I want to check if some elements are inside the current page (like "isElementPresent" of Selenium). And have some documentation im thinking to put inside the code.

How i can help you guys ?

Comment by simon.m.stewart, Oct 31, 2007

Help is always appreciated! There are several ways to get involved (from least to most effort)

  • Add a new issue to the project requesting the feature you'd love to see
  • Join the mailing list to discuss the proposed changes. We're a friendly bunch, so don't worry about asking questions!
  • CheckOut the source code, write a suitable automated test and add a patch to your issue

Hope that helps. :)

Comment by carlos.villela, Mar 28, 2008

'Before you start' is now incorrect - there are packaged binaries now :)

Comment by turturiellomartino, Apr 02, 2008

Any plan to support logging?

Comment by simon.m.stewart, May 07, 2008

There is some support for adding your own logging provided in the support package. Please take a look at EventFiringWebDriver?.

Comment by carlosluiszuniga, Aug 14, 2008

Hello Simon,

Are there any plans to release web driver as a maven artifact? I think a lot of projects are using maven and it could be beneficial for the adoption of this project.

Regards

Comment by yilativs, Oct 02, 2008

I vote fore Maven support too. BTW HtmlUnit is already in Maven Central Repo.

Comment by michael.tamm2, Oct 27, 2008

Hi, we already have put some Maven artifacts in the OpenQA repository.

For more information please see http://code.google.com/p/webdriver/wiki/UsingWebDriver#With_Maven

Comment by stev...@smarttesttech.com, Nov 21, 2008

The google suggest seems to have shortened the class names. The following worked for me, YMMV:

RenderedWebElement? resultsDiv = (RenderedWebElement?) driver.findElement(By.className("gac_m"));
List<WebElement> allSuggestions = driver.findElements(By.xpath("//td@class='gac_c'?"));
Comment by jrhuggins, Feb 15, 2009

Here's my 5 minute guide for getting started with PyWebDriver?: http://gist.github.com/60740

Comment by abhishek...@google.com, Mar 09, 2009

Is this now able to access the browser chrome?

Comment by simon.m.stewart, Mar 27, 2009

Accessing the browser chrome is outside the scope of what we're trying to do with WebDriver?. What problem are you trying to solve? Perhaps there's an alternative we can point you towards.

Comment by nirmaljpatel, May 19, 2009

Does webdriver work with web applications written in GWT / GWT-Ext??

Comment by arthur.kalm, Jun 10, 2009

During the Google Wave: Powered by GWT talk at Google I/O, they mentioned they use WebDriver?, so I'm assuming it works with GWT.

Comment by ruchi.malpani, Jul 24, 2009

Is there a file upload functionality in WebDriver?? 'sendKeys' command does not work in the "file" input field.

Comment by hanishi77, Jul 28, 2009

Is WebDriver? robust enough to serve a component that realize the infrastracture such as the diagram below? Serve as an adapter for XML<->XHML? Or become a ESB component?

HTTP Request <-XML-> RESTful <-> WebDriver?(Pooled) <-> Web Application(returns XHML)

Comment by auduwage, Nov 10, 2009

I downloaded the the files using link provided under "If you have downloaded the binaries, then the steps you need to follow are:" but I dont see the package structure as mentioned in the example. None of these packages are available 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;


Sign in to add a comment
Hosted by Google Code