Running Geb specs using a separate driver profile to test mobile views in Grails

We’re currently building mobile views for our grails application using jQuery mobile and the Spring mobile plugin. As such, we wanted to add a set of mobile tests in Geb to run in parallel with our application’s functional tests.

In this post, I show how you can set up a separate webdriver profile for a subset of your spock/geb functional tests. 

In order to specify a separate driver profile ( we’re setting an iPhone user agent in our tests ), we needed a way to mark the new tests we’re creating to use this separate profile.

Marcin Erdman suggested in the Geb mailing list that it is possible to use the Geb Configuration mechanism to accomplish this, but you would end up creating a new driver for each test, which we wanted to avoid.

After a little bit of tweaking, we came up with the following base class for our tests to extend.

import geb.spock.GebSpec
import spock.lang.Shared
import org.openqa.selenium.firefox.FirefoxProfile
import org.openqa.selenium.firefox.FirefoxDriver

class MobileSpeck extends GebSpec {

@Shared def cachedDriver

def setupSpec(){
  // create a new firefox profile with an iPhone user agent and cache it

  FirefoxProfile profile = new FirefoxProfile();
  profile.setPreference("general.useragent.override", 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.1');
  cachedDriver = new FirefoxDriver( profile )

def setup(){
  // assign this as the default driver on the browser for each test
  browser.driver = cachedDriver

def cleanupSpec(){
  // after running the spec, kill the driver


( Note, this code is for 1.3.7, you might need to change GebSpec to use Specification if you are using grails 2.0. )

Basically, we create a new firefox driver with the desired profile and cache it in memory.

When a new spec is ran extending this speck ( we use the base class speck so that spock won’t try to run it as a test ), it will be assigned the new cachedDriver and use it instead of the default Geb driver.

After adding this Speck, we are able to specify a test by just extending the class. I.e,

class iPhoneUserLoginSpec extends MobileSpeck {



9 thoughts on “Running Geb specs using a separate driver profile to test mobile views in Grails

  1. rstudnerRoger

    Time to ask the semi-unrelated question

    What is this “@Shared” annotation.. The Googles aren’t helping and the suspense is killing me 🙂

  2. Pingback: Plugins Grails | Pearltrees

  3. Pingback: » Blog Archive

  4. Pingback: An Army of Solipsists » Blog Archive » This Week in Grails (2012-03)

  5. Pingback: A Smattering of Selenium #76 « Official Selenium Blog

  6. Pingback: Adding Mobile Views to your Grails Applications with JQuery Mobile: A Real Life Example | iPhone Developers

  7. Pingback: Mobile web testing on your iPhone or iPad with Geb and Spock | Object Partners Inc

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s