Upload
dipesh-bhatewara
View
199
Download
4
Tags:
Embed Size (px)
Citation preview
3
WebDriver
Is a Self Contained Library of APIs
Uses browser capabilities over injected JavaScripts
“Best fit” technology
Clean & Object Oriented API
Easy to maintain
Faster than Selenium 1
Tightly bound to the browser, no need of Selenium Server
Bindings : Java, C#, Python, Ruby
Android and iPhone support
4
Selenium Server in Selenium2
Replicate Selenium RC functionalities
Remote WebDriver
Selenium Grid 2
5
WebDriver
WebDriver Interface
Firefox
Driver
Internet
Explorer
Driver
Chrome
Driver
HTML Unit
Driver
Opera Driver
Andriod
Driver
iPhone Driver
API – Must Know
Webdriver – Control Browser
Webdriver driver = new FirefoxDriver();
WebElement – works with elements on page
WebElement username = driver.findElement(By.id(“user”));
API – Must Know
void get(“url”) - open the web page
void quit() - close the browser
List<WebElement> findElements(By by) - find elements (more than one element)
API – Find Elements
There are many different ways to find elements
By.id(“objectId”)
By.linkText(“textUsedInTheLink”)
By.partialLinkText(“partOftextUsedInTheLink”)
By.tagName(“HTMLNodeTag”)
By.className(“cssClassOnObject”)
By.cssSelector(“cssSelectorOfElement”)
By.xpath(“//xpath/To/Element”)
By.name(“elementName”)
API - Operations
void click() - click on an element
void submit() - perform a submit
String getValue() – returns value set in the element
void sendKeys(“keysToSend”) - input values
void clear() - clear the input field
String getElementName() – returns value of Name of the element
String getAttriubute() – returns value of specified attribute of the element
void Actions() - perform mouse, drag and drops and keyboard operations www.time2test.co.uk !
API - windows and frames working with Browser
Windows
driver.getWindowHandles() driver.switchTo().window.(“window_name”)
Working with frames • driver.switchTo().frame.(“frame_name”)
Finding Dynamic Elements
Does the ID of your element dynamically change?
<p id="bootcamp_dynamic_1234">This p tag has a dynamic id</p>
Xpath notation to find the p tag on the page "//p[contains(@id,'bootcamp_dynamic_')]"
12
Locator Strategies
ID webDriver.findElement(By.id("logo"));
Name webDriver.findElement(By.name("q"));
Tag Name webDriver.findElement(By.tagName("H1"));
Class name webDriver.findElements(By.className("sponsor_logos"));
XPath webDriver.findElement(By.xpath("//section[@id=‘miniconfs’]/a[2]"));
Link Text webDriver.findElements(By.linkText("About"));
Partial Link Text webDriver.findElement(By.partialLinkText("visitcanberra"));
Sample Codes
Clicking Button/Link/CheckBox
Type in Textbox
driver.findElement(By.id("submitButton")).click();
driver.findElement(By.name("fname")).sendKeys("My First Name");
Sample Codes
Selecting from Drop Down/Radio button
<select id="44"> <option value="1">xyz</option> <option value="2">abc</option> <option value="3">pqr</option> </select>
WebElement e = driver.findElement(By.id("44")); Select selectElement=new Select(e); // both of the below statements will select first option in the weblist selectElement.selectByVisibleText("xyz"); selectElement.selectByValue("1");
Sample Codes
Store text of targeted element
Get page title
String dropdown = driver.findElement(By.tagName("select")).getText();
driver.getTitle();
17
Let’s code
Try the commands seen on your application under test using Selenium, Java, TestNG and Eclipse
Eclipse-Java-Selenium environment setup Reference - http://qastuff.blogspot.in/2012/03/setting-up-selenium-web-driver-eclipse.html
18
Page Interactions
webElement.click()
webElement.sendKeys(...)
webElement.submit()
Actions class -> Mouse Events / Drag and Drop
Advanced: Waits in Webdriver
Waiting is having the automated task execution elapse a certain amount of time before continuing with the next step.
Worst way of waiting is Thread.sleep();
Advanced Example: Implicit Wait
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
WebDriver webdriver = new FirefoxDriver();webdriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);webdriver.get("http://somedomain/url_that_delays_loading");WebElement myDynamicElement = webdriver.findElement(By.id("myDynamicElement"));
Advanced Example: Explicit Wait
An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code.
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
Some conditions for Explicit wait
ExpectedConditions.elementToBeClickable(locator);
ExpectedConditions.invisibilityOfElementLocated(locator);
ExpectedConditions.alertIsPresent();
ExpectedConditions.presenceOfElementLocated(locator);
ExpectedConditions.textToBePresentInElement(locator, text);
ExpectedConditions.visibilityOf(element);
Advanced Examples
Capture Screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("D:\\screenshot.jpg"));
Best Practice
To use implicit wait and explicit wait together
Define implicit wait for driver after initialization
This will define default implicit wait time frame for all findElements
At the special places requiring more time, use explicit wait
25
Additional useful commands
Polling the DOM for N seconds webDriver.manage().timeouts().implicitlyWait(30,
TimeUnit.SECONDS);
Testing CSS properties webElement.getCssValue(“height”);
Javascript execution JavascriptExecutor js = (JavascriptExecutor) webDriver;
Long value = (Long) js.executeScript("return window.scrollY");
Navigation
webDriver.navigate().back(); webDriver.navigate().forward(); webDriver.navigate().to(“url”);
26
Assignment
Fill the form of registration on a test website of your choice.
Run it on different browsers.
Try to use waits.
27
Pop-up/Alert handling
Pop up window
driver.switchTo().window(windowHandle);
Alerts
alert = driver.switchTo().alert();
alert.Accept();
alert.Dismiss();
Sample code Reference - http://qastuff.blogspot.in/2012/05/switch-to-window-smart-way.html
28
Backward Compatibility with Selenium 1
Selenium selenium = new WebDriverBackedSelenium(webDriver, “http://osdc.com.au”);
selenium.open("http://osdc.com.au");
selenium.click("id=follow_twitter");
selenium.waitForPageToLoad("10000");
WebDriver webDriver = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();
Dipesh BhatewaraTest Automation and Agile enthusiast.LinkedIn -https://in.linkedin.com/pub/dipesh-bhatewara/2/498/612/en
Blog - http://qastuff.blogspot.in/, www.digitalinfobytes.com
Email - [email protected]