Rails + Capybara: "Unexpected Warning Open" for Selenium Driver

I use Capybara and Chrome as my default selenium browser.

Test:

it "is successful with deleting a user", js: true do visit '/users' expect(User.count).to eq(1) expect(user.email).to eq(" ryan@drake.com ") expect(page).to have_content("Manage Users") click_link 'Delete User' page.driver.browser.confirm.accept user.reload visit '/users' expect(User.count).to eq(0) end 

I get this error for my test:

 Failure/Error: visit '/users' Selenium::WebDriver::Error::UnhandledAlertError: unexpected alert open 

In my test, I tried the following:

 page.driver.browser.switch_to.confirm page.driver.browser.switch_to.accept page.driver.browser.confirm.accept 

Any other options I should try with my test?

+7
ruby-on-rails selenium ruby-on-rails-4 capybara
source share
1 answer

Try wrapping code that triggers a warning request inside the accept_alert block, for example:

 it "is successful with deleting a user", js: true do visit '/users' expect(User.count).to eq(1) expect(user.email).to eq(" ryan@drake.com ") expect(page).to have_content("Manage Users") # Change is here: accept_alert do click_link 'Delete User' end user.reload visit '/users' expect(User.count).to eq(0) end 

I'm a little worried that you want to use a warning, not a confirmation when deleting a resource, but that is up to you. The warning will simply tell you that something will happen, while the confirmation allows the user to change their mind by clicking on the β€œCancel” button instead of β€œOK”. If the confirmation mode is used instead of the notification modality, then the accept_alert part should be changed to accept_confirm .

Refer to the rubydoc modal documentation for more information.

+14
source share

All Articles