Example usage for org.openqa.selenium WebDriver findElement

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

Introduction

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

Prototype

@Override
WebElement findElement(By by);

Source Link

Document

Find the first WebElement using the given method.

Usage

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from www  . j  a v  a 2 s  .co  m*/
 */
@Test
public void isDisplayed() throws Exception {
    final String html = "<html><head><title>Page A</title></head>\n" + "<body>\n"
            + "  <img id='myImg' usemap='#imgmap'"
            + " src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAA"
            + "HElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='>\n"
            + "  <map id='myMap' name='imgmap'>\n" + "    <area id='myArea' shape='rect' coords='0,0,1,1'>\n"
            + "  </map>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);

    boolean displayed = driver.findElement(By.id("myImg")).isDisplayed();
    assertTrue(displayed);

    displayed = driver.findElement(By.id("myMap")).isDisplayed();
    assertTrue(displayed);

    displayed = driver.findElement(By.id("myArea")).isDisplayed();
    assertTrue(displayed);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*  w  w w . j  a v a2  s  . c om*/
 */
@Test
public void isDisplayedHiddenImage() throws Exception {
    final String html = "<html><head><title>Page A</title></head>\n" + "<body>\n"
            + "  <img id='myImg' usemap='#imgmap' style='display: none'"
            + " src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAA"
            + "HElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='>\n"
            + "  <map id='myMap' name='imgmap'>\n" + "    <area id='myArea' shape='rect' coords='0,0,1,1'>\n"
            + "  </map>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);

    boolean displayed = driver.findElement(By.id("myImg")).isDisplayed();
    assertFalse(displayed);

    displayed = driver.findElement(By.id("myMap")).isDisplayed();
    assertFalse(displayed);

    displayed = driver.findElement(By.id("myArea")).isDisplayed();
    assertFalse(displayed);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/* ww w .  j  av  a 2s .  c  o m*/
 */
@Test
public void isDisplayedHiddenMap() throws Exception {
    final String html = "<html><head><title>Page A</title></head>\n" + "<body>\n"
            + "  <img id='myImg' usemap='#imgmap'"
            + " src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAA"
            + "HElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='>\n"
            + "  <map id='myMap' name='imgmap' style='display: none'>\n"
            + "    <area id='myArea' shape='rect' coords='0,0,1,1'>\n" + "  </map>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);

    boolean displayed = driver.findElement(By.id("myImg")).isDisplayed();
    assertTrue(displayed);

    displayed = driver.findElement(By.id("myMap")).isDisplayed();
    assertTrue(displayed);

    displayed = driver.findElement(By.id("myArea")).isDisplayed();
    assertTrue(displayed);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*w w  w  .ja v  a  2s  .c  o m*/
 */
@Test
@Alerts({ "false", "false", "false", "false", "false", "true" })
public void isDisplayedEmptyArea() throws Exception {
    final String html = "<html><head><title>Page A</title></head>\n" + "<body>\n"
            + "  <img id='myImg' usemap='#imgmap'"
            + " src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAA"
            + "HElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='>\n"
            + "  <map id='myMap' name='imgmap'>\n" + "    <area id='myArea1' shape='rect' coords='0,0,0,1'>\n"
            + "    <area id='myArea2' shape='rect' coords='0,0,1,0'>\n"
            + "    <area id='myArea3' shape='rect' coords='0,0,0,0'>\n"
            + "    <area id='myArea4' shape='rect' >\n" + "    <area id='myArea5' >\n"
            + "    <area id='myArea6' shape='rect' coords='0,0,1,1'>\n" + "  </map>\n" + "</body></html>";

    final String[] expected = getExpectedAlerts();

    setExpectedAlerts(new String[] {});
    final WebDriver driver = loadPageWithAlerts2(html);

    boolean displayed = driver.findElement(By.id("myArea1")).isDisplayed();
    assertEquals(Boolean.parseBoolean(expected[0]), displayed);

    displayed = driver.findElement(By.id("myArea2")).isDisplayed();
    assertEquals(Boolean.parseBoolean(expected[1]), displayed);

    displayed = driver.findElement(By.id("myArea3")).isDisplayed();
    assertEquals(Boolean.parseBoolean(expected[2]), displayed);

    displayed = driver.findElement(By.id("myArea4")).isDisplayed();
    assertEquals(Boolean.parseBoolean(expected[3]), displayed);

    displayed = driver.findElement(By.id("myArea5")).isDisplayed();
    assertEquals(Boolean.parseBoolean(expected[4]), displayed);

    displayed = driver.findElement(By.id("myArea6")).isDisplayed();
    assertEquals(Boolean.parseBoolean(expected[5]), displayed);
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   ww w  .  j  a  v a 2s  .  c o m*/
 */
@Test
public void click_javascriptUrl() throws Exception {
    try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-jpg.img")) {
        final byte[] directBytes = IOUtils.toByteArray(is);
        final URL urlImage = new URL(URL_FIRST, "img.jpg");
        final List<NameValuePair> emptyList = Collections.emptyList();
        getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok", "image/jpg", emptyList);
    }

    final String html = "<html><head><title>foo</title></head><body>\n"
            + "<img src='img.jpg' width='145' height='126' usemap='#somename'>\n" + "<map name='somename'>\n"
            + "  <area href='javascript:alert(\"clicked\")' id='a2' shape='rect' coords='0,0,30,30'/>\n"
            + "</map></body></html>";

    final WebDriver driver = loadPage2(html);
    final Page page;
    if (driver instanceof HtmlUnitDriver) {
        page = getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
    } else {
        page = null;
    }

    verifyAlerts(driver);

    driver.findElement(By.id("a2")).click();

    verifyAlerts(driver, "clicked");
    if (driver instanceof HtmlUnitDriver) {
        final Page secondPage = getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertSame(page, secondPage);
    }
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from  w  w w. j a  v a 2  s . c o m*/
 */
@Test
public void click_javascriptUrlMixedCase() throws Exception {
    try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-jpg.img")) {
        final byte[] directBytes = IOUtils.toByteArray(is);
        final URL urlImage = new URL(URL_FIRST, "img.jpg");
        final List<NameValuePair> emptyList = Collections.emptyList();
        getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok", "image/jpg", emptyList);
    }

    final String html = "<html><head><title>foo</title></head><body>\n"
            + "<img src='img.jpg' width='145' height='126' usemap='#somename'>\n" + "<map name='somename'>\n"
            + "  <area href='javasCRIpT:alert(\"clicked\")' id='a2' shape='rect' coords='0,0,30,30'/>\n"
            + "</map></body></html>";

    final WebDriver driver = loadPage2(html);
    final Page page;
    if (driver instanceof HtmlUnitDriver) {
        page = getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
    } else {
        page = null;
    }

    verifyAlerts(driver);

    driver.findElement(By.id("a2")).click();

    verifyAlerts(driver, "clicked");
    if (driver instanceof HtmlUnitDriver) {
        final Page secondPage = getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertSame(page, secondPage);
    }
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from www. j  a v a  2s. c om
 */
@Test
public void click_javascriptUrlLeadingWhitespace() throws Exception {
    try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-jpg.img")) {
        final byte[] directBytes = IOUtils.toByteArray(is);
        final URL urlImage = new URL(URL_FIRST, "img.jpg");
        final List<NameValuePair> emptyList = Collections.emptyList();
        getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok", "image/jpg", emptyList);
    }

    final String html = "<html><head><title>foo</title></head><body>\n"
            + "<img src='img.jpg' width='145' height='126' usemap='#somename'>\n" + "<map name='somename'>\n"
            + "  <area href='    javascript:alert(\"clicked\")' id='a2' shape='rect' coords='0,0,30,30'/>\n"
            + "</map></body></html>";

    final WebDriver driver = loadPage2(html);
    final Page page;
    if (driver instanceof HtmlUnitDriver) {
        page = getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
    } else {
        page = null;
    }

    verifyAlerts(driver);

    driver.findElement(By.id("a2")).click();

    verifyAlerts(driver, "clicked");
    if (driver instanceof HtmlUnitDriver) {
        final Page secondPage = getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertSame(page, secondPage);
    }
}

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

License:Apache License

/**
 * In action "this" should be the window and not the area.
 * @throws Exception if the test fails// ww  w .  ja v  a 2  s .  c o m
 */
@Test
public void thisInJavascriptHref() throws Exception {
    try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-jpg.img")) {
        final byte[] directBytes = IOUtils.toByteArray(is);
        final URL urlImage = new URL(URL_FIRST, "img.jpg");
        final List<NameValuePair> emptyList = Collections.emptyList();
        getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok", "image/jpg", emptyList);
    }

    final String html = "<html><head><title>foo</title></head><body>\n"
            + "<img src='img.jpg' width='145' height='126' usemap='#somename'>\n" + "<map name='somename'>\n"
            + "  <area href='javascript:alert(this == window)' id='a2' shape='rect' coords='0,0,30,30'/>\n"
            + "</map></body></html>";

    final WebDriver driver = loadPage2(html);
    final Page page;
    if (driver instanceof HtmlUnitDriver) {
        page = getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
    } else {
        page = null;
    }

    verifyAlerts(driver);

    driver.findElement(By.id("a2")).click();

    verifyAlerts(driver, "true");
    if (driver instanceof HtmlUnitDriver) {
        final Page secondPage = getWebWindowOf((HtmlUnitDriver) driver).getEnclosedPage();
        assertSame(page, secondPage);
    }
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from  ww w .ja v a 2s .  c o  m
 */
@Test
@Alerts(DEFAULT = "[object HTMLUnknownElement]", IE = "[object HTMLBGSoundElement]")
public void simpleScriptable() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head>\n" + "<script>\n"
            + "  function test() {\n" + "    alert(document.getElementById('myId'));\n" + "  }\n"
            + "</script>\n" + "</head><body onload='test()'>\n" + "  <bgsound id='myId'/>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver && getExpectedAlerts()[0].contains("Sound")) {
        assertTrue(HtmlBackgroundSound.class.isInstance(toHtmlElement(driver.findElement(By.id("myId")))));
    }
}

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

License:Apache License

/**
 * According to the HTML spec, the default type for a button is "submit".
 * IE is different than the HTML spec and has a default type of "button".
 * @throws Exception if the test fails//from  w  ww  . ja  v a2 s  .  c o m
 */
@Test
@Alerts({ "submit", "1", "button-pushme", "Second" })
public void defaultButtonType_StandardsCompliantBrowser() throws Exception {
    final String firstContent = "<html><head><title>First</title></head><body>\n" + "<form id='form1' action='"
            + URL_SECOND + "' method='post'>\n"
            + "  <button name='button' id='button' value='pushme'>PushMe</button>\n" + "</form></body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body'></body></html>";

    getMockWebConnection().setResponse(URL_SECOND, secondContent);

    final WebDriver driver = loadPage2(firstContent);
    final WebElement button = driver.findElement(By.id("button"));

    assertEquals(getExpectedAlerts()[0], button.getAttribute("type"));

    button.click();

    final List<NameValuePair> params = getMockWebConnection().getLastParameters();
    assertEquals(getExpectedAlerts()[1], "" + params.size());

    if (params.size() > 0) {
        assertEquals(getExpectedAlerts()[2], params.get(0).getName() + "-" + params.get(0).getValue());
    }
    assertEquals(getExpectedAlerts()[3], driver.getTitle());
}