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

2

6

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?

Vaucluse answered 2/9, 2015 at 8:32 Comment(0)
C
8

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]);
}
Countdown answered 2/9, 2015 at 8:42 Comment(2)
Thanks Igor, I know about this method but I don't know how to get window nameVaucluse
if your window name is a dynamic for example, then just use getWindowNames() as I mentioned above and switch to the second name. Maybe you need also to add method for waiting while second window will be opened.Countdown
F
0

Just in case this helps anyone, the window names are an array. So, using Igor's answer, $windowNames[1] is the 2nd tab, [2] would be a 3rd, and so fourth. If you are wondering how to go back to the first tab after this, simply remove the 'if' block (or check if at least 1 window exists) from Igor's answer and make a new function like so:

  /**
  * @Given I go to the first tab
  */
  public function goToFirstTab()
  {
    $windowNames = $this->getSession()->getWindowNames();
    $this->getSession()->switchToWindow($windowNames[0]);
  }
Fairtrade answered 25/1, 2022 at 23:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.