How to handle confirm popup with phantomjs + behat + mink How to handle confirm popup with phantomjs + behat + mink symfony symfony

How to handle confirm popup with phantomjs + behat + mink


I updated my "Selenium2Driver.php" with the following:

public function acceptAlert(){$this->wdSession->accept_alert();}

This makes the accept_alert() available for the driver.

So in the script, you could do something line this to accept the alert.

$this->getSession()->getDriver()->acceptAlert();

Note that I'm using the RawMinkContext not the native MinkContext


phantomjs is a headless browser, therefore all dialogs are not show and cannot be interacted with. A solution is to rewrite widnow.confirm and window.alert with your own functions that return pre-defined values.

Since a scenario runs within the same driver, it is perfectly safe to overwrite native methods with pre-defined return values (you will not have a situation where you really need to see a window within the same scenario). Moreover, it is safe to call these step definitions multiple times within a single scenario to flip returned value.

/** * @When I accept confirmation dialogs */public function acceptConfirmation() {  $this->getSession()->getDriver()->executeScript('window.confirm = function(){return true;}');}/** * @When I do not accept confirmation dialogs */public function acceptNotConfirmation() {  $this->getSession()->getDriver()->executeScript('window.confirm = function(){return false;}');}

Scenario example:

Scenario: Removal of something with confirmation dialogGiven I accept confirmation dialogsAnd I click a ".mylink" elementAnd I wait for AJAX to finishAnd I should not see a ".some-removed-element" element