When a Cucumber feature fails, Selenium quits and closes the browser instance. Quite often you want it to stay open to see what’s on the screen at that point to investigate what’s going on.

Looking for answers online I came across a lot of suggestions to put a breakpoint on the closing of Selenium or webdriver and run the test in debug mode, waiting on the breakpoint while you’re able to inspect the browser window. A more elegant approach I found over at bitcrowd without using a debugger!

Since it’s Ruby-orientented here’s a shamelessly copied and adjusted version for Cucumber JVM. Add the following file to your features/support directory:

@After("@leave_window_open")
public void leave_window_open(Scenario scenario) {
    if (scenario.isFailed()) {

        System.out.println("Step failed. Press ENTER to close browser...");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Add the tag @leave_window_open to the feature you want to investigate:

@leave_window_open
Feature: Dates with different date formats
 This feature shows you can have different date formats, as long as you annotate the
 corresponding step definition method accordingly.

 Scenario: Determine past date
  Given today is 2011-01-20
  When I ask if Jan 19, 2011 is in the past
  # here comes the failing step
  ...

Now, the step fails, but the selenium browser window stays open. When done, just hit return in the console you used to fire up Cucumber and it’ll finish as usual.

Thanks bitcrowd!

Advertisements