5

In my tests I use this step to confirm a javascript confirm popup:

/**
 * @when /^(?:|I )confirm the popup$/
 */
public function confirmPopup()
{
    $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
}

This step work fine with selenium2 and chrome/firefox, but doesn't work with phantomjs.

How can I handle a confirm popup with phantomjs ?

for informations:

  • symfony: 2.0.23
  • behat: 2.4.6
  • mink: 1.5.0
  • Symfony2Extension: 1.0.2
  • MinkExtension: 1.1.4
  • MinkBrowserKitDriver: 1.1.0
  • MinkSelenium2Driver: 1.1.0
  • phamtomjs 1.9.1

behat.yml

default:
    extensions:
        Behat\Symfony2Extension\Extension:
            mink_driver: true
        Behat\MinkExtension\Extension:
            base_url: "http://localhost:8000/app_test.php"
            default_session: selenium2
            selenium2:
                wd_host: "http://localhost:9876/wd/hub"

Thanks!

PS: Here the gist : https://gist.github.com/blazarecki/2888851

Benjamin Lazarecki
  • 2,950
  • 1
  • 20
  • 27

2 Answers2

1

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 dialog
Given I accept confirmation dialogs
And I click a ".mylink" element
And I wait for AJAX to finish
And I should not see a ".some-removed-element" element
Alex Skrypnyk
  • 1,331
  • 13
  • 12
0

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

vijay pujar
  • 1,683
  • 4
  • 19
  • 32