[cuke4duke] How to best open and close Selenium from Cucumber

933 views
Skip to first unread message

Robert

unread,
Dec 12, 2010, 10:33:07 PM12/12/10
to cu...@googlegroups.com
We are currently using cucumber, cuke4duke, and selenium. Our implementation uses a Hooks class with a Before and After annotation to open and close Selenium, respectively.  

public class Hooks {

@Before
public boolean setupTestEnvironment() {
return TestEnvironment.loadTestEnvironment();
}
@After
public boolean teardownTestEnvironment() {
return TestEnvironment.unloadTestEnvironment();
}
}

This solution, however, strikes me as a bit of a "hack" -- it starts and stops Selenium for each scenario, which can add additional time to our tests due to having to reopen a new instance of WebDriver.  A recent to post to this group (at_exit hook?), discussed the use of env.rb, which got me to thinking that our implementation may not be the ideal solution.  

I was wondering how others may be starting and stopping Selenium from within Cucumber.  Ideally, I'd like our solution to start Selenium just once, then iterate over each of the scenarios, keeping the browser open until each of the steps have been executed, and then close Selenium after completing the acceptance tests.  Any guidance with respect to this would be most appreciated.

Regards,
Robert

Richard Paul

unread,
Dec 13, 2010, 5:33:45 AM12/13/10
to Cukes
Hi Robert,

We are using the Groovy binding for cuke4duke, but we suffered from
the same problem whereby the tests take much longer to run than they
should due to the browser opening and closing for each scenario.
Our first attempt to speed this up was to only create the driver if it
didn't already exist

Before {
if (!browser) {
browser = new FirefoxDriver();
}
}

Where the browser was stored in the Groovy script binding, this didn't
work though as a new script binding was created for each scenario.
After reading about Geb (http://geb.codehaus.org) we discovered it
caches an instance of the driver automatically, so repeated calls to
new Browser() actually result in the same underlying browser, e.g.

Before {
browser = new Browser() // Browser is the Geb Browser object which
wraps a raw WebDriver instance
}

Geb is a Groovy API, so I don't think it would work too well if you
are writing your step definitions in Java, but looking into its source
code it think it uses thread local to store the driver instance so
it's available across different scenarios.

To ensure our scenarios are independent we clear the cookies after
each scenario:

After {
browser.clearCookies()
}

Not quite the answer you were looking for I'm sure but hopefully it
helps.
By the way I'll be talking at the Agile Testing UK usergroup at
SkillsMatter on the evening of the 26th Jan about Cucumber with Groovy
etc if you are interested/in the same country http://agiletesting.org.uk/january_2011.php

Cheers,
Richard

Robert

unread,
Dec 13, 2010, 9:57:51 AM12/13/10
to cu...@googlegroups.com
Richard,

Much appreciated.  I'll have a look at Geb.  Although we are using Java for our step definitions, it may still be worthwhile to have a look at Geb's source code to see how they implemented their solution, and possibly implement that in Java.  Thanks.

Regards,
Robert

Karthik

unread,
Dec 15, 2010, 5:43:42 PM12/15/10
to Cukes
I had similar issues with Watir-webdriver and the solution was to make
the browser object global like $browser.

This works as a treat- which opens the browser instance once, run all
the scenarios and at_exit do $browser.close

Karthik
Reply all
Reply to author
Forward
0 new messages