Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

One of the things that I miss about WatiN is the ability to attach to browser instances that were not spawned by Selenium. In my last position, this was the whole reason why I used WatiN, as the browser was always spawned by another application. I was hoping that I would be able to do this through .getWindowHandle() and then .SwitchTo().Window(string), but this doesn't seem as though it's the case.

The ability to do this is invaluable when it comes to writing and debuging scripts and do hope that I'm either doing something wrong, or that it's on the roadmap.

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

I am actually not entirely sure you can switch to a window not spawned by the driver. I think the people working on the selenium 2 project have worked a little bit on switching to a window spawned by a different driver. When you do:

driver.getWindowHandles()

All you get are the windows spawned by the driver object it is called on.

share|improve this answer
1  
This is exactly right. There is no API for attaching to a running browser instance at present. It's a common feature request, though, and yes it's on the roadmap. It would be easiest to implement for IE, given the architecture of that particular driver. Remember that the "handle" you get from getWindowHandles() isn't a Windows HWND; rather it's an opaque string Selenium WebDriver uses to identify the window. –  JimEvans May 19 '11 at 15:54
    
Unfortunately, I've found this to be the case. Luckily, I don't need the functionality at the moment, however, anxiously await this ability in the future. –  Lyndon Vrooman Jun 6 '11 at 10:12
add comment

It is indeed possible in Selenium 2. You could get window handle and switch to one on them. i.e.

for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}

For more on this look here.

share|improve this answer
    
I spent some time today trying to get this to work, but could only switch to handles of browsers that were opened during the current test execution. I'll sit down with one of my dev's though and see if I can't get this to work still. –  Lyndon Vrooman May 9 '11 at 23:15
    
I do not think you can attach to a browser not spawned by the driver. Tarun's solution will only allow switching to and from browsers spawned by that particular driver –  Jason Ward May 10 '11 at 14:08
add comment

You definitely won't be able to for Firefox (since selenium starts that browser with a selenium xpi ~ plugin file and is how it can control it).

I'm not sure about how IE is started, but I'm willing to bet it would be possible.

Not sure about chrome.

share|improve this answer
    
IE uses a dll and chrome uses a zip file. –  Jason Ward May 10 '11 at 20:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.