Poly Driver: A Phantom Band-Aid for Geb

The Problem:

You have an extensive suite of Geb functional tests running against Firefox and it takes a long time to run. You switch your tests over to PhantomJS; now they run twice as fast!

But five percent of the tests intermittently fail. After spending a few hours trying to figure it out, you still don’t know why they are failing — just that they fail sometimes, and you don’t want to spend the next week trying to figure out a workaround. As it stands, it’s not practical to switch to PhantomJS at this time.

A Solution:

Why not use both? Add the Poly Driver plugin to your project and configure it in GebConfig.groovy:

driver = {

   FirefoxDriver firefoxDriver = new FirefoxDriver()
   PhantomJSDriver phantomJSDriver = getPhantomDriver()

   new PolyDriver(mainDriver: phantomJSDriver, alternateDrivers: ['firefox' : firefoxDriver])
}

private PhantomJSDriver getPhantomDriver() {
   // see Tomás Lin’s excellent blog post on configuring drivers for Geb:
   // http://fbflex.wordpress.com/2013/03/18/how-to-configure-webdriver-in-grails-for-your-geb-tests/
}

Change the class inheritance of your problem test and annotate it:

class MyGebSpecCase extends GebReportingSpec {
  ...
}

becomes

import com.polydriver.spec.PolyDriverGebReportingSpec

@PreferredDriver('firefox')
class MyGebSpecCase extends PolyDriverGebReportingSpec {
  ...
}

When you run your tests, you will see a Firefox window open but only the FooGebSpecCase (and any others with the @PreferredDriver(‘firefox’) annotation) will use it. The rest of your tests will run quickly in PhantomJS.

The driver allows you to specify as many drivers as you want — you can even declare multiple instances of the same browser, so if you want to run a subset of tests against different Firefox profiles, you can do that:

driver = {
   FirefoxDriver firefoxDriver = new FirefoxDriver()
   FirefoxProfile ffProfile = new FirefoxProfile()
   // setup the other profile
   FirefoxDriver altFirefoxDriver = new FirefoxDriver(ffProfile)
   PhantomJSDriver phantomJSDriver = getPhantomDriver()

   new PolyDriver(
         mainDriver:       phantomJSDriver, 
         alternateDrivers: [
               'firefox':  firefoxDriver, 
               'ff-alt':   altFirefoxDrive
         ]
   )
}

Hopefully this eases the pain of transitioning your test suites to PhantomJS.

About the Author

Object Partners profile.
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
An Exploration in Rust: Musings From a Java/C++ Developer
Why Rust? It’s fast (runtime performance) It’s small (binary size) It’s safe (no memory leaks) It’s modern (build system, language features, etc) When Is It Worth It? Embedded systems (where it is implied that interpreted […]
Getting Started with CSS Container Queries
For as long as I’ve been working full-time on the front-end, I’ve heard about the promise of container queries and their potential to solve the majority of our responsive web design needs. And, for as […]
Simple improvements to making decisions in teams
Software development teams need to make a lot of decisions. Functional requirements, non-functional requirements, user experience, API contracts, tech stack, architecture, database schemas, cloud providers, deployment strategy, test strategy, security, and the list goes on. […]
JavaScript Bundle Optimization – Polyfills
If you are lucky enough to only support a small subset of browsers (for example, you are targeting a controlled set of users), feel free to move along. However, if your website is open to […]