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?

Ihor Bovkit
  • 120
  • 3
  • 9

2 Answers2

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]);
}
Igor Lantushenko
  • 1,771
  • 10
  • 19
  • Thanks Igor, I know about this method but I don't know how to get window name – Ihor Bovkit Sep 02 '15 at 08:44
  • 1
    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. – Igor Lantushenko Sep 02 '15 at 08:49
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]);
  }
DORRITO
  • 621
  • 2
  • 8
  • 25