1.

What does Locator mean? Name different types of locators.

Answer»

Id Locator :

The ID locator SEARCHES for an element having an id attribute corresponding to the specified pattern on the page.

Syntax is - driver.findElement(By.id("id value"));

PROS:

  • Each id is expected to be unique, so no chance of matching several elements

CONS:

  • Works on elements with fixed ids and not generated ones

Name Locator :

The Name locator searches for an element having a name attribute corresponding to the specified pattern on the page. You can specify a filter to refine your locator.

Syntax is - driver.findElement(By.name("name-value"));

PROS:

  • Fits well with a fixed list of similar elements

CONS:

  • It is difficult to use with data-bound lists

Link Locator:

The link locator is intended to select links only and selects the anchor element containing the specified text: link=The text of the link

Syntax is - driver.findElement(By.linkText("link text"));

PROS:

  • Selects only anchor elements
  • Useful in testing navigation

CONS:

  • You should know the text of the link in advance

DOM Locator :

The DOM locator works by locating elements that match the javascript expression referring to an element in the DOM.

Locating by getElementById

Syntax is - document.getElementById("id of the element");

Locating by getElementsByName

Syntax is - document.getElementsByName("name")[index];

Locating by DOM - dom: name

Syntax is - document.forms["name of the form"].elements["name of the element"]

  • name of the form = the value of the name attribute of the element you want to access of the form tag
  • name of the element = the value of the name attribute of the element you want to access

Locating by DOM - dom: index

Syntax is - document.forms[index of the form].elements[index of the element]

  • index of the form = With respect to the whole page, the index number (starts at 0) of the form
  • index of the element = With respect to the whole form, the index number (starts at 0) of the element

PROS:

  • Javascript allows building dynamic locators

CONS:

  • Depends on the structure of the page

XPath Locator:

XPath is the navigation tool for XML. XPath can be USED everywhere where there is XML. Xpath Locate element by using an XPath expression.

Syntax is -  

//button[@value="value text"];  //div[@id="id value"]/button[0];

PROS:

  • Very precise locators

CONS:

  • Slower compared to CSS
  • Depends on the browser’s XPath implementation which is not always complete and not recommended for cross-browser testing

CSS Locator

To find the elements on the page, the CSS locator uses CSS selectors.

Syntax is - div[id="id value"] > button[value="value text"];

selects the button with its value property set at value text if children of the id value are div

PROS:

  • Faster than XPath
  • good balance between structure and attributes
  • Allows selection of elements by their SURROUNDING context

CONS:

  • More complex and require a steeper learning curve

Structure-Dependent Or Not?

Locators are classified into two categories ie Structure-based locators and Attributes-based locators.

  • Locators that depends on the structure of the page to find elements are structured based locator. Below locators are structure-based locators.
    • XPath
    • DOM
    • CSS
  • Locators that depend on the attributes of the elements to locate are Attribute-based locators. Below locators are the Attribute-based locators.
    • Identifier
    • Id
    • Name
    • Link
    • CSS
  • Consider all these points pertaining to CSS before CHOOSING a locator strategy
  • CSS is used widely because it is flexible and gives a good balance between using structure and attributes to find the elements.


Discussion

No Comment Found