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:com.springer.omelet.driver.DriverUtility.java

License:Apache License

/***
 * Switching between windows./*from  w  w w  . j a  v  a  2  s  .  c om*/
 * 
 * @param driver
 * @param sString
 *            :Target window Title
 * @return:True if window switched
 */
public static boolean switchToWindow(WebDriver driver, String sString) {
    String currentHandle = driver.getWindowHandle();
    Set<String> handles = driver.getWindowHandles();
    if (handles.size() >= 1) {
        for (String handle : handles) {
            LOGGER.debug("Switching to other window");
            driver.switchTo().window(handle);
            if (driver.getTitle().contains(sString)) {
                LOGGER.info("switched to window with title:" + sString);
                return true;
            }
        }
        driver.switchTo().window(currentHandle);

        LOGGER.info("Window with title:" + sString + " Not present,Not able to switch");
        return false;
    } else {
        LOGGER.info("There is only one window handle :" + currentHandle);
        return false;
    }
}

From source file:com.synapticpath.naica.selenium.SeleniumOp.java

License:Open Source License

protected void processActionSuccess(Action action) {
    WebDriver driver = SeleniumTestContext.getInstance().getDriver();
    Iterator<String> iter = driver.getWindowHandles().iterator();
    String firstHandle = iter.next();
    String lastHandle = firstHandle;
    while (iter.hasNext()) {
        lastHandle = iter.next();//from w  w w.  j  a v  a  2 s  . co  m
    }
    if (!lastHandle.equals(firstHandle)) {
        driver.switchTo().window(lastHandle);
    }
}

From source file:com.thoughtworks.selenium.webdriven.commands.GetAllWindowNames.java

License:Apache License

@Override
protected String[] handleSeleneseCommand(WebDriver driver, String ignored, String alsoIgnored) {
    String current = driver.getWindowHandle();

    List<String> attributes = new ArrayList<>();
    for (String handle : driver.getWindowHandles()) {
        driver.switchTo().window(handle);
        attributes.add(((JavascriptExecutor) driver).executeScript("return window.name").toString());
    }/*w  ww . j  a va2s.c  o  m*/

    driver.switchTo().window(current);

    return attributes.toArray(new String[attributes.size()]);

}

From source file:com.thoughtworks.selenium.webdriven.commands.GetAllWindowTitles.java

License:Apache License

@Override
protected String[] handleSeleneseCommand(WebDriver driver, String ignored, String alsoIgnored) {
    String current = driver.getWindowHandle();

    List<String> attributes = new ArrayList<>();
    for (String handle : driver.getWindowHandles()) {
        driver.switchTo().window(handle);
        attributes.add(driver.getTitle());
    }/* w  w w.  ja  va  2  s. c  o m*/

    driver.switchTo().window(current);

    return attributes.toArray(new String[attributes.size()]);

}

From source file:com.thoughtworks.selenium.webdriven.commands.GetAttributeFromAllWindows.java

License:Apache License

@Override
protected String[] handleSeleneseCommand(WebDriver driver, String attributeName, String ignored) {
    String current = driver.getWindowHandle();

    List<String> attributes = new ArrayList<>();
    for (String handle : driver.getWindowHandles()) {
        driver.switchTo().window(handle);
        String value = (String) ((JavascriptExecutor) driver).executeScript("return '' + window[arguments[0]];",
                attributeName);//from w  w w .  j  ava  2  s .co  m
        attributes.add(value);
    }

    driver.switchTo().window(current);

    return attributes.toArray(new String[attributes.size()]);
}

From source file:com.thoughtworks.selenium.webdriven.commands.Windows.java

License:Apache License

public void selectPopUp(WebDriver driver, String windowID) {
    if ("null".equals(windowID) || "".equals(windowID)) {
        Set<String> windowHandles = driver.getWindowHandles();
        windowHandles.remove(originalWindowHandle);
        if (windowHandles.size() > 0) {
            driver.switchTo().window(windowHandles.iterator().next());
        } else {//  w w w . ja v  a 2  s  .  c  o m
            throw new SeleniumException("Unable to find a popup window");
        }
    } else {
        selectWindow(driver, windowID);
    }
}

From source file:com.thoughtworks.selenium.webdriven.commands.Windows.java

License:Apache License

private void selectWindowWithTitle(WebDriver driver, String title) {
    String current = driver.getWindowHandle();
    for (String handle : driver.getWindowHandles()) {
        driver.switchTo().window(handle);
        if (title.equals(driver.getTitle())) {
            return;
        }//  ww w . jav  a 2 s  . co m
    }

    driver.switchTo().window(current);
    throw new SeleniumException("Unable to select window with title: " + title);
}

From source file:com.thoughtworks.selenium.webdriven.commands.Windows.java

