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.common.ExpectedConditions.java

License:Apache License

public static ExpectedCondition<Boolean> numberOfWindowsToBe(final int expectedNumberOfWindows) {
    return new ExpectedCondition<Boolean>() {
        @Override/*from  w  w  w  .  j  av  a2s  .co  m*/
        public Boolean apply(WebDriver driver) {
            try {
                return driver.getWindowHandles().size() == expectedNumberOfWindows;
            } catch (WebDriverException e) {
                return null;
            }
        }

        @Override
        public String toString() {
            return "number of open windows to be " + expectedNumberOfWindows;
        }
    };
}

From source file:com.evidon.areweprivateyet.Crawler.java

License:Open Source License

private void killPopups(String baseWindow, WebDriver driver) {
    // close any new popups.
    for (String handle : driver.getWindowHandles()) {
        if (!handle.equals(baseWindow)) {
            WebDriver popup = driver.switchTo().window(handle);
            log("\tClosing popup: " + popup.getCurrentUrl());
            popup.close();/*from   ww  w.  jav  a  2 s  . c  o  m*/

            // TODO: need to see if this breaks when there is a modal.
        }
    }

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

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchor2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs// w ww . j  a v  a2  s .c  o  m
 */
@Test
public void clickBlankTargetHashOnly() throws Exception {
    final String html = "<html>\n" + "<head><title>foo</title></head>\n" + "<body>\n"
            + "<a id='a' target='_blank' href='#'>Foo</a>\n" + "</body></html>\n";

    final WebDriver driver = loadPage2(html);
    assertEquals(1, driver.getWindowHandles().size());

    final WebElement tester = driver.findElement(By.id("a"));
    tester.click();
    assertEquals(2, driver.getWindowHandles().size());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchorTest.java

License:Apache License

/**
 * @exception Exception If the test fails
 *///from   www.  ja  va 2 s . com
@Test
@BuggyWebDriver(IE)
public void shiftClick() throws Exception {
    final String html = "<html><head><title>First</title></head><body>\n" + "<a href='" + URL_SECOND
            + "'>Click Me</a>\n" + "</form></body></html>";

    getMockWebConnection().setResponse(URL_SECOND, "<head><title>Second</title>");
    final WebDriver driver = loadPage2(html);

    final WebElement link = driver.findElement(By.linkText("Click Me"));

    final String originalTitle = driver.getTitle();

    final int windowsSize = driver.getWindowHandles().size();

    new Actions(driver).moveToElement(link).keyDown(Keys.SHIFT).click().keyUp(Keys.SHIFT).perform();

    assertEquals("Should have opened a new window", windowsSize + 1, driver.getWindowHandles().size());
    assertEquals("Should not have navigated away", originalTitle, driver.getTitle());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchorTest.java

License:Apache License

/**
 * @exception Exception If the test fails
 */// w  w w.  ja va  2 s. c om
@Test
@BuggyWebDriver({ IE, FF })
public void ctrlClick() throws Exception {
    final String html = "<html><head><title>First</title></head><body>\n" + "<a href='" + URL_SECOND
            + "'>Click Me</a>\n" + "</form></body></html>";

    getMockWebConnection().setResponse(URL_SECOND, "<head><title>Second</title>");
    final WebDriver driver = loadPage2(html);

    final WebElement link = driver.findElement(By.linkText("Click Me"));

    final String originalTitle = driver.getTitle();

    final int windowsSize = driver.getWindowHandles().size();

    new Actions(driver).moveToElement(link).keyDown(Keys.CONTROL).click().keyUp(Keys.CONTROL).perform();

    assertEquals("Should have opened a new window", windowsSize + 1, driver.getWindowHandles().size());
    assertEquals("Should not have navigated away", originalTitle, driver.getTitle());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLAnchorElement2Test.java

License:Apache License

private void javascriptTarget(final String target, final int newWindows, final String[] expectedAlerts)
        throws Exception {
    final String html = "<html>\n" + "<head><title>main</title></head>\n" + "<body>\n"
            + "  <iframe id='testFrame' src='" + URL_SECOND + "'></iframe>\n" + "</body></html>";

    final String secondHtml = "<html>\n" + "<head><title>inner</title></head>\n" + "<body>\n"
            + "  <a id='tester' " + target
            + " href='javascript: try { alert(document.title); } catch(e) { alert(e); }'>no href</a>\n"
            + "</body>\n" + "</html>";

    getMockWebConnection().setResponse(URL_SECOND, secondHtml);

    setExpectedAlerts(expectedAlerts);// www.j a v a2s  .  c  o m
    final WebDriver driver = loadPage2(html);

    driver.switchTo().frame("testFrame");
    assertEquals(1, driver.getWindowHandles().size());
    driver.findElement(By.id("tester")).click();
    assertEquals(1 + newWindows, driver.getWindowHandles().size());

    verifyAlerts(driver, getExpectedAlerts());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement2Test.java

License:Apache License

/**
 * Test the use of innerHTML to set new HTML code.
 * @throws Exception if the test fails/*  w w w.j a va  2  s .c  om*/
 */
@Test
@Alerts({ "Old = <b>Old innerHTML</b><!-- old comment -->", "New =  <b><i id=\"newElt\">New cell value</i></b>",
        "I" })
public void getSetInnerHTMLComplex() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <title>test</title>\n" + "  <script>\n"
            + "  function doTest() {\n" + "    var myNode = document.getElementById('myNode');\n"
            + "    alert('Old = ' + myNode.innerHTML);\n"
            + "    myNode.innerHTML = ' <b><i id=\"newElt\">New cell value</i></b>';\n"
            + "    alert('New = ' + myNode.innerHTML);\n"
            + "    alert(document.getElementById('newElt').tagName);\n" + "  }\n" + "  </script>\n"
            + "</head>\n" + "<body onload='doTest()'>\n"
            + "<p id='myNode'><b>Old innerHTML</b><!-- old comment --></p>\n" + "</body>\n" + "</html>";

    final WebDriver driver = loadPageWithAlerts2(html);

    final WebElement pElt = driver.findElement(By.id("myNode"));
    assertEquals("p", pElt.getTagName());

    final WebElement elt = driver.findElement(By.id("newElt"));
    assertEquals("New cell value", elt.getText());
    assertEquals(1, driver.getWindowHandles().size());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement2Test.java

License:Apache License

/**
 * Test the use of outerHTML to set new HTML code.
 * @throws Exception if the test fails//from  w ww.  j  a  v  a2s  .com
 */
@Test
@Alerts({ "Old = <b id=\"innerNode\">Old outerHTML</b>", "New =  <b><i id=\"newElt\">New cell value</i></b>",
        "I" })
public void getSetOuterHTMLComplex() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <title>test</title>\n" + "  <script>\n"
            + "  function doTest() {\n" + "    var myNode = document.getElementById('myNode');\n"
            + "    var innerNode = document.getElementById('innerNode');\n"
            + "    alert('Old = ' + innerNode.outerHTML);\n"
            + "    innerNode.outerHTML = ' <b><i id=\"newElt\">New cell value</i></b>';\n"
            + "    alert('New = ' + myNode.innerHTML);\n"
            + "    alert(document.getElementById('newElt').tagName);\n" + "  }\n" + "  </script>\n"
            + "</head>\n" + "<body onload='doTest()'>\n"
            + "<p id='myNode'><b id='innerNode'>Old outerHTML</b></p>\n" + "</body>\n" + "</html>";

    final WebDriver driver = loadPageWithAlerts2(html);

    final WebElement pElt = driver.findElement(By.id("myNode"));
    assertEquals("p", pElt.getTagName());

    final WebElement elt = driver.findElement(By.id("newElt"));
    assertEquals("New cell value", elt.getText());
    assertEquals(1, driver.getWindowHandles().size());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.Window2Test.java

License:Apache License

/**
 * @throws Exception if the test fails/* w ww  . jav a 2  s  .  c  om*/
 */
@Test
public void setLocation() throws Exception {
    final String firstContent = "<html>\n" + "<head><title>First</title></head>\n" + "<body>\n"
            + "<form name='form1'>\n" + "  <a id='link' onClick='location=\"" + URL_SECOND
            + "\";'>Click me</a>\n" + "</form>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";

    getMockWebConnection().setResponse(URL_SECOND, secondContent);

    final WebDriver driver = loadPage2(firstContent);
    assertEquals("First", driver.getTitle());
    assertEquals(1, driver.getWindowHandles().size());

    driver.findElement(By.id("link")).click();
    assertEquals("Second", driver.getTitle());

    assertEquals(1, driver.getWindowHandles().size());
    assertEquals(new String[] { "", "second/" }, getMockWebConnection().getRequestedUrls(URL_FIRST));
    assertEquals(URL_SECOND.toString(), driver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.Window2Test.java

License:Apache License

/**
 * @throws Exception if the test fails//from   w w w.  j  a  v a  2 s . co m
 */
@Test
public void setWindowLocation() throws Exception {
    final String firstContent = "<html>\n" + "<head><title>First</title></head>\n" + "<body>\n"
            + "<form name='form1'>\n" + "  <a id='link' onClick='window.location=\"" + URL_SECOND
            + "\";'>Click me</a>\n" + "</form>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";

    getMockWebConnection().setResponse(URL_SECOND, secondContent);

    final WebDriver driver = loadPage2(firstContent);
    assertEquals("First", driver.getTitle());
    assertEquals(1, driver.getWindowHandles().size());

    driver.findElement(By.id("link")).click();
    assertEquals("Second", driver.getTitle());

    assertEquals(1, driver.getWindowHandles().size());
    assertEquals(new String[] { "", "second/" }, getMockWebConnection().getRequestedUrls(URL_FIRST));
    assertEquals(URL_SECOND.toString(), driver.getCurrentUrl());
}