Wrappers for Selenium methods GOOD or BAD?

1,554 views
Skip to first unread message

Lazar Jojic

unread,
Nov 18, 2016, 11:47:15 PM11/18/16
to Selenium Users
I would like to open this as a form of discussion. For me right now the biggest issue and the thing that I really don't like in Selenium is dealing with those wrong states of web elements when we want to do some actions on them. There are many of these states in which web element can be: not clickable, disabled, not displayed, not visible...If we now try to do some action on them (click, type text, check checkbox...) we can get a bunch of exceptions. In order to stop this we must do some checks. Selenium has huge number of methods for checking whether element is prepared to receive some action. Some of those methods are isElementDisplayed, isElementEnabled, isElementVisible (lets call them "state checks")... If element is not yet in desired state then we must include waits, and again there are a bunch of them. So we have many states of elements, many actions, many state checks and many waits. When we combine those our tests can be overwhelmed with code which only wants to do simple action (e.g. click). If the page is very dynamic then this is big problem. So for a long period of time I was trying to avoid all of these with wrappers for selenium methods (e.g. click()). But in order to do this prior to doing the click() I need to go through many "state checks" and then through corresponding types of waits". And I need to do the same thing for every action. So I created method isElementCool() and I would call it in every wrapper method before specific action. This works in most of the cases but time to time I realize I missed some check so I need to add it and this is hard to do. Another thing that is creating additional complication is that some of wrapper methods are calling other wrapper methods so isElementCool() is being executed two times.

I saw that this guy advices to use wrappers.


But his wrapper method for click is too weak. There are things that can happen and it will not do well.

What are your experiences and thoughts on this matter?

P.S. I am really not satisfied how this problem is handled in Selenium. I am having some feeling that this could be done better.

Jim Evans

unread,
Nov 19, 2016, 7:03:30 AM11/19/16
to Selenium Users
So-called "wrapper methods" are exactly the right way to approach this scenario. The Selenium WebDriver API is designed to be a building block for more sophisticated libraries. It isn't, and was never meant to be, the end-all, be-all approach to automating every type of web page, for all users. The web is simply too dynamic, with too many complex states to be handled perfectly by a single library. The project does, however, give you the primitives that you can use to build your own solution that perfectly fits your specific needs.

cjayswal

unread,
Nov 19, 2016, 1:03:47 PM11/19/16
to Selenium Users
I agree with @Jim Evans, selenium or webdriver is api that enables you to interact with browser then its up to user how they use it to build solution to satisfy specific needs. There are few frameworks also available for webdriver that provides different design concepts to reduce development as well as maintenance efforts.

You can use QMetry Automation Framework framework built for selenium, which allows you to stick with webdriver and still customize your own way. For example, it has QAFExtendedWebElement that takes care of element presence or stale reference before any action on element. You can write element listeners for different purpose. You also can create custom component as per your need.

शनिवार, 19 नवंबर 2016 को 10:17:15 पूर्व UTC+5:30 को, Lazar Jojic ने लिखा:

Lazar Jojic

unread,
Dec 7, 2016, 3:16:18 PM12/7/16
to Selenium Users

I have been trying wrappers for one year and eventually got tired and bored. There are mainly two reasons.
1. I could not make wrapper that is working fine all the time, because as I said in the first post there can be number of things that can happen to element. So even if I think that I finally made wrapper for click method that is perfect, it will work for some time but I will eventually hit some element that is failing the click. Now I need again to repair wrapper and it gets bigger and more ugly every time.
2. They are too generic. For every element that I want to perform click, I will do number of checks that are not necessary. 

Currently I am trying to work without the wrappers and will see how things go. I am handing waits in Page Object classes and every method is very specific. For one button in some PO class, click method will be very simple, maybe without any waits, just driver.click(). But for some buttons click method would have waits, and what waits I'm using is specific to this button and the behaviour of the page.
I will continue in this manner for some time and will compare to wrappers style.
 

Anish Pillai

unread,
Dec 7, 2016, 7:33:40 PM12/7/16
to Selenium Users
I have always used wrappers but in a very basic manner, just for logging purpose. I have always found it useful to figure out issues by checking out the logs. Regarding the wrappers being too generic, I tend to not have all the checks for all the places where I want to do clicks. My approach is to create different functions for specific scenarios. For example, I might have two click wrapper methods - one generic which would work on most of the object and then a specific one with lots of checks for those cases where I keep finding issues while clicking.

Lazar Jojic

unread,
Mar 14, 2017, 11:23:13 AM3/14/17
to Selenium Users
After this time while I was avoiding wrappers I found it much better to do. Currently I don't use them at all. But I guess Anish you are right about using them in some way. And not to make them too generic.
My first Selenium mentor was trying (without the success) to make Superman wrappers, so he pointed me in wrong direction. 

Reply all
Reply to author
Forward
0 new messages