9p. WebDriver – Timesaver/Shortcut: Locator validation

Here comes today’s icing on the cake: ‘a shortcut to validate if the chosen locator strategy uniquely identifies the web element under test’.

Imagine you have a test script that is dependent on a lot of factors. And you are asked to make some additions to it as new web elements are to be tested. In order to locate each element, any of the locator strategies discussed in the previous posts (by Id, name, tagName, className, linkText, cssSelector, xpath etc.) can be used. To check if your code works as expected and the web element is located accurately, you will have to run the entire test case time and again. What a pain!

Who doesn’t appreciate a shortcut or a timesaver in this situation! I hear you.

It is time to revisit our all time leader of Magic Meadows, “Selenium IDE” and harness the power of  ‘Target’ and ‘Find’ features in the “Command/Target/Value Editor” to our advantage.

Example:

Let us locate the ‘Your current email address’ text box on the Gmail account sign up page using cssSelector (tag and name attribute).

Right click on the ‘Your current email address’ text box and select inspect element to get the corresponding HTML code which is as below,

<input name="RecoveryEmailAddress" id="RecoveryEmailAddress" 
value="" spellcheck="false" style="background-color: rgb(255, 255, 255);" 
type="text">

We can see that the ‘input’ tag has the ‘name’ attribute as ‘RecoveryEmailAddress’. The image below shows the steps followed to validate our locator.

Shortcut

  1. Enter “css=input[name=’RecoveryEmailAddress’]” as Target. To understand how this command is formed, check out 7l. Selenium IDE – Locating Elements Contd. (by CSS, DOM, XPath)
  2. Click ‘Find’
  3. ‘Your current email address’ text box is highlighted as expected on the Gmail account Sign up page.

This shows that our locator uniquely identifies the required element and we can directly use it in our code with so much more assurance. This method saves a huge amount of time. Mission accomplished!

Easy peasy lemon squeezy! Isn’t it?

See you again in another post. Have a great day ahead!

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.