Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle browser window or tab using Behat and Mink PHP

I have test that click on link and then new browser window open and in new browser window I need to check some elements are present. How to switch to new Browser window that was opened using BEHAT/MINK?

like image 244
Ihor Bovkit Avatar asked Sep 02 '15 08:09

Ihor Bovkit


People also ask

How do I install mink in PHP?

Mink is a php 5.3 library that you’ll use inside your test suites or project. Before you begin, ensure that you have at least PHP 5.3.1 installed. The recommended way to install Mink with all its dependencies is through Composer: For local installations of composer you must call it like this: $ php composer.phar require --dev behat/mink .

How to add Mink to a Behat project?

The simplest one is to use inheritance. Just extend your context from Behat\MinkExtension\Context\MinkContext instead of the base BehatContext. Note that you will also have to do this if you’ve already been using Behat in your project, but without Mink, and are now adding Mink to your testing:

How do I start a session in mink?

In Mink, the entry point to the browser is called the session. Think about it as being your browser window (some drivers even let you switch tabs!). First, start your session (it’s like opening your browser tab). Nothing can be done with it before starting it. // Choose a Mink driver.

What is mink and how do I use it?

Mink is a PHP 5.3+ library that you’ll use inside your test and feature suites. Before you begin, ensure that you have at least PHP 5.3.1 installed. Mink integration into Behat happens thanks to MinkExtension. The extension takes care of all configuration and initialization of the Mink, leaving only the fun parts to you.


1 Answers

You can use switchToWindow($windowName) method.

$this->getSession()->switchToWindow($windowName);

Method declaration is here

You can get all windows from current session and then switch to the second one for example

$windowNames = $this->getSession()->getWindowNames();
if(count($windowNames) > 1) {
    $this->getSession()->switchToWindow($windowNames[1]);
}
like image 128
Igor Lantushenko Avatar answered Nov 14 '22 22:11

Igor Lantushenko