Read more

How to configure Selenium WebDriver to not automatically close alerts or other browser dialogs

Arne Hartherz
January 31, 2024Software engineer at makandra GmbH

tl;dr

We recommend configuring Selenium's unhandled prompt behavior to "ignore".

When running tests in a real browser, we use Selenium. Each browser is controlled by a specific driver, e.g. Selenium::WebDriver::Chrome for Chrome.

Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show archive.org snapshot

There is one quirk to all drivers (at least those following the W3C webdriver spec) that can be impractical:
When any user prompt Show archive.org snapshot (like an alert) is encountered when trying to perform an action, they will dismiss the dialog by default Show archive.org snapshot and raise an error (dismiss and notify mode).

You maybe encountered this in the past, when an Selenium::WebDriver::Error::UnexpectedAlertOpenError was raised.
However, when the error is raised in from the Selenium driver, the user prompt will have been closed already and can not be brought back. Also, any JavaScript code placed after the alert will be executed.

Automatically closing user prompts happens when performing most options, like a simple visit or execute_script from Capybara.

Different behaviors

While drivers default to dismissing user prompts, you can change that. Specifically, the specification Show archive.org snapshot describes these possible behaviors:

Keyword Description
dismiss All simple dialogs encountered should be dismissed.
accept All simple dialogs encountered should be accepted.
dismiss and notify All simple dialogs encountered should be dismissed, and an error returned that the dialog was handled. (default)
accept and notify All simple dialogs encountered should be accepted, and an error returned that the dialog was handled.
ignore All simple dialogs encountered should be left to the user to handle. (recommended)

While the description of ignore does not say if errors would be raised, they are. When a user prompt is open, the driver cannot perform actions like Capybara's visit or execute_script, and you will still receive a Selenium::WebDriver::Error::UnexpectedAlertOpenError. This is probably what you want for running tests.

How to configure Selenium WebDriver

Configuring the unhandled prompt behavior is fairly simple. You need to specify the webdriver option unhandled_prompt_behavior when building your Capybara driver.

For example, the Chrome driver can be configured like this:

Capybara.register_driver(:selenium) do |app|
  options = Selenium::WebDriver::Chrome::Options.new(
    unhandled_prompt_behavior: 'ignore',
    # ...
  )
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
Arne Hartherz
January 31, 2024Software engineer at makandra GmbH
Posted by Arne Hartherz to makandra dev (2024-01-31 09:29)