Thursday 18 July 2013

Highlighting a Web Element in Selenium

Highlighting web elements comes in very handy when we are doing validations on web elements using selenium.It is also useful for debugging purposes when webdriver actually selects the element and we dont want to go in and use Selenium IDE each and everytime to visually find the element.

Link to the code : 
https://github.com/Madusudanan/Selenium/blob/master/WebElementHighlighter.java

The above code is kind of raw and is suitable if you are implementing it in a selector method in a framework of your own.But there is another way of doing this with the help of AbstractWebDriverEventLister.This is a utility class that was developed by the selenium folks that can be used for logging and other purposes.

AbstractWebDriverEventListener has a problem,it does not have listener methods for isDisplayed(), where we can verify if some text is displayed on the page,we cannot use click() here since it is just text.We can overcome this by putting our highlighter code inside the beforeFindBy method of the EventListener, so that all element FindBy's trigger the highlighter listener method.

I though this is the best way to implement it,but I would definitely welcome if there are better ways of implementing it inside the EventListener class,if you come across any,do let me know.

Link to the EventListener implementation code :
https://github.com/Madusudanan/Selenium/blob/master/WebElementHighlighterAsEventListener.java



Code in action : 



Please let me know in case of any issues you face with the code.