I have an ‘ask_a_question’ page with an onbeforeunload function to alert the user before leaving something unsaved (hey! like SO :P). I’m testing it using cucumber with capybara and webdriver, adding the @javascript tag because it uses a lot of javascript. A cucumber feature might look like this:
@javascript
Scenario: add a question
Given I login as "Mauricio"
And I go to the create question page
Then I should see "Ask a Question" within "header"
But once the test pases, Capybara (or WebDriver, I dunno) tries to reuse the same browser window for other tests then the onbeforeunload alert is displayed screwing the following test.
As my feature is not closing or exiting the page per se. I don’t think it might be good idea to add something to accept the alert. But honestly I’m quite lost.
How can I tell capybara to use a new browser window for each @javascript test or automatically close the onbeforeunload alert?
Thanx
you can use page.driver.browser.switch_to.alert.dismiss (docs here) to get rid of the alert.
You could put this in a cucumber after hook