What's new? | Help | Directory | Sign in
Google
webdriver
A developer focused tool for automated testing of webapps
  
  
  
  
    
Search
for
Updated Jun 27, 2008 by simon.m.stewart
Labels: Featured
GettingStarted  
Getting started with WebDriver

The 5 Minute Getting Started Guide

Before you start:

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:

We're now ready to write some code. Perhaps something that looks like:

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
        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. Congratulations, you've managed to get started with WebDriver!

Now let's try something that needs Javascript. Follow the instructions on the FirefoxDriver page in order to create a profile called "WebDriver" and install the WebDriver extension. 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.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GoogleSuggest {
    @SuppressWarnings("unchecked")
    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 "I like cheese"
        WebElement query = driver.findElement(By.name("q"));
        query.sendKeys("Cheese");

        // This should go at some point, but allows Google to return some suggestions for now
        Thread.sleep(2000);

        // And now list the suggestions
        List<WebElement> allSuggestions = driver.findElements(By.xpath("//td[@class='google-ac-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! You're ready to take the NextSteps, now.


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?.


Sign in to add a comment