how do you reference a newly created window/tab from twist?

Comments

3 comments

  • Avatar
    Ranjan

    Hi Gareth,


    Which driver are you using for your twist project - Sahi or Selenium? I ask as the process would be different for the two.


     


    Thanks,


    r.

  • Avatar
    gareth

    i think i figured it out.. i found the same question in the twist help.. although the code referenced does not compile:


     


       String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.
    WebDriver popup = null;
    Iterator<String> windowIterator = browser.getWindowHandles();
    while(windowIterator.hasNext()) {
    String windowHandle = windowIterator.next();
    popup = browser.switchTo().window(windowHandle);
    if (popup.getTitle().equals("Google") {
    break;
    }
    }

     

    using the selenium/webdriver model (if that makes a difference for this) the call to getWindowHandles() does not compile.
  • Avatar
    Ranjan

    Hi Gareth,


    Please try using -


     


    private WebDriver getPopup(String windowTitle) {


    WebDriver popup = null;


    Iterator<String> windowIterator = browser.getWindowHandles().iterator();


    while (windowIterator.hasNext()) {


    String windowHandle = windowIterator.next();


    popup = browser.switchTo().window(windowHandle);


    if (popup.getTitle().equals(windowTitle)) {


    break;


    }


    }


    return popup;


    }


    You should have imported the following -


     


    import java.util.Iterator;


    import java.util.Set;


     


    to get this to work.


     


    The call to getWindowHandle() returns a string which you should keep in a local variable, so that when you close the popup/tab/window - you can switch back to the main by calling -


    browser.switchTo().window(<parentWindowHandle>);

Please sign in to leave a comment.