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.HtmlAnchorTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*from w  w w. j  a v  a  2  s .  com*/
 */
@Test
public void javascriptWithReturn() throws Exception {
    final String html = "<html><head><title>First</title></head><body>\n"
            + "  <a id='myLink' href='javascript:return true'>hi</a>\n" + "</body></html>";
    final WebDriver webDriver = loadPage2(html);
    webDriver.findElement(By.id("myLink")).click();
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from www  .  j  a  v a 2  s.com
 */
@Test
public void javascriptWithReturnWhitespace() throws Exception {
    final String html = "<html><head><title>First</title></head><body>\n"
            + "  <a id='myLink' href='javascript: return true'>hi</a>\n" + "</body></html>";
    final WebDriver webDriver = loadPage2(html);
    webDriver.findElement(By.id("myLink")).click();
}

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

License:Apache License

/**
 * @exception Exception If the test fails
 *///  w w  w.j ava  2 s .  co  m
@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
 *//*from   ww  w  .j a v a  2 s . c o m*/
@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.html.HtmlArea2Test.java

License:Apache License

/**
 * @throws Exception if the test fails//from  www  .  j a  v  a 2s .  c  om
 */
@Test
public void referer() throws Exception {
    final WebDriver driver = createWebClient("");

    driver.get(URL_FIRST.toExternalForm());
    driver.findElement(By.id("third")).click();

    final Map<String, String> lastAdditionalHeaders = getMockWebConnection().getLastAdditionalHeaders();
    assertEquals(URL_FIRST.toString(), lastAdditionalHeaders.get("Referer"));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*w  w w.  j a v a 2 s.c  o  m*/
 */
@Test
public void isDisplayed() throws Exception {
    final String html = "<html><head><title>Page A</title></head>\n" + "<body>"
            + "  <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.HtmlArea2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs/*  w  w  w. j a va  2 s  .  com*/
 */
@Test
public void isDisplayedHiddenImage() throws Exception {
    final String html = "<html><head><title>Page A</title></head>\n" + "<body>"
            + "  <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.HtmlArea2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs/*from  w  w  w  .  ja  v  a  2s .  c  om*/
 */
@Test
public void isDisplayedHiddenMap() throws Exception {
    final String html = "<html><head><title>Page A</title></head>\n" + "<body>"
            + "  <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.HtmlArea2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs/*from  w  w w . jav a  2s . co  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>"
            + "  <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.HtmlArea2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs/*from  w  w w  . j  a  va2  s.com*/
 */
@Test
public void isDisplayedMissingImage() throws Exception {
    final String html = "<html><head><title>Page A</title></head>\n" + "<body>\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("myMap")).isDisplayed();
    assertFalse(displayed);

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