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

License:Apache License

/**
 * @throws Exception if the test fails/*ww w  .  j  a  v a 2 s .c  o  m*/
 */
@Test
public void typeWhileDisabled() throws Exception {
    final String html = "<html><body><input type='number' 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.HtmlNumberInputTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from   w  ww  .  j  a  v a  2 s  . c o m
 */
@Test
@Alerts({ "null", "null" })
public void typeDoesNotChangeValueAttribute() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n" + "  <input type='number' 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.HtmlNumberInputTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from   w w  w.j a v a  2  s  . c om
 */
@Test
@Alerts({ "1234", "1234" })
public void typeDoesNotChangeValueAttributeWithInitialValue() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n"
            + "  <input type='number' id='t' value='1234'/>\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("987");
    check.click();
    verifyAlerts(driver, getExpectedAlerts()[1]);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs// ww  w .  ja  v  a  2s.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='number' id='p'></input>\n" + "</body></html>";

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

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

License:Apache License

/**
 * @throws Exception if an error occurs//from   w  w  w.j a v  a 2s  .c om
 */
@Test
public void preventDefault_OnKeyPress() 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').onkeypress = handler;\n"
            + "  }\n" + "</script></head>\n" + "<body onload='init()'>\n"
            + "<input type='number' id='p'></input>\n" + "</body></html>";

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

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

License:Apache License

/**
 * @throws Exception if an error occurs//from  ww w. j a  v  a2 s. c o m
 */
@Test
public void typeOnChange() throws Exception {
    final String html = "<html><head></head><body>\n" + "<input type='number' id='p' value='1234'"
            + " onChange='alert(\"foo\");alert(event.type);'" + " onBlur='alert(\"boo\");alert(event.type);'>\n"
            + "<button id='b'>some button</button>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement p = driver.findElement(By.id("p"));
    p.sendKeys("567");

    assertEquals(Collections.emptyList(), getCollectedAlerts(driver));

    // trigger lost focus
    driver.findElement(By.id("b")).click();
    final String[] expectedAlerts1 = { "foo", "change", "boo", "blur" };
    assertEquals(expectedAlerts1, getCollectedAlerts(driver, 4));

    // set only the focus but change nothing
    p.click();
    assertTrue(getCollectedAlerts(driver, 1).isEmpty());

    // trigger lost focus
    driver.findElement(By.id("b")).click();
    final String[] expectedAlerts2 = { "boo", "blur" };
    assertEquals(expectedAlerts2, getCollectedAlerts(driver, 2));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from  w w  w  .  ja v  a  2  s . com
 */
@Test
public void setValueOnChange() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n"
            + "  <input type='number' id='t' value='1234'" + " onChange='alert(\"foo\");alert(event.type);'>\n"
            + "  <button id='b'>some button</button>\n"
            + "  <button id='set' onclick='document.getElementById(\"t\").value=\"1234\"'>setValue</button>\n"
            + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("set")).click();

    assertEquals(Collections.emptyList(), getCollectedAlerts(driver));

    // trigger lost focus
    driver.findElement(By.id("b")).click();
    assertEquals(Collections.emptyList(), getCollectedAlerts(driver));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs// w w w.  j a va2 s.co  m
 */
@Test
public void setDefaultValueOnChange() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n"
            + "  <input type='number' id='t' value='1234'" + " onChange='alert(\"foo\");alert(event.type);'>\n"
            + "  <button id='b'>some button</button>\n"
            + "  <button id='set' onclick='document.getElementById(\"t\").defaultValue=\"1234\"'>"
            + "setValue</button>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("set")).click();

    assertEquals(Collections.emptyList(), getCollectedAlerts(driver));

    // trigger lost focus
    driver.findElement(By.id("b")).click();
    assertEquals(Collections.emptyList(), getCollectedAlerts(driver));
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from   w  w w .ja  v a2  s  .  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='1234'/>\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.HtmlNumberInputTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*w  w w . j a  v  a 2s  .c o m*/
 */
@Test
public void sendKeysEnterWithoutForm() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <input id='t' type='number' value='1234'>\n" + "</body>\n"
            + "</html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("t")).sendKeys("\n");

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