Keeping the browser open when you’re writing a Cucumber test

I find it handy to keep the browser open when I am writing a Cucumber test, so that I know what to do next.
This is pretty simple to do in your env.rb file. (Note: Chrome always closes when a test is finished no matter what, it is a known bug).

After do |scenario|
  KEEP_OPEN = true if scenario.status == :pending or scenario.status == :undefined
end

at_exit do
  browser.close if not KEEP_OPEN
end

Sometimes I keep the browser open on failure too, but this is not very good if you’re running unattended, for example on a CI build, so I often just capture a screenshot instead.

6 thoughts on “Keeping the browser open when you’re writing a Cucumber test

  1. Just as a note, this will fail if you have a Background (as opposed to a Scenario) that is pending.

    The error I get in such a situation is:

    undefined local variable or method ‘status’ for # NoMethodError

  2. Actually my last comment lost the HTML like information. The undefined method ‘status’ for was called on Cucumber::Ast::Background.

  3. This won’t work on Windows at all because I think Cucumber is broken. If you have a step that passes, try this:

    AfterStep do |scenario|
    puts scenario.status
    end

    You’ll find that even with a step that passed, Cucumber will report “skipped” here. I tried this all the way up to the latest 1.1.2. When i say the step passed, I mean I see it come back with green. If the same step is made to fail, the puts command above never fires: so AfterStep only seems to be after the step has succeeded.

    But then if you have this:

    After do |scenario|
    puts scenario.status
    end

    That also doesn’t run the puts command. Incidentally, even if my step passes the puts in the After logic is never utilized.

    So this KEEP_OPEN thing doesn’t have a chance of working in those situations because the AfterStep and After logic does not seem to work (at least on Windows) very well. Maybe this is happening on other OS’s as well.

Comments are closed.