“When” and “Why” about Locators

 Generally when we are writing our automation test in Selenium or any other driver , the first thing we tend to do is start looking for object Id. Thought we have multiple ways of getting the hold of the object on a webpage like:
  • Id
  • name
  • Link text
  • XPath
  • DOM
Id or name is the most simplest and easy to use locators. Advantages with them is it increases the readability of your test code. It’s also better than other locators in terms of test performance. If you are using lot of Id’s and your test code is becoming too clumsy then one suggested way of using Id’s would be to have a separate file and then probably you can rename them if they are not properly named in the page source (Google page “q” as their search box Id).
Link Text is mostly used with links and limited to that. They are easy to use. However they are a little problematic to maintain because of often changing link texts.
Next is the famous XPath, simple to use (if you are using Firebug or any other add-in  to get it) but makes your test code look ugly. XPath should generally be used when the object is having neither Id nor name. Problem with XPath is when we run the test, browser runs it XPath processor to check if it can find any object, which actually impacts the performance. One important thing which we tend to forget while using XPath is, it ensures the order of elements. So it should be used to verify some object with respect to certain other object. One example could be to verify if “Today’s Deal” appears with in the header section of the Amazon home page.
DOM I haven’t used much so can’t comment on that.


Popular posts from this blog

The 3 qualities that will always help you become better at almost anything…

Test Case Paths : Happy, Sad & Bad

How to install Android Emulator on Windows