You're running Mink / Webdriver tests (or Drupal 8+) Tests and need to assert expected JavaScript errors?
Sounds like an easy task, but doesn't seem very well-supported yet.
After playing around a bit, this pattern is what worked best:
<?php
public function testExpectedAlert() {
/**
* @var \Behat\Mink\Driver\Selenium2Driver $driver
*/
$driver = $this->getSession()->getDriver();
// Go to front page and expect the alert to fire:
try {
$this->drupalGet('<front>');
}
catch (UnexpectedAlertOpen $e) {
$this->assertTrue(TRUE);
}
$message = $driver->getWebDriverSession()->getAlert_text();
$driver->getWebDriverSession()->accept_alert();
$this->assertEquals('This is an alert message', $message);
}
?>
Huge thanks to @Grevil, who found this out with me and wrote these great Drupal Testing Snippets: https://gitlab.com/-/snippets/2264049
Another alternative to test might be
<?php
$this->expectException('UnexpectedAlertOpen');
$this->expectExceptionMessage("Couldn't find page variant to store Panels display");
?>
Links that might be helpful:
- https://stackoverflow.com/questions/66450025/unexpected-alert-open-causes-error-in-behat-mink-test
- https://stackoverflow.com/questions/19173195/how-to-handle-the-unexpected-alert-open
- https://github.com/php-webdriver/php-webdriver/issues/475
- https://stackoverflow.com/questions/19173195/how-to-handle-the-unexpected-alert-open
- https://git.drupalcode.org/project/varbase/-/commit/f63a9e46255137f19b43889caa1a2cfadc539d3c