Wednesday, 10 September 2014

Selenium Webdriver


WebDriver is a tool for automating testing web application popularly known as Selenium 2.0. WebDriver uses a different underlying framework while Selenium Remote Control uses javascript Selenium-Core embedded within the browser which has got some limitations. Webdriver interacts directly with the browser without any intermediary unlike Selenium remote control which depends on a Server. It is used in the below context :
The developer community in Selenium strive hard to improve Selenium continuously and Integrating WebDriver with selenium is one among them.
  • Mult-browser testing including improved functionality for browsers not well-supported by Selenium Remote control (selenium 1.0)
  • Handling multiple frames, multiple browser windows, popups, and alerts.
  • Complex Page navigation.
  • Advanced user navigation such as Drag-and-drop.
  • AJAX-based UI elements

Architecture

Webdriver is best explained with a simple architecture diagram as shown below.
Selenium IDE 92

Selenium RC Vs WebDriver

Selenium RCSelenium WebDriver
Selenium RC architecture is complicated as the server needs to be up and running before starting a test.WebDriver's architecture is simpler than Selenium RC as it controls the browser from the OS level.
Selenium Server acts as a middle man between browser and selenese commandsWebDriver interacts directly to the browser and uses the browser's engine to control it.
Selenium RC script execution is slower since it uses a Javascript to interact with RCWebDriver is faster as it interacts directly with browser.
Selenium RC cannot support the headless as tt needs a real browser to work with.WebDriver can support the headless execution
Its a simple and small APIComplex and a bit large API as compared to RC
Less Object oriented APIPurely Object oriented API
Cannot test mobile ApplicationsCan test iPhone/Android applications

Scripting using Webdriver

Let us understand how to work with webdriver. For demo purposes, we would use http://www.calculator.net/. We will perform a "percent Calculator" which is located under "Math Calculator". We have already downloaded the required webdriver JAR's. Refer Environmental chapter for details.
Step 1 : Launch "Eclipse" from the Extracted Eclipse Folder.
Selenium IDE 75
Step 2 : Select the Workspace by clicking on 'Browse' Button.
Selenium IDE 76
Step 3 : Now Create 'New Project' from 'File' Menu.
Selenium IDE 53
Step 4 : Enter the Project Name and Click 'Next'.
Selenium IDE 77
Step 5 : Goto Libraries Tab and select all the JAR's that we have dowonloaded(Refer Environment Set up Chapter). Add reference to all JAR's of Selenium Webdriver Library folder and also selenium-java-2.42.2.jar and selenium-java-2.42.2-srcs.jar
Selenium IDE 78
Step 6 : The Package is created as shown below.
Selenium IDE 79
Step 7 : Now let us create a 'Class' by performing right click on package and select 'New' >> 'Class'
Selenium IDE 82
Step 8 : Now name the class and make it as main Function
Selenium IDE 80
Step 9 : The class outline is shown as below.
Selenium IDE 81
Step 10 : Now it is time to code. The below Script is easier to understand as it explains clearly step by step in the embedded comments. Please take a look at "Locators" chapter to understand how to capture object properties.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class webdriverdemo
{
  public static void main(String[] args)
  {
 WebDriver driver = new FirefoxDriver();

    //Puts a Implicit wait, Will wait for 10 seconds before throwing exception
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    //Launch website
 driver.navigate().to("http://www.calculator.net/");
 
 //Maximize the browser
 driver.manage().window().maximize();

    // Click on Math Calculators
 driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click();
  
    // Click on Percent Calculators
 driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click();

 // Enter value 10 in the first number of the percent Calculator
    driver.findElement(By.id("cpar1")).sendKeys("10");

    // Enter value 50 in the second number of the percent Calculator
    driver.findElement(By.id("cpar2")).sendKeys("50");
    
    // Click Calculate Button
    driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click();

    // Get the Result Text based on its xpath
    String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText();
    
 //Print a Log In message to the screen
    System.out.println(" The Result is " + result);
    
 //Close the Browser.
    driver.close();    
  }
}
Step 11 : The Output of the above script would be printed in Console.
Selenium IDE 83

Most used Commands

The below table lists the most used commands in Webdriver along with its syntax which will help us to develop Webdriver scripts seamlessly.
CommmandDescription
driver.get("URL")To Navigate to an application
element.sendKeys("inputtext")Enter some text into an input box
element.clear()Clear the contents from the input box
select.deselectAll()This will deselect all OPTIONs from the first SELECT on the page
select.selectByVisibleText("some text")select the OPTION with the input specified by the user.
driver.switchTo().window("windowName")Moving the focus from one window to another
driver.switchTo().frame("frameName")swing from frame to frame
driver.switchTo().alert()Helps in handling alerts
driver.navigate().to("URL")Navigate to the URL
driver.navigate().forward()To Navigate forward
driver.navigate().back()To Navigate back
driver.close()Closes the current Browser associated with the driver
driver.quit()Quits the driver and closes all the associated window of that driver.
driver.refresh()Refreshes the current page.

No comments:

Post a Comment