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.

I've been attempting to figure this out for the past two days, unfortunately with no luck. I'm currently automating a scenario where a new window for a form is popped up from the main application under test. Unfortunately, the page with the originating link, and the popup page has the same title.

The code that I'm currently using looks as follows:

string MainWindowHandle = browser.GetWindowHandle();
browser.Click();
ReadOnlyCollection<string> handles = browser.GetWindowHandles();
string newWindowHandle = string.Empty;
foreach (string handle in handles)
  {
    if(handle != mainWindowHandle)
    {
      newWindowHandle  handle.ToString();
      break;
    }
  }
browser.SwitchTo().Window(newWindowHandle);

Is there another way that I should be doing this, or am I going to need to discuss the possibility of having one of the titles changed by a character?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Sounds like an odd bug, I thought the window handle used to be a hash like object and you could use either the handle or the title of the window to switch to and from but that might have changed since the last time I saw it. If you want both windows to have the same name you could use the driver to execute JavaScript that will grab the element creating the window and have it change the title of the window only. This way if the JavaScript ever changes the test won't immediately break. It's not the best solution; I hate running JavaScript in selenium tests, but every so often it is the easiest/fastest solution.

share|improve this answer
    
The handle is normally a hash. The handles are different in this case (don't have my tests with me at the moment, but, ie: internet_explorer_browser_6 with the pop up being internet_explorer_browser_8). I'm not familiar with injecting javascript to change an element through selenium. Would you happen to have any references where I could begin to start looking? –  Lyndon Vrooman Jun 30 '11 at 9:39
    
Sure, for a basic overview there is some at Selenium FAQ and for a more in-depth explanation you can use JavascriptExectutor API. –  Jason Ward Jun 30 '11 at 14:52
    
Cool. Thank you! I'll take a look into it. –  Lyndon Vrooman Jun 30 '11 at 14:57
    
Back before Selenium 2 could deal with alerts we used to have to call self.driver.execute_script('window.onbeforeunload = function(){};') (sorry its python bindings) in order to prevent the alert from showing up when we were trying to change pages without saving. Its a simple example, but I feel you will be able to do something similar –  Jason Ward Jun 30 '11 at 18:21
    
Haven't had a chnace to touch this project since asking this, however, I've used this technique in a few other circumstances since and it seems as though it will work. –  Lyndon Vrooman Jul 19 '11 at 9:54
add comment

If the popup window is generated by execution of javascript on the parent window, there used to be an issue with Selenium that it would not identify the newly generated window. If you haven't tried, the first thing I would do is check that the Selenium / WebDriver even knows that a second window exists.

share|improve this answer
    
Although it is generated by javascript, when I do the same thing with another popup that is generated the same way, but has a title different than the originating window, it seems to work fine. –  Lyndon Vrooman Jun 24 '11 at 17:43
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.