|
|
The 5 Minute Getting Started Guide
Before you start:
- CheckOut the latest version of the code, because there aren't any pre-packed binaries yet.
- Follow the instructions in BuildingWebDriver.
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
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.
Sign in to add a comment

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 ?
Help is always appreciated! There are several ways to get involved (from least to most effort)
Hope that helps. :)
'Before you start' is now incorrect - there are packaged binaries now :)
Any plan to support logging?
There is some support for adding your own logging provided in the support package. Please take a look at EventFiringWebDriver?.