Tuesday, September 27, 2016

Selenium Webdriver

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 RC Vs WebDriver

Selenium RCSelenium WebDriverSelenium 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 executionIts a simple and small APIComplex and a bit large API as compared to RCLess Object oriented APIPurely Object oriented APICannot 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.

Step 2 : Select the Workspace by clicking on 'Browse' Button.

Step 3 : Now Create 'New Project' from 'File' Menu.

Step 4 : Enter the Project Name and Click 'Next'.

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-ids.jar

Step 6 : The Package is created as shown below.

Step 7 : Now let us create a 'Class' by performing right click on package and select 'New' >> 'Class'

Step 8 : Now name the class and make it as main Function

Step 9 : The class outline is shown as below.

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.

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.

CommmandDescriptiondriver.get("URL")To Navigate to an applicationelement.sendKeys("inputtext")Enter some text into an input boxelement.clear()Clear the contents from the input boxselect.deselectAll()This will deselect all OPTIONs from the first SELECT on the pageselect.selectByVisibleText("some text")select the OPTION with the input specified by the user.driver.switchTo().window("windowName")Moving the focus from one window to anotherdriver.switchTo().frame("frameName")swing from frame to framedriver.switchTo().alert()Helps in handling alertsdriver.navigate().to("URL")Navigate to the URLdriver.navigate().forward()To Navigate forwarddriver.navigate().back()To Navigate backdriver.close()Closes the current Browser associated with the driverdriver.quit()Quits the driver and closes all the associated window of that driver.driver.refresh()Refreshes the current page.

No comments:

Post a Comment