3

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

1
  • What version of Behat are you using? Commented Sep 23, 2016 at 9:26

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I can not access ->getWebDriverSession(). Not sure if my setup is wrong or what.
I am getting method getWebDriverSession not found in class Behat\Mink\Driver\DriverInterface
2

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

1 Comment

The 3rd line is not proper PHP syntax... seems that the while got clobbered with the try clause.
0

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++;
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.