Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Reusing browser instance in Geb UI tests

DZone's Guide to

Reusing browser instance in Geb UI tests

· Performance Zone
Free Resource

Transform incident management with machine learning and analytics to help you maintain optimal performance and availability while keeping pace with the growing demands of digital business with this eBook, brought to you in partnership with BMC.

Geb is next-generation UI testing library that frees developer from dealing with Selenium API which is in many cases not so friendly. The most important feature of Geb is its support, or I would even say, strong encouragement to use Page Objects in your UI tests. Page Objects are also present in Selenium, but they are not as intuitive and east to use as in Geb.
But enough about Geb itself. Today I will share with you simple trick how to share one browser instance when running many Geb UI tests. This will significantly reduce time needed to execute UI tests and also reduce memory usage on test machines.

The Problem

Assume we have many test classes (spefications) similar to this one below.

class ExampleUITest extends GebSpec {
 
    def "Should test feature 1"() {
        given:
        to LoginPage
 
        when:
        loginWith("login", "password")
 
        then:
        at HomePage
        assert loggedUserLabel == "loggedUser"
    }
 
    def "Should test feature 2"() {
        given:
        to LoginPage
 
        when:
        loginWith("login", "password")
 
        then:
        at HomePage
        assert loggedUserLabel == "loggedUser"
    }
    
    // and so on...

What we have to do is prepare browser instance (in our case Firefox one) to click through our tests. We could to it in setup() method:

class ExampleUITest extends GebSpec {
 
    def setup() {
        driver = new FirefoxDriver()
    }
 
    def "Should test feature 1"() {
        // ...
    } 
 
    def cleanup() {
        driver.quit()
    }

This approach works but it has one, big drawback. We all know that UI tests tend to last quite long and creating new browser instance for each, yes, each test method will only make this situation worse as it also takes some time to start browser on your testing server.

Second approach

We also could reuse same browser in each specification (each Geb class) by using setupSpec() and cleanupSpec() methods instead. This will reduce number of new instances created but still we will have many of them, especially in large codebase with many test classes.

Solution

My solution to this issue takes from both approaches presented above. But instead of creating browser for each specification, I am reusing one created in the first executed class.

To make it work we need to introduce common abstract class for all our UI tests:

class IntelliGebSpec extends GebSpec {
 
    static def cachedDriver // static variable will store our single driver instance
 
    def setupSpec() {
        if ( cachedDriver == null) { // in first Specification this will be true
            cachedDriver = new FirefoxDriver()  // so let's create driver and put it in cachedDriver variable
        }
    }
 
    def setup() {
        driver = cachedDriver   // each test should use our cached browser instance
    }
 
    // as we are reusing browser, we should logout from our application after
    // each test to have clean browser state on each test run
    def cleanup() { 
        if ($(id: "logout").isDisplayed()) {
            $(id: "logout").click()
            waitFor { $(id: "inputName").isDisplayed() }
        }
        driver.manage().deleteAllCookies()
    }
}

Summary

In this post I have shown my home made solution to reusing browser in multiple tests. I strongly suggest all of you to check and try Geb, as after over a year of using Selenium I am really impressed by this library. Easy to use with really cool Page Objects.




Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

Topics:

Published at DZone with permission of Tomasz Dziurko, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}