5
votes

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

2
Are you using the native javascript "alert" or a cooked up one? - Ken Hannel
I use the native one. - Benjamin Lazarecki
Did you manage to figure this out Benjamin? I've come across the same issue... - Matt
Not I didn't. I'll update the post if I find a solution. - Benjamin Lazarecki
How do you run your tests that include an alert at the mo then, via Selenium? - Matt

2 Answers

0
votes

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

0
votes

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