PHP-WebDriver and Alerts

Technically, dealing with an Alert (or Prompt, or Confirmation) window in PHP-WebDriver is the same as dealing with any other thing in the driver session.

$message = $this->session->alert_text();


While yes, that is true to the JSON Wire Protocol (which was one of Facebook’s goals when they wrote the bindings), this can hide that you are in the context of an alert, and you have to remember which arguments to pass to trigger which behaviour you want. And is not very user friendly. So stealing (like always!) from the WebDriver Python Bindings I’ve add an Alert class to smooth out some of the edges around this.

First, you want to switch to the context of an alert. This is not an actual switch like with windows or frames, it just follows the same naming conventions.

$p = $this->session->switch_to_alert();

Once in the Alert context you can accept or dismiss it


Or get the message

$message = $p->text;

Or even set the text if it is a Prompt.

$message = $p->sendKeys('cheese');

Ideally your application doesn’t use Alerts, but if it does then at least the impact on your scripts won’t be too bad now.

Post a Comment

Your email is never published nor shared. Required fields are marked *