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

License:Apache License

/**
 * @throws Exception if the test fails/*www  . j av a2s  . c o m*/
 */
@Test
@Alerts(" foo \n bar <p>html snippet</p>")
public void parentAsText() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "<textarea name='textArea1'> foo \n bar " + "<p>html snippet</p>\n" + "</textarea>\n"
            + "</form></body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement textArea = driver.findElement(By.id("form1"));
    assertEquals(getExpectedAlerts()[0], textArea.getText());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*  w ww.jav  a  2s. c o m*/
 */
@Test
public void submitOnEnter() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <form action='result.html'>\n"
            + "    <input id='t' value='hello'/>\n" + "  </form>\n" + "</body>\n" + "</html>";

    final WebDriver driver = loadPage2(html);
    final WebElement field = driver.findElement(By.id("t"));

    field.sendKeys("\n");

    assertEquals(2, getMockWebConnection().getRequestCount());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   w w w .j  a va 2s . c  o m*/
 */
@Test
@NotYetImplemented
public void submitOnEnterWithoutForm() throws Exception {
    // this seem to be a bug in Selenium
    // real browsers simply ignore the missing form
    final String html = "<html>\n" + "<body>\n" + "  <input id='t' value='hello'/>\n" + "</body>\n" + "</html>";

    final WebDriver driver = loadPage2(html);
    final WebElement field = driver.findElement(By.id("t"));

    field.sendKeys("\n");

    assertEquals(1, getMockWebConnection().getRequestCount());
}

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

License:Apache License

/**
 * Verifies that a asText() returns an empty string.
 * @throws Exception if the test fails/*w  ww. jav a  2  s . c om*/
 */
@Test
public void asText() throws Exception {
    final String htmlContent = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "  <input type='text' name='foo' id='foo' value='bla'>\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.HtmlTextInputTest.java

License:Apache License

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

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

License:Apache License

/**
 * @throws Exception if the test fails/*from ww  w.jav a  2  s . c  o  m*/
 */
@Test
public void typeWhileDisabled() throws Exception {
    final String html = "<html><body><input type='text' id='p' disabled='disabled'/></body></html>";
    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));
    try {
        p.sendKeys("abc");
        fail();
    } catch (final InvalidElementStateException e) {
        // as expected
    }
    assertEquals("", p.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from  w  ww .  ja v  a2  s  .co m*/
 */
@Test
@Alerts({ "null", "null" })
public void typeDoesNotChangeValueAttribute() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n" + "  <input type='text' id='t'/>\n"
            + "  <button id='check' onclick='alert(document.getElementById(\"t\").getAttribute(\"value\"));'>"
            + "DoIt</button>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement t = driver.findElement(By.id("t"));

    final WebElement check = driver.findElement(By.id("check"));
    check.click();
    verifyAlerts(driver, getExpectedAlerts()[0]);

    t.sendKeys("abc");
    check.click();
    verifyAlerts(driver, getExpectedAlerts()[1]);
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   w  w  w.j  a v  a 2 s  .  c  om*/
 */
@Test
@Alerts({ "HtmlUnit", "HtmlUnit" })
public void typeDoesNotChangeValueAttributeWithInitialValue() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n"
            + "  <input type='text' id='t' value='HtmlUnit'/>\n"
            + "  <button id='check' onclick='alert(document.getElementById(\"t\").getAttribute(\"value\"));'>"
            + "DoIt</button>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement t = driver.findElement(By.id("t"));

    final WebElement check = driver.findElement(By.id("check"));
    check.click();
    verifyAlerts(driver, getExpectedAlerts()[0]);

    t.sendKeys("abc");
    check.click();
    verifyAlerts(driver, getExpectedAlerts()[1]);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from w w w. ja  v a  2  s  .c o  m
 */
@Test
public void preventDefault_OnKeyDown() throws Exception {
    final String html = "<html><head><script>\n" + "  function handler(e) {\n"
            + "    if (e && e.target.value.length > 2)\n" + "      e.preventDefault();\n"
            + "    else if (!e && window.event.srcElement.value.length > 2)\n" + "      return false;\n"
            + "  }\n" + "  function init() {\n" + "    document.getElementById('p').onkeydown = handler;\n"
            + "  }\n" + "</script></head>\n" + "<body onload='init()'>\n"
            + "<input type='text' id='p'></input>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));
    p.sendKeys("abcd");
    assertEquals("abc", p.getAttribute("value"));
}