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?
How to handle browser window or tab using Behat and Mink PHP
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]);
}
Thanks Igor, I know about this method but I don't know how to get window name –
Vaucluse
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
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]);
}
© 2022 - 2024 — McMap. All rights reserved.