Object Identification In Selenium

Object Identification In Selenium:

Object identification is a very crucial part of any automation effort be it in QTP, selenium or any other tool for that matter. So it makes it very important for each one of the automation folks around to be able to identify the objects used in the automation effort

Identifying Objects is Selenium (WebDriver) is pretty simple if you are working on an application built on Firefox, but it gets very tricky if you are working on IE application. So it would be best to master the object identification techniques.

If you have used the Selenium IDE for any recording you would have noticed that there are multiple ways of identifying an object. By default the Selenium IDE tries to recognize the object by the ID or name attribute. Selenium IDE identifies the objects by the attributes mentioned in the order below

Object identification is very important part of automation activity. So the automation people should be able to identify the objects for automation purpose.

For identifying these objects Selenium uses a concept of ‘Locators’. These locators are used to identify the objects which are mostly used while automating the test cases using Selenium.

Locators: There are number of locators present. These are among them.

  •  ID
  •  Name
  •  Link Text
  •  CSS Selector
    Tag and ID
    Tag and class
    Tag and attribute
    Tag, class, and attribute
    Inner text
  •   DOM (Document Object Model)
    dom: index
  •  XPath

For testing different applications i.e. Application Under Test (AUT), we need to identify the object it is made of. All applications either it is Web Application or Desktop Application; it is consisting of different objects like below:

  • Button
  • Text Box
  • Link
  • Drop Down
  • Radio Button
  • List Box
  • Slider

Finding an element and confirming the expected result requires locators. Through locator, In Document Object Mode (DOM) i.e. webpage, we uniquely identify the object. The ‘By‘ class is used in WebDriver to locate the elements.

o identify the objects such as Links, Buttons, Edit boxes, Drop downs, etc on the application Selenium uses a concept called “Locators”. There are around 8 different types of locators. Out of them, I will be explaining only four as they are widely used while automating the test cases using Selenium.

1. id = id – By default Selenium IDE takes “id” as Locator Every Web Page will be having some HTML Source code. This can be viewed using “View –> Page Source / View source” on the browser. In the following picture we can see “id” attribute of a HTML tag is used as a locator to identify an object.

2. name = name (Second locator) We will do the following steps to understand how exactly Selenium identifies the objects

3. xpath = xpathExpression. Xpath is used as a Locator to identify an object. This is an expression which is formed by combining various HTML tags. For example: //a[contains(text(),’VRL travels’)], //div[@id=’shortcuts’]/span/span[2]/a/span

4. link=link text (in HTML source we can located this using “href” tag)

Here XPath and CSS are the most widely used locators.

It is very easy to identify the object of the application which is built on Firefox but same on the other way if that application is made on IE browser then it is pretty difficult to identify the object.

There are different tools available to identify the objects on different browsers like Firefox/Chrome/IE etc.

If we are using Selenium IDE there are different ways to identifying the objects. By default Selenium identify the object using ‘ID’ or ‘Name’.

Here I have taken the example of ‘Flipkart’ home page. Here for search text box on Flipkart home page the recording is done through Selenium IDE. We can see here the order of identification of text box via different locators as above.

We can see the code of above recording. For each locator the code will look like as below:

1. driver.findElement(By.id(“fk-top-search-box”)).click();
2 driver.findElement(By.name(“q”)).click();
3. driver.findElement(By.cssSelector(“#fk-top-search-box”)).click();
4. driver.findElement(By.xpath(“//input[@id=’fk-top-search-box’]”)).click();
5. driver.findElement(By.xpath(“//form[@id=’fk-header-search-form’]/div/div/div/input”)).click();
6. driver.findElement(By.xpath(“//form/div/div/div/input”)).click();

6 Tools to Help You with Object Recognition

Using one the six tools

  1. Firefox – Selenium IDE
  2. Firefox – Firebug
  3. Firefox – Firepath
  4. Google Chrome Developer Tools
  5. IE Developer tool bar
  6. fire-ie-selenium
  7. XPath Checker
  8. Fire Finder