Switch browser tabs - or at least follow the tab with focus

1,336 views
Skip to first unread message

Mike

unread,
Jan 25, 2013, 8:00:40 AM1/25/13
to be...@googlegroups.com
I've got some tests which switch between browser windows and they work just fine with selenium2 driver and switchToWindow().

How about switching browser tabs? I have a form which appears in a new tab (the result of target="_blank" in the HTML) and any "And I should see" steps referring to the new tab are failing. In fact, I have an "And I should not see" things of the original tab and they are failing, that is to say, behat or mink thinks it's on the tab which is not in focus.

I don't need to switch browser tabs at will, just to follow the tab that would be in focus with a real user.

Thanks.

Konstantin Kudryashov

unread,
Jan 25, 2013, 8:36:45 AM1/25/13
to be...@googlegroups.com
Almost sure no browser will allow you to do such things because of security. And as any browser-manipulator works from inside browser JS layer, I'm almost sure it's simply impossible to do.

Konstantin Kudryashov
--- PHP developer, BDD evangelist, Scrum master

a: http://about.me/everzet
b: http://everzet.com
g: https://github.com/everzet

Mike Simms

unread,
Jan 28, 2013, 10:39:11 AM1/28/13
to be...@googlegroups.com
OK: I understand what your saying, but it surprises me that the new tab
which 'naturally' has user focus is not interacted with: the session of
Mink/Selenium is tied to the original tab then?

I suppose I can work around it by having an optional 'new tab' / 'this
tab' so that I can use the 'this tab' for testing, but it's a buit clunky.
--
Mike Simms +34 648749426

Marijn Huizendveld

unread,
Jan 28, 2013, 10:43:07 AM1/28/13
to be...@googlegroups.com
Why is switching tabs even part of your scenario? Sounds like something far out of context of feature testing...

Mike Simms

unread,
Jan 28, 2013, 11:25:22 AM1/28/13
to be...@googlegroups.com
It's not that I have a step saying 'And I switch to tab "foo"' or
anything, it's just that I have a long list of parts, each one being a
link to a page with details about that part in a new tab.

Now, time to feel silly: I've realised that using the goutte driver it
will 'see' the new tab as expected, so that solves my original problem
for some scenarios, it's when I have @javascript set for a feature that
the problems arise.

I'll look into it a bit more and report back.

On 28/01/13 16:43, Marijn Huizendveld wrote:
> Why is switching tabs even part of your scenario? Sounds like something far out of context of feature testing...
>

Mike

unread,
Jan 30, 2013, 5:39:56 PM1/30/13
to be...@googlegroups.com
OK, I'm now better informed: the tab is a construction of the browser only, made obvious by the "Open new windows in a new tab" setting in Firefox. So, I solve my problem by assuming that all windows open in a new window, and happily switchToWindow() as needed.

Issue closed!


Navaraj Neupane

unread,
Aug 7, 2015, 5:04:54 AM8/7/15
to Behat
Hello Mike,

Based on your first statement "I've got some tests which switch between browser windows and they work just fine with selenium2 driver and switchToWindow()". Do you still have an example of how you made this work. I am struggling to make switchWindow work. My composer.json looks like this:
Enter code here...
{
 
"require": {
   
"behat/behat": "3.0.*@stable",
   
"behat/mink": "1.6.*@stable",
   
"behat/mink-extension": "*",
   
"behat/mink-goutte-driver": "*",
   
"behat/mink-selenium2-driver": "*"
  },
 
"minimum-stability": "dev",
 
"config": {
   
"bin-dir": "bin/"
  }
}

And I am trying to access
$this->getSession()->getWindowName();

But I get this error: Fatal error: Call to undefined method Behat\Mink\Session::getWindowName(). Do you happen to know what I am missing? Do I need to implement anything else in my FeatureContext. Current I am just expending MinkContext.

Thank you very much in advance.
Reply all
Reply to author
Forward
0 new messages