License:Apache License

/**
 * Selects the only <code>_blank</code> window. A window open with <code>target='_blank'</code>
 * will have a <code>window.name = null</code>.
 * <p/>// w w  w.ja v a 2  s. co m
 * <p>
 * This method assumes that there will only be one single <code>_blank</code> window and selects
 * the first one with no name. Therefore if for any reasons there are multiple windows with
 * <code>window.name = null</code> the first found one will be selected.
 * <p/>
 * <p>
 * If none of the windows have <code>window.name = null</code> the last selected one will be
 * re-selected and a {@link SeleniumException} will be thrown.
 * 
 * @throws NoSuchWindowException if no window with <code>window.name = null</code> is found.
 */
public void selectBlankWindow(WebDriver driver) {
    String current = driver.getWindowHandle();
    // Find the first window without a "name" attribute
    List<String> handles = new ArrayList<String>(driver.getWindowHandles());
    for (String handle : handles) {
        // the original window will never be a _blank window, so don't even look at it
        // this is also important to skip, because the original/root window won't have
        // a name either, so if we didn't know better we might think it's a _blank popup!
        if (handle.equals(originalWindowHandle)) {
            continue;
        }
        driver.switchTo().window(handle);
        String value = (String) ((JavascriptExecutor) driver).executeScript("return window.name;");
        if (value == null || "".equals(value)) {
            // We found it!
            return;
        }
    }
    // We couldn't find it
    driver.switchTo().window(current);
    throw new SeleniumException("Unable to select window _blank");
}

From source file:com.thoughtworks.selenium.webdriven.Windows.java

License:Apache License

public void selectPopUp(WebDriver driver, String windowID) {
    if ("null".equals(windowID) || "".equals(windowID)) {
        Set<String> windowHandles = driver.getWindowHandles();
        windowHandles.remove(originalWindowHandle);
        if (!windowHandles.isEmpty()) {
            driver.switchTo().window(windowHandles.iterator().next());
        } else {//from  w  ww.  jav a2 s.c o  m
            throw new SeleniumException("Unable to find a popup window");
        }
    } else {
        selectWindow(driver, windowID);
    }
}

From source file:com.vaadin.testbench.TestBenchDriverTest.java

@Test
public void testTestBenchDriverActsAsProxy() {
    FirefoxDriver mockDriver = createMock(FirefoxDriver.class);
    mockDriver.close();//w w  w . ja va 2 s  . c o m
    expectLastCall().once();
    WebElement mockElement = createNiceMock(WebElement.class);
    expect(mockDriver.findElement(isA(By.class))).andReturn(mockElement);
    List<WebElement> elements = Arrays.asList(mockElement);
    expect(mockDriver.findElements(isA(By.class))).andReturn(elements);
    mockDriver.get("foo");
    expectLastCall().once();
    expect(mockDriver.getCurrentUrl()).andReturn("foo");
    expect(mockDriver.getPageSource()).andReturn("<html></html>");
    expect(mockDriver.getTitle()).andReturn("bar");
    expect(mockDriver.getWindowHandle()).andReturn("baz");
    Set<String> handles = new HashSet<String>();
    expect(mockDriver.getWindowHandles()).andReturn(handles);
    Options mockOptions = createNiceMock(Options.class);
    expect(mockDriver.manage()).andReturn(mockOptions);
    Navigation mockNavigation = createNiceMock(Navigation.class);
    expect(mockDriver.navigate()).andReturn(mockNavigation);
    mockDriver.quit();
    expectLastCall().once();
    expect(((JavascriptExecutor) mockDriver).executeScript(anyObject(String.class))).andStubReturn(true);
    TargetLocator mockTargetLocator = createNiceMock(TargetLocator.class);
    expect(mockDriver.switchTo()).andReturn(mockTargetLocator);
    replay(mockDriver);

    // TestBenchDriverProxy driver = new TestBenchDriverProxy(mockDriver);
    WebDriver driver = TestBench.createDriver(mockDriver);
    driver.close();
    By mockBy = createNiceMock(By.class);
    assertTrue(driver.findElement(mockBy) instanceof TestBenchElementCommands);
    assertTrue(driver.findElements(mockBy).get(0) instanceof TestBenchElementCommands);
    driver.get("foo");
    assertEquals("foo", driver.getCurrentUrl());
    assertEquals("<html></html>", driver.getPageSource());
    assertEquals("bar", driver.getTitle());
    assertEquals("baz", driver.getWindowHandle());
    assertEquals(handles, driver.getWindowHandles());
    assertEquals(mockOptions, driver.manage());
    assertEquals(mockNavigation, driver.navigate());
    driver.quit();
    assertEquals(mockTargetLocator, driver.switchTo());

    verify(mockDriver);
}