Web driver training

Dipesh Bhatewara
Dipesh BhatewaraStaff Engineer à VMware
WebDriver Training
Trainer: Dipesh Bhatewara
www.digitalinfobytes.com
History
2
2004 2007 2009 2011
Selenium 1 WebDriver
Selenium2
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
3
Selenium Server in Selenium2
 Replicate Selenium RC functionalities
 Remote WebDriver
 Selenium Grid 2
4
WebDriver
WebDriver Interface
Firefox
Driver
Internet
Explorer
Driver
Chrome
Driver
HTML
Unit
Driver
Opera
Driver
Andriod
Driver
iPhone
Driver
5
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_')]"
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"));
12
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();
Sample Code for Demonstration
16
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
17
Page Interactions
 webElement.click()
 webElement.sendKeys(...)
 webElement.submit()
 Actions class -> Mouse Events / Drag and Drop
18
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("myDynamicE
lement")));
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
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”); 25
Assignment
 Fill the form of registration on a test website of your choice.
 Run it on different browsers.
 Try to use waits.
26
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
27
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(); 28
Dipesh Bhatewara
Test 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 - dipesh.bhatewara@outlook.com
1 sur 29

Contenu connexe

Web driver training

  • 1. WebDriver Training Trainer: Dipesh Bhatewara www.digitalinfobytes.com
  • 2. History 2 2004 2007 2009 2011 Selenium 1 WebDriver Selenium2
  • 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 3
  • 4. Selenium Server in Selenium2  Replicate Selenium RC functionalities  Remote WebDriver  Selenium Grid 2 4
  • 6. API – Must Know  Webdriver – Control Browser  Webdriver driver = new FirefoxDriver();  WebElement – works with elements on page  WebElement username = driver.findElement(By.id(“user”));
  • 7. 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)
  • 8. 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”)
  • 9. 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 !
  • 10. API - windows and frames working with Browser  Windows  driver.getWindowHandles()  driver.switchTo().window.(“window_name”)  Working with frames • driver.switchTo().frame.(“frame_name”)
  • 11. 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")); 12
  • 13. Sample Codes  Clicking Button/Link/CheckBox  Type in Textbox driver.findElement(By.id("submitButton")). click(); driver.findElement(By.name("fname")).sendKeys ("My First Name");
  • 14. 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");
  • 15. Sample Codes  Store text of targeted element  Get page title String dropdown = driver.findElement(By.tagName("select")).getText(); driver.getTitle();
  • 16. Sample Code for Demonstration 16
  • 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 17
  • 18. Page Interactions  webElement.click()  webElement.sendKeys(...)  webElement.submit()  Actions class -> Mouse Events / Drag and Drop 18
  • 19. 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();
  • 20. 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"));
  • 21. 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("myDynamicE lement"))); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
  • 22. Some conditions for Explicit wait  ExpectedConditions.elementToBeClickable(locator);  ExpectedConditions.invisibilityOfElementLocated(locator);  ExpectedConditions.alertIsPresent();  ExpectedConditions.presenceOfElementLocated(locator);  ExpectedConditions.textToBePresentInElement(locator, text);  ExpectedConditions.visibilityOf(element);
  • 23. Advanced Examples  Capture Screenshot File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType. FILE); FileUtils.copyFile(screenshot, new File("D:screenshot.jpg"));
  • 24. 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”); 25
  • 26. Assignment  Fill the form of registration on a test website of your choice.  Run it on different browsers.  Try to use waits. 26
  • 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 27
  • 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(); 28
  • 29. Dipesh Bhatewara Test 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 - dipesh.bhatewara@outlook.com