Selenium 2 : Complete Reference

Selenium HQ has officially announced the future of Selenium as Webdriver and OpenQA has completely moved thoughtwork code out of boundaries of Selenium.With the introduction of GRID2, its clear that we also have 2 move in this new direction.

Why Selenium 2 when there are many monuments build on Selenium 1?
  1. No remote control (RC) , more like playing it live now. No need to invoke RC,driver will directly drive the browser using WebDriver 
  2. Easy OOPS oriented, more creative WebElements
  3. It reduced the JS security model (same origin policy rule) that restricts Selenium 1.x capabilities 
  4. Selenium 2.0 works at the OS/browser level
  5. Backward compatible API support
Whats a driver, and how many types of drivers do we have?

WebDriver provides 4 implementations of its driver:
  • HtmlUnitDriver, a pure Java driver based on HtmlUnit browser simulator (all Java-capable OS)
    • When running, you won’t see any window opening because it doesn’t render any page like Firefox would. You can’t use it to get size or position of pages' elements
    • It is very fast but does not provides JavaScript yet: this feature is still a WIP. If you want to use it anyway, use driver.setJavascriptEnabled(true)
    • It does not support windows related cases, making it hard to work while switching windows or handling popups
  • FirefoxDriver: supports upto Firefox 9  
  • InternetExplorerDriver: supports InternetExplorer ( IE6, 7 ,8 and 9)
  • ChromeDriver: drives Google’s browser (>= 4.0, all OS)

Webdriver and WebElements:

WebDriver is a clean, fast framework for automated testing of webapps. it uses whichever mechanism is most appropriate to control the browser. For Firefox, this means that WebDriver is implemented as an extension. For IE, WebDriver makes use of IE's Automation controls. By changing the mechanism used to control the browser, we can circumvent the restrictions placed on the browser by the JavaScript security model. In those cases where automation through the browser isn't enough, WebDriver can make use of facilities offered by the Operating System. For example, on Windows we simulate typing at the OS level, which means we are more closely modeling how the user interacts with the browser, and that we can type into "file" input elements.There is a Object-based API for WebDriver, rather than follow Selenium's dictionary-based approach.

A typical example using WebDriver in Java looks like this:

// Create an instance of WebDriver backed by Firefox
WebDriver driver = new FirefoxDriver();

// Now go to the Google home page
driver.get("http://www.google.com");

// Find the search box, and (ummm...) search for something
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("selenium");
searchBox.submit();

// And now display the title of the page
System.out.println("Title: " + driver.getTitle());

WebElement is defined as a java collection of element or objects on the webpage.WebElement provides the methods to interact with the element. If you call an invalid method on an element (toggle() on a Button for instance), an exception will be thrown

Now, let us look into HtmlUnitDriver more closely:


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) {
        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("Enjoy!"); //

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


Instead of driver.get() you can also use driver.navigate().to("http://www.example.com"); //
 
Some other browser commands:
driver.navigate().forward();
driver.navigate().back();
 
Interacting with Frames and windows of the browser:
for (String handle : driver.getWindowHandles()) { //
  driver.switchTo().window(handle);
}

...

driver.switchTo().frame("frameName");
driver.switchTo().frame("frameName.0.child"); // 

Managing cookies:
 
through manage() 
 
// Go to the correct domain
driver.get("http://www.example.com");

// Now set the cookie. This one's valid for the entire domain
Cookie cookie = new Cookie("key", "value");
driver.manage().addCookie(cookie);

// And now output all the available cookies for the current URL
Set<Cookie> allCookies = driver.manage().getCookies();
for (Cookie loadedCookie : allCookies) {
    System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue()));
}
 
 To be continued tomorrow..

Comments

  1. Thanks, Experience with various technologies and businesses this is generally helpful.
    Still, I followed step-by-step your method in this selenium online training
    selenium certification
    selenium online training Hyderabad
    selenium online courses

    ReplyDelete

Post a Comment

Popular Posts