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

License:Apache License

/**
 * @throws Exception if an error occurs/* w w w. j a v a2s . c  om*/
 */
@Test
public void asText() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <link id='l' href='file1.css'>\n" + "</head>\n"
            + "<body>\n" + "</body>\n" + "</html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    final String text = driver.findElement(By.id("l")).getText();
    assertEquals("", text);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//  w ww.ja  v  a 2  s. c  o  m
 */
@Test
public void isDisplayed() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <link id='l' href='file1.css'>\n" + "</head>\n"
            + "<body>\n" + "</body>\n" + "</html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    final boolean displayed = driver.findElement(By.id("l")).isDisplayed();
    assertFalse(displayed);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from w  w  w.j  ava  2  s  .  c o  m
 */
@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);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from   w  w  w  .  j  ava2s.  c om*/
 */
@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);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from  w  w  w  . jav  a2s.  c  o  m
 */
@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);

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

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from   w  w  w  .j  a  va  2s  .c  om*/
 */
@Test
public void asText() throws Exception {
    final String html = "<html><head><meta id='m' http-equiv='a' content='b'></head><body></body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    final String text = driver.findElement(By.id("m")).getText();
    assertEquals("", text);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//  w  w  w.ja  va 2  s .  c om
 */
@Test
public void isDisplayed() throws Exception {
    final String html = "<html><head><meta id='m' http-equiv='a' content='b'></head><body></body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    final boolean displayed = driver.findElement(By.id("m")).isDisplayed();
    assertFalse(displayed);
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from   w  w w .java 2  s . c o  m
 */
@Test
public void formValues() throws Exception {
    final String html = "<html><body>\n" + "<form name='item' method='get'>\n" + "  <noscript>\n"
            + "    <input type=hidden name='__webpage_no_js__' value='1'>\n" + "  </noscript>\n"
            + "  <input type=hidden name='myParam' value='myValue'>\n"
            + "  <input type='submit' id='clickMe'>\n" + "</form>\n" + "</body></html>";

    final WebDriver webDriver = loadPage2(html);
    webDriver.findElement(By.id("clickMe")).click();

    assertFalse(webDriver.getCurrentUrl().contains("__webpage_no_js__"));
}

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

License:Apache License

/**
 * Verifies that a asText() returns an empty string.
 * @throws Exception if the test fails/*ww w  .  j  av  a 2  s. c o m*/
 */
@Test
public void asText() throws Exception {
    final String htmlContent = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "  <input type='number' name='foo' id='foo' value='123'>\n" + "</form></body></html>";

    final WebDriver driver = loadPage2(htmlContent);

    final WebElement input = driver.findElement(By.id("foo"));
    assertEquals("", input.getText());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from ww w .  j a v  a 2  s.  c  o  m*/
 */
@Test
public void type() throws Exception {
    final String html = "<html><head></head><body><input type='number' id='t'/></body></html>";
    final WebDriver driver = loadPage2(html);
    final WebElement t = driver.findElement(By.id("t"));
    t.sendKeys("123");
    assertEquals("123", t.getAttribute("value"));
    t.sendKeys("\b");
    assertEquals("12", t.getAttribute("value"));
    t.sendKeys("\b");
    assertEquals("1", t.getAttribute("value"));
    t.sendKeys("\b");
    assertEquals("", t.getAttribute("value"));
    t.sendKeys("\b");
    assertEquals("", t.getAttribute("value"));
}