Validate The Validation You Do – isEnabled() & isSelected() Methods in Selenium WebDriver

Front end Automation testing is not only about Navigating/Clicking/Typing here and there. It is more about validation so that it could catch real bugs. 

If we use Selenium WebDriver for front end automation, it provides methods to do some validation like isEnabled(), isDisplayed() , isSelected() etc. We must need to understand the correct usage of methods before using it and if it is serving actual purpose.

I want to give an example here of how people use wrong method to validate something. Let’s consider below scenario:

  1. Navigate to “http://makeseleniumeasy.com“.
  2. Verify “Home” tab is active.
  3. Click on “API Testing”.
  4. Verify “Home” tab is not active but “API Testing” is active now.

Above scenario is similar to a real time scenario whose automation script I saw and got an idea of writing this post. You may be thinking its a very simple scenario. We just need to call isEnabled() method to get if a particular tab is active or inactive. Is it a correct validation? Actually NO.

“isEnaled()” method does not work in above way. Selenium javadoc defines it as below:

Is the element currently enabled or not? This will generally return true for everything but disabled input elements. “. This method checks for “disable” attribute in web elements like input elements, buttons, textareas, Select etc. A link (An anchor tag ‘a’) can not have “disable” property. So calling isEnabled() method to check if a link in enabled or disabled is not correct.

Reference:

https://github.com/SeleniumHQ/selenium/issues/1377

https://github.com/SeleniumHQ/selenium/issues/1348

Now you can think of using isSelected() method. Will take do the correct validation of above scenario? Let’s see.

Selenium javadoc defines isSelected() method as below:

“Determine whether or not this element is selected or not. This operation only applies to input elements such as checkboxes, options in a select and radio buttons. For more information on which elements this method supports, refer to the specification. “

You should understand now that you can not use isEnabled() or isSelected() method to check if a link is active or inactive or selected.

How to do above validation then?

We need to observe the difference in DOM when tab is active or inactive or selected. For example:- See below images:-

when Home tab is selected, its class attribute contains a value named ‘current-menu-item’.



When I selected other tab and Home tab is no more selected, its class attribute does not contain ‘current-menu-item’.

Now we can drive the logic here. We just need to get class attribute and verify if it contains ‘current-menu-item’ if it is Active. In case of inactive, it should not be available.

We will take another example of different site here:-


Selenium Code:-

Hope you learnt a new thing and will be careful when using these types of scenario. And also hope that you know where to use isEnabled() and isSelected() methods.

In case of any query, doubt kindly comment. If you like my posts, please share to reach maximum.

#ThanksForReading

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

1 thought on “Validate The Validation You Do – isEnabled() & isSelected() Methods in Selenium WebDriver

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.
%d bloggers like this: