I have a simple link with a data-turbo-confirm
attribute. I intermittently experience an UnexpectedAlertOpenException
.
My test was using
$client->click($link);
$client->wait()->until(WebDriverExpectedCondition::alertIsPresent());
$client->switchTo()->alert()->accept();
The problem is that with $client->click($link)
, the Panther client will try to refresh its crawler. This entails another WebDriver request, which may fail, presumably because of timing, if the alert is already present. I’ve seen two different exceptions that are traced back to src/Client.php#L281.
Facebook\WebDriver\Exception\UnknownErrorException: Unexpected server response to findElements command
/app/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:236
/app/vendor/symfony/panther/src/Client.php:281
and
Facebook\WebDriver\Exception\UnexpectedAlertOpenException: unexpected alert open
/app/vendor/php-webdriver/webdriver/lib/Exception/WebDriverException.php:139
/app/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php:385
/app/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:598
/app/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:232
/app/vendor/symfony/panther/src/Client.php:281
I changed my test to the following, and I have not encountered these exceptions at all, even while running the tests dozens of times.
$link->getElement()->click();
$client->wait()->until(WebDriverExpectedCondition::alertIsPresent());
$alert = $client->switchTo()->alert();
// You can make assertions about alert
// $this->assertEquals('Are you sure?', $alert->getText());
$alert->accept();
// My tests don’t require it, but you may need to refresh the crawler manually at this point
// to mirror what `clickLink` does.
//
// $client->refreshCrawler();