From b2320ccd71f35491a4cbae312a184f6e174bfa97 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Sun, 7 Jul 2024 21:03:48 +0800 Subject: [PATCH] MDL-82373 behat: Wait for alerts before accepting/dismissing them --- lib/tests/behat/behat_general.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) 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(); } /**