• No results found

The 5 Minute Getting Started Guide

In document Selenium_Documentation (Page 112-115)

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 byDownloadingthe 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 library does: it’s entirely self-contained, and you usually 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");

// 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()); }

}

HtmlUnit isn’t confined to just Java. Other languages can access it as well. Below is the same example in C#. Note that you’ll need to run the remote WebDriver server to use HtmlUnit from C#

using OpenQA.Selenium;

using OpenQA.Selenium.Remote;

class Example {

static void Main(string[] args) {

//to use HtmlUnit from .Net we must access it through the RemoteWebDriver

//Download and run the selenium-server-standalone-2.0b1.jar locally to run this example ICapabilities desiredCapabilities = DesiredCapabilities.HtmlUnit();

IWebDriver driver = new RemoteWebDriver(desiredCapabilities);

//the .Net Webdriver relies on a slightly different API to navigate to //web pages because ’get’ is a keyword in .Net

driver.Navigate().GoToUrl( "http://google.ca/" );

//The rest of the code should look very similar to the Java library IWebElement element = driver.FindElement(By.Name( "q" ));

element.SendKeys( "Cheese!" ); element.Submit();

System.Console.WriteLine( "Page title is: " + driver.Title); driver.Quit();

System.Console.ReadLine(); }

}

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 thatFirefox is installed on your machine and is in the normal location for your OS.

10.3.1 Java

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()); } } } 10.3.2 C# using OpenQA.Selenium.Firefox; using OpenQA.Selenium; using System.Collections.ObjectModel; class GoogleSuggest {

static void Main(string[] args) {

IWebDriver driver = new FirefoxDriver();

driver.Navigate().GoToUrl( "http://www.google.com/webhp?complete=1&hl=en" ); IWebElement query = driver.FindElement(By.Name( "q" ));

query.SendKeys( "Cheese" );

//This line is different than the Java version above. Instead of telling the

//executing thread to sleep we use an implicit wait. This means the driver won’t instantly //throw an error if the suggestion box isn’t there. Instead it will poll the web page until //the element is present of the timeout expires, in this case 5 seconds.

driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 5));

ReadOnlyCollection<IWebElement> allSuggestions = driver.FindElements(By.XPath( "//td[@class=’gac_c’]" )); foreach (IWebElement suggestion in allSuggestions)

{ System.Console.WriteLine(suggestion.Text); } System.Console.ReadLine(); driver.Quit(); } }

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!

In document Selenium_Documentation (Page 112-115)