如何使用PHP behat / mink selenium2 chrome webdriver处理javascript警报窗口

When testing I get a javascript alert box and I try closing it but I get error unexpected alert open: {Alert text : OK to remove this exclusion?}

I am trying to use:

$this->driver = new Selenium2Driver('chrome');
$this->driver->getWebDriverSession()->accept_alert();

What is the proper way to use PHP behat/mink selenium2 chrome webdriver to close an alert box?

Using Behat 3.2.0 mink 1.7.1

Could you check with this

$this->getSession()->getDriver()->getWebDriverSession()->accept_alert();

or

Could you try updating ConfirmPopup function in featureContext.php file as follows

public function iConfirmPopup()
{
$this->getMainContext()->getSession()->getDriver()->getWebDriverSession()->accept_alert();
}

add this in featureContext.php file

Reference Link solution to use alert(), confirm() and prompt() in Selenium2Driver

You don't need to create Selenium2Driver for this method. For Behat 3 this should work if you add it in an object that extends Page object.

public function iConfirmThePopup(){
    $i = 0;
    while($i < 5) {
        try {
            $this->getDriver()->getWebDriverSession()->accept_alert();
            break;
        }
        catch(NoAlertOpenError $e) {
            sleep(1);
            $i++;
        }
    }
}

and add to the beginning of the class:

use WebDriver\Exception\NoAlertOpenError;

You can customize the method according to your needs, you can remove the while and the try-catch if you don't need them.

UPD: code formatting fixed

I find this function is really working for me:

public function acceptAlert()
{
    $driver = $this->getDriver();
    if ($driver instanceof Selenium2Driver) {
        for ($i = 0; $i < 10; $i++) {
            try {
                $driver->getWebDriverSession()->accept_alert();
                break;
            }
            catch (NoAlertOpenError $e) {
                sleep(2);
                $i++;
            }
        }
    }
}