Interacting with a Javascript confirmation from Selenium

Gabe Berke-Williams

Ever needed to interact with a Javascript confirm() dialog? Here’s how to do it in Selenium from Cucumber:

When /^I confirm the dialog$/ do
  page.driver.browser.switch_to.alert.accept
end

When /^I cancel the dialog$/ do
  page.driver.browser.switch_to.alert.dismiss
end

About thoughtbot

We've been helping engineering teams deliver exceptional products for over 20 years. Our designers, developers, and product managers work closely with teams to solve your toughest software challenges through collaborative design and development. Learn more about us.