Read more

Understanding the Selenium error "Modal Dialog Present"

Henning Koch
August 23, 2012Software engineer at makandra GmbH

So your Cucumber feature sometimes dies with this exception:

Modal Dialog Present (Selenium::WebDriver::Error::UnhandledAlertError)
Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

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:

@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 browser window around between scenarios, your second scenario now fails.

How to solve this

Make sure your AJAX requests always finish before your scenario ends.

Posted by Henning Koch to makandra dev (2012-08-23 23:50)