Example usage for org.openqa.selenium WebDriver getWindowHandles

List of usage examples for org.openqa.selenium WebDriver getWindowHandles

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriver getWindowHandles.

Prototype

Set<String> getWindowHandles();

Source Link

Document

Return a set of window handles which can be used to iterate over all open windows of this WebDriver instance by passing them to #switchTo() .

Usage

From source file:org.jboss.arquillian.graphene.context.TestGrapheneProxyHandler.java

License:Open Source License

@Test
public void test_webDriver_methods_which_should_return_proxy() {
    IsProxyable isProxyable = new IsProxyable();

    // when// w  w w . j  a va 2 s  .  co m
    WebDriver driver = Mockito.mock(WebDriver.class, isProxyable);
    Options options = mock(Options.class, isProxyable);
    TargetLocator targetLocator = mock(TargetLocator.class, isProxyable);
    ImeHandler ime = mock(ImeHandler.class, isProxyable);
    Timeouts timeouts = mock(Timeouts.class, isProxyable);

    // then
    try {
        driver.manage();
        driver.navigate();
        driver.switchTo();
        driver.findElement(By.className(""));
        driver.findElements(By.className(""));
        driver.getWindowHandles();

        options.ime();
        options.logs();
        options.timeouts();
        options.window();
        options.getCookies();

        targetLocator.activeElement();
        targetLocator.alert();
        targetLocator.defaultContent();
        targetLocator.frame(0);
        targetLocator.frame("name");
        targetLocator.frame(mock(WebElement.class));
        targetLocator.window("name");

        ime.getAvailableEngines();

        timeouts.implicitlyWait(1L, TimeUnit.MICROSECONDS);
        timeouts.setScriptTimeout(1L, TimeUnit.MICROSECONDS);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    assertEquals(Arrays.asList(), isProxyable.getViolations());
}

From source file:org.jitsi.meet.test.FilmstripOnlyTest.java

License:Apache License

/**
 * Checks if device selection popup can load.
 *//*from   ww  w .  j  av a2 s .c  o m*/
@Test(dependsOnMethods = { "testLoadsOnlyTheFilmstrip" })
public void testDisplaysDeviceSelection() {
    getParticipant1().getToolbar().clickFilmstripOnlySettingsButton();

    // give some time for the window to open and load
    TestUtils.waitMillis(2000);

    WebDriver driver1 = getParticipant1().getDriver();

    Set<String> windowHandles = driver1.getWindowHandles();
    Iterator<String> handleIterator = windowHandles.iterator();
    String mainWindowHandle = handleIterator.next();
    String deviceSelectionHandle = handleIterator.next();

    driver1.switchTo().window(deviceSelectionHandle);

    // Ensure the device selection modal content is displayed.
    TestUtils.waitForDisplayedElementByXPath(driver1, "//div[contains(@class, 'device-selection')]", 5);

    driver1.close();
    driver1.switchTo().window(mainWindowHandle);
}

From source file:org.jitsi.meet.test.InviteTest.java

License:Apache License

/**
 * Checks if clicking to view more numbers shows the appropriate static
 * page.//from  w ww. j  a  v a  2  s  .c  o  m
 */
@Test(dependsOnMethods = { "testDialInDisplays" })
public void testViewMoreNumbers() {
    InfoDialog infoDialog = participant.getInfoDialog();
    infoDialog.open();

    String displayedNumber = infoDialog.getDialInNumber();
    String displayedPin = infoDialog.getPinNumber();

    infoDialog.openDialInNumbersPage();

    // give some time for the window to open and load
    TestUtils.waitMillis(2000);

    WebDriver driver = participant.getDriver();

    ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
    driver.switchTo().window(tabs.get(1));

    DialInNumbersPage dialInNumbersPage = participant.getDialInNumbersPage();
    dialInNumbersPage.waitForLoad();

    String conferenceIDMessage = dialInNumbersPage.getConferenceIdMessage();

    assert (conferenceIDMessage.contains(displayedPin));
    assert (dialInNumbersPage.includesNumber(displayedNumber));

    driver.close();

    driver.switchTo().window(tabs.get(0));
}

From source file:org.kuali.rice.krad.labs.transactional.LabsLookupTravelAuthorizationDocumentCloseActionAft.java

License:Educational Community License

public void testTravelAuthorizationDocumentLookupAndClose() throws Exception {
    final String xpathExpression = "//iframe[1]";
    driver.switchTo().frame(driver.findElement(By.xpath(xpathExpression)));

    // search based on document type
    waitAndTypeLabeledInput("Document Type:", "TravelAuthorization");

    // click the search button
    jGrowl("Click the Search button");
    final String imgXpath = "//input[contains(@src,'/kr/static/images/buttonsmall_search.gif')]";
    waitAndClick(By.xpath(imgXpath));/*  ww  w . jav a  2s  .  c  o  m*/

    // click on the first item returned.
    jGrowl("Click on the first result in status SAVED returned from the search.");
    String windowHandle = driver.getWindowHandle();
    waitAndClick(By.xpath("//td[contains(text(),'SAVED')]/../td[1]/a"));

    // wait for the new window to pop up
    new WebDriverWait(driver, 10).until(new ExpectedCondition<Object>() {
        @Override
        public Object apply(WebDriver driver) {
            //Wait until we have at least two windows.
            return driver.getWindowHandles().size() > 1;
        }
    });

    // switch focus to the new handle
    for (String handle : driver.getWindowHandles()) {
        if (!handle.equals(windowHandle)) {
            driver.switchTo().window(handle);
            break;
        }
    }

    // click the close button
    waitAndClickButtonByText("Close", WebDriverUtils.configuredImplicityWait() * 10);

    waitAndClickCancelSaveOnClose();

    waitForTextPresent("Development made easy");
}

From source file:org.mozilla.zest.core.v1.ZestClientWindowHandle.java

License:Mozilla Public License

@Override
public String invoke(ZestRuntime runtime) throws ZestClientFailException {
    WebDriver window;//  w  w  w.  j a v  a 2s  . com
    Pattern p = null;
    String runUrl = runtime.replaceVariablesInString(this.url, true);
    if (this.isRegex()) {
        p = Pattern.compile(runUrl);
    }
    boolean found = false;
    for (WebDriver wd : runtime.getWebDrivers()) {
        for (String wh : wd.getWindowHandles()) {
            window = wd.switchTo().window(wh);
            if (this.isRegex()) {
                if (p.matcher(window.getCurrentUrl()).matches()) {
                    runtime.addWebDriver(this.windowHandle, wd);
                    runtime.debug(
                            "Matched window " + window.getWindowHandle() + " url: " + window.getCurrentUrl());
                    found = true;
                    break;
                } else {
                    runtime.debug("Didnt match window " + window.getWindowHandle() + " url: "
                            + window.getCurrentUrl());
                }
            } else {
                if (window.getCurrentUrl().equals(runUrl)) {
                    runtime.addWebDriver(this.windowHandle, wd);
                    runtime.debug(
                            "Matched window " + window.getWindowHandle() + " url: " + window.getCurrentUrl());
                    found = true;
                    break;
                } else {
                    runtime.debug("Didnt match window " + window.getWindowHandle() + " url: "
                            + window.getCurrentUrl());
                }
            }
        }
    }
    if (!found) {
        runtime.output("Failed to find window " + this.getUrl() + " regex=" + this.isRegex());
        throw new ZestClientFailException(this,
                "Failed to find window " + this.getUrl() + " regex=" + this.isRegex());
    }

    return this.windowHandle;
}

From source file:org.richfaces.photoalbum.ftest.webdriver.utils.PhotoalbumUtils.java

License:Open Source License

public static void loginWithSocial(Class<? extends SocialLoginPage> pageClass, final WebDriver browser,
        WebElement loginLink, boolean workaroundGplusAccInChache) {
    String originalWindow = browser.getWindowHandle();
    Graphene.guardAjax(loginLink).click();
    Graphene.waitAjax().until(new Predicate<WebDriver>() {
        @Override/*from   w  w  w.j  a v  a  2 s  .c  o m*/
        public boolean apply(WebDriver input) {
            return browser.getWindowHandles().size() == 2;
        }

        @Override
        public String toString() {
            return "2 windows should be opened, but have: " + browser.getWindowHandles().size();
        }
    });
    Set<String> windowHandles = browser.getWindowHandles();
    windowHandles.remove(originalWindow);
    try {
        WebDriver window = browser.switchTo().window(windowHandles.iterator().next());
        Graphene.waitModel().until().element(By.tagName("body")).is().visible();
        Graphene.createPageFragment(pageClass, window.findElement(By.tagName("body")))
                .login("rf.photoalbum@gmail.com", "rf.photoalbumrf.photoalbum");
    } finally {
        browser.switchTo().window(originalWindow);
        PhotoalbumUtils.waitFor(5000);// FIXME: replace with some wait condition
        Graphene.waitModel().until(new Predicate<WebDriver>() {
            @Override
            public boolean apply(WebDriver input) {
                return browser.getWindowHandles().size() == 1;
            }

            @Override
            public String toString() {
                return "Only one window should be opened, but have: " + browser.getWindowHandles().size();
            }
        });
    }
}

From source file:org.richfaces.photoalbum.ftest.webdriver.utils.PhotoalbumUtils.java

License:Open Source License

public static void loginWithSocial(Class<? extends SocialLoginPage> pageClass, final WebDriver browser,
        WebElement loginLink) {/*from   w w  w. j  a  v a2 s  .  com*/
    // in case this incovation targets G+ login, check if G+ credentials were provided; if not then throw IllegalArgumentException
    if (GPlusLoginPage.class.equals(pageClass) && (System.getProperty("googlePlus.username").equals("undefined")
            || System.getProperty("googlePlus.password").equals("undefined"))) {
        throw new IllegalArgumentException(
                "G+ login was invoked but no login parameters were provided. Please specify them via -DgooglePlus.username and -DgooglePlus.password!");
    }
    String originalWindow = browser.getWindowHandle();
    Graphene.guardAjax(loginLink).click();
    Graphene.waitModel().until(new Predicate<WebDriver>() {
        @Override
        public boolean apply(WebDriver input) {
            return browser.getWindowHandles().size() == 2;
        }

        @Override
        public String toString() {
            return "2 windows should be opened, but have: " + browser.getWindowHandles().size();
        }
    });
    Set<String> windowHandles = browser.getWindowHandles();
    windowHandles.remove(originalWindow);
    try {
        WebDriver window = browser.switchTo().window(windowHandles.iterator().next());
        Graphene.waitModel().until().element(By.tagName("body")).is().visible();
        SocialLoginPage loginPage = Graphene.createPageFragment(pageClass,
                window.findElement(By.tagName("body")));
        if (GPlusLoginPage.class.equals(pageClass)) {
            //G+ credential are extracted from system property
            loginPage.login(System.getProperty("googlePlus.username"),
                    System.getProperty("googlePlus.password"));
        } else {
            //FCB test account credentials are hardcoded and usable by anyone, should not result in acc being locked
            loginPage.login("vocfryc_wongwitz_1429527192@tfbnw.net", "12345");
        }
    } finally {
        browser.switchTo().window(originalWindow);
        PhotoalbumUtils.waitFor(5000);// FIXME: replace with some wait condition
        Graphene.waitModel().until(new Predicate<WebDriver>() {
            @Override
            public boolean apply(WebDriver input) {
                return browser.getWindowHandles().size() == 1;
            }

            @Override
            public String toString() {
                return "Only one window should be opened, but have: " + browser.getWindowHandles().size();
            }
        });
    }
}

From source file:org.richfaces.tests.showcase.push.TestPushCdi.java

License:Open Source License

/**
 *
 * @param currentNumberOfConsumerWindows
 * @param oldWindows//from   w w w.java  2 s  .  co  m
 * @return
 */
private String waitForConsumerWindowLoadingAfterInvocation(final int currentNumberOfConsumerWindows,
        Set<String> oldWindows) {

    webDriver.findElement(MESSAGE_CONSUMER_INVOKE_LINK).click();

    (new WebDriverWait(webDriver, 4)).until(new ExpectedCondition<Boolean>() {

        public Boolean apply(WebDriver d) {

            return (d.getWindowHandles().size() - 1) > currentNumberOfConsumerWindows;
        }
    });

    Set<String> currentWindows = webDriver.getWindowHandles();
    currentWindows.removeAll(oldWindows);

    return Collections.list(Collections.enumeration(currentWindows)).get(0);
}

From source file:org.suren.autotest.web.framework.invoker.EngineInvoker.java

License:Apache License

/**
 * urlwindow//from  w  w w .  j  a v  a 2  s. c  o m
 * @param phoenix 
 * @param params ?
 */
public static void closeWinByUrlStartWith(Phoenix phoenix, String[] params) {
    String startWith = params[0];
    WebDriver driver = phoenix.getEngine().getDriver();
    Set<String> handles = driver.getWindowHandles();
    Iterator<String> handleIt = handles.iterator();

    String currentHandle = driver.getWindowHandle();
    while (handleIt.hasNext()) {
        String handle = handleIt.next();

        driver.switchTo().window(handle);

        if (driver.getCurrentUrl().startsWith(startWith)) {
            driver.close();
            break;
        }
    }

    driver.switchTo().window(currentHandle);
}

From source file:org.suren.autotest.web.framework.invoker.EngineInvoker.java

License:Apache License

/**
 * window??//www .ja va  2s  .  c om
 * @param phoenix 
 */
public static void windowSwitch(Phoenix phoenix) {
    SeleniumEngine engine = phoenix.getEngine();
    WebDriver driver = engine.getDriver();
    Set<String> handlers = driver.getWindowHandles();
    Iterator<String> it = handlers.iterator();
    while (it.hasNext()) {
        String name = it.next();

        driver.switchTo().window(name);
    }
}