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