diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index 863b1a65333..50cc04c62c9 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -32,8 +32,11 @@ use Behat\Mink\Element\NodeElement; use Behat\Mink\Exception\DriverException; use Behat\Mink\Exception\ElementNotFoundException; use Behat\Mink\Exception\ExpectationException; +use Facebook\WebDriver\Exception\NoSuchAlertException; use Facebook\WebDriver\Exception\NoSuchElementException; use Facebook\WebDriver\Exception\StaleElementReferenceException; +use Facebook\WebDriver\WebDriverAlert; +use Facebook\WebDriver\WebDriverExpectedCondition; /** * Cross component steps definitions. @@ -262,12 +265,25 @@ class behat_general extends behat_base { $this->getSession()->switchToWindow($names[0]); } + /** + * Wait for an alert to be displayed. + * + * @return WebDriverAlert + */ + public function wait_for_alert(): WebDriverAlert { + $webdriver = $this->getSession()->getDriver()->getWebdriver(); + $webdriver->wait()->until(WebDriverExpectedCondition::alertIsPresent()); + + return $webdriver->switchTo()->alert(); + } + /** * Accepts the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental. * @Given /^I accept the currently displayed dialog$/ */ public function accept_currently_displayed_alert_dialog() { - $this->getSession()->getDriver()->getWebDriver()->switchTo()->alert()->accept(); + $alert = $this->wait_for_alert(); + $alert->accept(); } /** @@ -275,7 +291,8 @@ class behat_general extends behat_base { * @Given /^I dismiss the currently displayed dialog$/ */ public function dismiss_currently_displayed_alert_dialog() { - $this->getSession()->getDriver()->getWebDriver()->switchTo()->alert()->dismiss(); + $alert = $this->wait_for_alert(); + $alert->dismiss(); } /**