Getting focus on popup window

658 views
Skip to first unread message

Jimmy 99

unread,
Feb 13, 2012, 8:05:03 AM2/13/12
to Selenium Users
Greetings
I am using C# and dotnet framework

I am trying to test a website using Selenium

I first create the Selenium instance like so:

IWebDriver selenium = new RemoteWebDriver(new Uri("http://
127.0.0.1:555/wd/hub"),
DesiredCapabilities.HtmlUnitWithJavaScript());

Then I connect to the target url

driver.Navigate().GoToUrl("http://1.2.3.4/apex/cape.zul");

then I find the login button

IWebElement clickLogin = driver.FindElement(By.ClassName("z-
toolbarbutton-cnt"));

Then I click the login button

clickLogin.Click();

at this point at window pops up with a username and password textbox.



I am struggling to get the focus of the page to change to the popup
window.

There are quite a few examples on the internet of how to do this:

selenium.waitForPopUp("popupWindowID", "30000");
selenium.selectWindow("name=popupWindowID");
assertEquals(selenium.getTitle(), "Popped Up Window");


However, in my code, IwebDriver does not expose a method
like .selectWindow
Do you know which object exposes .selectWindow or am I missing
something?

Jim Evans

unread,
Feb 13, 2012, 10:36:19 AM2/13/12
to Selenium Users
Assuming the window that pops up is an actual browser window (and not
the Windows user name/password/security dialog), you should be able to
use:

selenium.SwitchTo().Window("<WindowHandle>");

The tricky bit will be to get the "<WindowHandle>". The .NET bindings
have a PopupWindowFinder class in the support classes
(WebDriver.Support.dll) which might make this a little easier. If
nothing else, you can examine the code in that class[1] to help you
understand how to get something similar working in your code.

Oh, wait a minute. I just realized you're trying to use HtmlUnit for
this. I'm not sure how well HtmlUnit handles multiple windows. If it
supports them fully, you should be okay, I think.

--Jim

[1] http://code.google.com/p/selenium/source/browse/trunk/dotnet/src/WebDriver.Support/UI/PopupWindowFinder.cs

Jimmy 99

unread,
Feb 13, 2012, 12:04:31 PM2/13/12
to Selenium Users
Hi Jim
Thanks for this info.
Is the PopupWindowFinder class something extra that I have to install?
If not which namespace is it in?

Regards
Jimmy


On Feb 13, 5:36 pm, Jim Evans <james.h.evans...@gmail.com> wrote:
> Assuming the window that pops up is an actual browser window (and not
> the Windows user name/password/security dialog), you should be able to
> use:
>
> selenium.SwitchTo().Window("<WindowHandle>");
>
> The tricky bit will be to get the "<WindowHandle>". The .NET bindings
> have a PopupWindowFinder class in the support classes
> (WebDriver.Support.dll) which might make this a little easier. If
> nothing else, you can examine the code in that class[1] to help you
> understand how to get something similar working in your code.
>
> Oh, wait a minute. I just realized you're trying to use HtmlUnit for
> this. I'm not sure how well HtmlUnit handles multiple windows. If it
> supports them fully, you should be okay, I think.
>
> --Jim
>
> [1]http://code.google.com/p/selenium/source/browse/trunk/dotnet/src/WebD...
> > something?- Hide quoted text -
>
> - Show quoted text -

Jim Evans

unread,
Feb 13, 2012, 12:28:34 PM2/13/12
to Selenium Users
From my previous reply:

> > The .NET bindings
> > have a PopupWindowFinder class in the support classes
> > (WebDriver.Support.dll) which might make this a little easier.

Whether you have to "install" something else depends on how you got
WebDriver into your project in the first place. If you downloaded it
from the project site[1], you simply have to add a reference to
WebDriver.Support.dll, and you should be able to find it in the .zip
you downloaded. If you're using NuGet, you'll need to add the
WebDriver.Support package to your dependencies. The class is in the
OpenQA.Selenium.Support.UI namespace.

--Jim
> > - Show quoted text -- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages