开发者

Capybara and Javascript onbefoureunload

开发者 https://www.devze.com 2023-02-07 23:14 出处:网络
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, addi

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

After "@javascript" do
  page.driver.browser.switch_to.alert.dismiss
  # or accept it if that is what you prefer
  page.driver.browser.switch_to.alert.accept
end


A similar answer for anyone using Rspec and Capybara. This code visits 'about:blank' after each test.

context 'page that displays an alert when navigating away' do

  after(:each) do
    begin
      accept_confirm { visit('about:blank') }
    rescue Capybara::ModalNotFound
      # For examples that don't end with an alert
    end
  end

  ...
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号