Posted almost 7 years ago. Visible to the public.

Understanding the Selenium error "Modal Dialog Present"

So your Cucumber feature sometimes dies with this exception:

Copy
Modal Dialog Present (Selenium::WebDriver::Error::UnhandledAlertError)

As a seasoned Selenium vetaran you are used to misleading error messages. Hence you might be surprised that the reason for this particular error is that there is actually a modal dialog present and preventing Selenium from executing commands like click or page.have_css?.

How your code triggers this issue

The reason why a dialog is shown is somewhat fucked up. It often happens like this:

Copy
@javascript Scenario: First scenario When I am on a page And I trigger an AJAX request that shows an alert in case of an error @javascript Scenario: Second scenario When I am on another page

If your PC is fast enough, the "When I am on another page" from the second scenario can interrupt an incomplete AJAX request from the first scenario. This triggers the error alert callback you so helpfully registered in your Javascript. Because Selenium keeps your Firefox window around between scenarios, your second scenario now fails.

How to solve this

Make sure your AJAX requests always finish before your scenario ends. The best way is to observe some side effect of your AJAX call within your scenario, so you know it has finished.

If there is no side effect to observe, change your code so there is one as described in Waiting for page loads and AJAX requests to finish with Capybara.

Many Bothans died to bring you this card.

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Henning Koch
Last edit:
over 4 years ago
by Dominik Schöler
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more