|
GettingStarted
Getting started with WebDriver
The 5 Minute Getting Started GuideBefore 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:
If you have downloaded the binaries, then the steps you need to follow are:
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! |
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?.
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
I vote fore Maven support too. BTW HtmlUnit is already in Maven Central Repo.
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
The google suggest seems to have shortened the class names. The following worked for me, YMMV:
Here's my 5 minute guide for getting started with PyWebDriver?: http://gist.github.com/60740
Is this now able to access the browser chrome?
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.
Does webdriver work with web applications written in GWT / GWT-Ext??
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.
Is there a file upload functionality in WebDriver?? 'sendKeys' command does not work in the "file" input field.
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)
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;