MDL-82373 behat: Wait for alerts before accepting/dismissing them

This commit is contained in:
Andrew Nicols 2024-07-07 21:03:48 +08:00
parent 8bba968f7b
commit 78ccdc9939
No known key found for this signature in database
GPG key ID: 6D1E3157C8CFBF14

View file

@ -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();
}
/**