Webdriver

Getting Started:

http://www.hurricanesoftwares.com/15-common-selenium-webdriver-commands/
http://www.softwaretestinghelp.com/selenium-webdriver-commands-selenium-tutorial-17/

Download WebDriver and unpack it into a directory (any directory). We will refer to this directory as $WEBDRIVER_HOME

With your favorite IDE, add all the JAR files under $WEBDRIVER_HOME to your CLASSPATH.

Write your test code:

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.JavascriptExecutor;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

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.

Each test is a class, and it defines:

public static void main(String[] args) throws Exception {
  ...
}

What is HtmlUnitDriver?

HtmlUnitDriver is a pure Java driver that runs entirely in-memory. Because of this, you won't see any new browser window open.

How many ways does WebDriver support to retrieve an element?

WebElement element;
element = driver.findElement(By.id("passwd-id"));
element = driver.findElement(By.name("passwd"));
element = driver.findElement(By.xpath("//input[@id='passwd-id']"));

You can also look for a link by its text, but be careful! The text must be an exact match! You should also be careful when using XPATH in WebDriver. If there’s more than one element that matches the query, then only the first will be returned. If nothing can be found, a NoSuchElementException will be thrown.

Can WebDriver emulate Selenium RC?

Yes, via WebDriverBackedSelenium. If we have existing tests that were written using the Selenium RC API, but you wish to use WebDriver API (because WebDriver API is supposedly better), you can emulate RC. This allows you to get your existing tests to run with WebDriver, and over time, you can refactor your tests and convert those Selenium RC API to WebDriver API:

// You may use any WebDriver implementation. Firefox is used here as an example
WebDriver driver = new FirefoxDriver();

// A "base url", used by selenium to resolve relative URLs
 String baseUrl = "http://www.google.com";

// Create the Selenium implementation
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);

// Perform actions with selenium
selenium.open("http://www.google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");

// Get the underlying WebDriver implementation back. This will refer to the
// same WebDriver instance as the "driver" variable above.
WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();

// Finally, close the browser. Call stop on the WebDriverBackedSelenium instance
// instead of calling driver.quit(). Otherwise, the JVM will continue running after
// the browser has been closed.
selenium.stop();

Can we use WebDriver on top of Selenium RC?

WebDriver does not support as many browsers as Selenium RC does. In order to provide that support while still using the WebDriver API, you can make use of the SeleniumCommandExecutor:

Capabilities capabilities = new DesiredCapabilities()
capabilities.setBrowserName("safari");
CommandExecutor executor = new SeleneseCommandExecutor("http:localhost:4444/", "http://www.google.com/", capabilities);
WebDriver driver = new RemoteWebDriver(executor, capabilities);

How can we catch JavaScript error?

Can we run tests that are recorded by Selenium IDE using WebDriver?

How can we indicate which browser to be used?

Perhaps we can get this from build.xml file, and in the code we have:

if (browser is Firefox) {
  browser = new FirefoxDriver();
}

How can I run individual test?

How can I organize a test suite?

WebDriver + TestNG + Eclipse

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License