Skip to content

Instantly share code, notes, and snippets.

@blazarecki
Last active November 10, 2021 15:54
Show Gist options
  • Star 45 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save blazarecki/2888851 to your computer and use it in GitHub Desktop.
Save blazarecki/2888851 to your computer and use it in GitHub Desktop.
Alert, confirm and prompt with mink
<?php
namespace Widop\Mink\Extension;
/**
* Dictionary to manage popups.
*
* @author Benjamin Lazarecki <benjamin.lazarecki@gmail.com>
*/
trait PopupDictionary
{
/**
* @when /^(?:|I )confirm the popup$/
*/
public function confirmPopup()
{
$this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
}
/**
* @when /^(?:|I )cancel the popup$/
*/
public function cancelPopup()
{
$this->getSession()->getDriver()->getWebDriverSession()->dismiss_alert();
}
/**
* @When /^(?:|I )should see "([^"]*)" in popup$/
*
* @param string $message The message.
*
* @return bool
*/
public function assertPopupMessage($message)
{
return $message == $this->getSession()->getDriver()->getWebDriverSession()->getAlert_text();
}
/**
* @When /^(?:|I )fill "([^"]*)" in popup$/
*
* @param string $message The message.
*/
public function setPopupText($message)
{
$this->getSession()->getDriver()->getWebDriverSession()->postAlert_text($message);
}
}
@lphuberdeau
Copy link

Works like a charm, thanks for publishing.

@blazarecki
Copy link
Author

Thanks, if you want i can add a github repository in order to manage it with composer ?

@jdewit
Copy link

jdewit commented Apr 22, 2013

wdSession is a private variable. You need to use ->getWebDriverSession()

@blazarecki
Copy link
Author

I have update the gist, now it's use the getter. Thanks to @jdewit

@mesimms
Copy link

mesimms commented Oct 16, 2013

With Selenium at least, the parameters to POST alert_text need to be in JSON format. postAlert_text(array("text" => $message)) works fine for me.

@parvbhullar
Copy link

I'm not able to get ->getWebDriverSession. any help

@luketarplin
Copy link

Awesome works great thanks for this trait.

@steveyoungqa
Copy link

As with parvbhullar I am not able to get ->getWebDriverSession

I am using Behat in PhpStorm, can you clarify if there is any missing code or cavets not shown here?

@czettnersandor
Copy link

This saved me a lot of time, thanks.

@elodie-nazaret
Copy link

I have the same problem with Behat and Selenium (version 2.44).
My php function :

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

And the error when I launch the test :

[WebDriver\Exception\UnexpectedAlertOpen]                                                                           
Modal dialog present: interface.delete.confirmation
Driver info: org.openqa.selenium.firefox.FirefoxDriver

Thanks for your help

@acfreitas
Copy link

Thank! This gist was helpfull

@balusai
Copy link

balusai commented Sep 8, 2015

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

I'm not able to get ->getWebDriverSession , I am using Behat in PhpStorm, can you clarify if there is any missing code

@balusai
Copy link

balusai commented Sep 11, 2015

/**
* @when /^I confirm popup$/
*/
public function iConfirmPopup()
{
$this->getMainContext()->getSession()->getDriver()->getWebDriverSession()->accept_alert();
}

add this in featureContext.php file

and add "When I confirm popup " in your feature file

Now it works fine.

@blazarecki
Copy link
Author

@vincenzodibiaggio
Copy link

tnx for your work. I only suggest to you to use the assert() function instead of a simple return

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment