Skip to content

Commit

Permalink
MDL-82373 behat: Wait for alerts before accepting/dismissing them
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jul 17, 2024
1 parent 8bba968 commit 78ccdc9
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/tests/behat/behat_general.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
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.
Expand Down Expand Up @@ -262,20 +265,34 @@ public function i_close_all_opened_windows() {
$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();
}

/**
* Dismisses the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental.
* @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();
}

/**
Expand Down

0 comments on commit 78ccdc9

Please sign in to comment.