Wrapper methods in selenium framework’s

Jayakrishnan Kunnath
1 min readOct 28, 2021

Users of Selenium WebDriver might have come across the problem that when they’re using Selenium for testing responsive, dynamic web applications, timing and synchronization can be a major hurdle in creating useful and reliable automated tests.

Using the standard Selenium methods such as click() and sendKeys() directly in my script often results in failures as a result of the page element being invisible, disabled or reloaded (this last example resulting in a StaleElementReferenceException).

Solution : Wrapper methods

The solution to this problem lies in using wrapper methods for the standard Selenium methods. So instead of doing this every time I need to perform a click:

(new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(By.id(“loginButton”)));driver.findElement(By.id(“loginButton”)).click();

wrapper method click(), look like this

public static void click(WebDriver driver, By by) {(new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(by));driver.findElement(by).click();

}

Benefits of wrapper methods :

Extending your wrapper methods: error handling

Using wrapper methods for Selenium calls has the additional benefit of making error handling much more generic as well

Extending your wrapper methods: logging

Your wrapper methods also allow for better logging capabilities.

More readability of the code and real encapsulation of the framework

Here is some of the wrapper’s, written recently, Hope these helps someone!!! Do share if you find it useful or comment if you find anything new.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response