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 have clicked an image in a slide show and it generates a new window. Once the window opens I need to bring focus on new window and do some stuff. I have attached my code below, but it is not successful.

@Test
public void testCreateAccount() throws Exception{

//OPEN manorama online  url
driver.navigate().to("http://www.manoramaonline.com/cgi-bin/MMOnline.dll/portal/ep/home.do?tabId=0&BV_ID=@@@");

// to click a link inside a slide show

while (!driver
        .findElement(
                By.xpath("/html/body/div[2]/div[2]/div[4]/div/div/div/div/center/div/div[2]/span/div/a/img"))
        .isDisplayed()) {
}
driver.findElement(
        By.xpath("/html/body/div[2]/div[2]/div[4]/div/div/div/div/center/div/div[2]/span/div/a/img"))
        .click();



String newwindowTitle = driver.getTitle();

switchWindow(newwindowTitle);

}

protected static boolean switchWindow(String title) throws IOException {

    String currentWindow = driver.getWindowHandle();
    Set<String> availableWindows = driver.getWindowHandles();
    if (!availableWindows.isEmpty()) {
    for (String windowId : availableWindows) {
    if (driver.switchTo().window(windowId).getTitle().equals(title)) {
    return true;
    } else {
        driver.switchTo().window(currentWindow);
    }}}

    return false;   
    }
share|improve this question
    
You're making an assumption that titles of pages are different/unique. Is it true for your app? –  dzieciou Oct 20 '12 at 1:51

3 Answers 3

A possibly much simpler solution would be this:

driver.SwitchTo().Window(driver.WindowHandles.Last());

.Last() will just switch to the most recently opened window, which should be the one that just opened when you clicked the link.

share|improve this answer

I think that you're probably over complicating the problem in your switchWindow method.

If I understand you correctly, all that you want to do is iterate through the open windows, compare the title of each window, and if it's the right one, return true.

C# example

protected static Boolean SwitchWindow(string title)
            {
                var currentWindow = driver.CurrentWindowHandle;
                var availableWindows = new List<string>(driver.WindowHandles);

                foreach (string w in availableWindows)
                {
                    if (w != currentWindow)
                    {
                        driver.SwitchTo().Window(w);
                        if (driver.Title == title)
                            return true;
                        else
                        {
                            driver.SwitchTo().Window(currentWindow);
                        }

                    }
                }
                return false;
            }
share|improve this answer

When you only have a URL, this may be a helpful solution:

                   public void SwitchTo(string prefix)                 
                   {                        
                    var handlers = driver.WindowHandles;

                        foreach (var handler in handlers)
                        {
                            driver.SwitchTo().Window(handler);
                            if (driver.Url.Contains(prefix)) return;                                     
                        }
                   }
share|improve this answer

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.