Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

Saturday, February 27, 2016

Locators in selenium webdriver

Lets understand locators by asking WH questions.

Locators in selenium
Click on the image to enlarge it
What are locators ?
  1. Locators are the lifeblood of the tests.
  2. Locators are different strategies for locating elements on a page
  3. Locator can be termed as an address that identifies a web element uniquely within the webpage.
  4. Locators are the HTML properties of a web element. 

When and Why they are required ? 
  1. To tell the Selenium about the web element it need to perform action on
  2. To tell Selenium which GUI elements ( say Text Box, Buttons, Check Boxes etc) its needs to operate on.
  3. To get information of an element

Who are they ?
  • Id 
  • Name
  • Linktext
  • Partial Linktext
  • Tag Name
  • Class name
  • Css selectors
    • selectors are patterns used to select the element(s) you want to style
    • is the part of a CSS rule set that actually selects the content you want to style 
  • XPath expression
    • document is parsed into a tree structure (DOM tree)
    • for selecting nodes from a document
    • is used to navigate through elements and attributes in a document.
Where are they in selenium web driver? 
  • abstract class By inside the package org.openqa.selenium
  • subclasses of By and interfaces
How to use locators, to find elements with different locator strategies ? 
  • Using developer tools available in browsers
  • Right-click --> Inspect Element or Inspect (Firefox / Crome)
  • Using browser add-on like this 
  • Example: 
    • <input name="register" class="required" type="text"/>
    • WebElement register= driver.findElement(By.name("register"));
 Which locator strategy to use?
  • There is no perfect locator
  • If you’re smart about your locators you can reduce the cost of maintenance
References
  1. http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/By.html
  2. http://www.seleniumeasy.com/selenium-tutorials/selenium-locators
  3. http://toolsqa.com/selenium-webdriver/locators/
  4. http://www.guru99.com/locators-in-selenium-ide.html