Example usage for org.openqa.selenium WebElement sendKeys

List of usage examples for org.openqa.selenium WebElement sendKeys

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement sendKeys.

Prototype

void sendKeys(CharSequence... keysToSend);

Source Link

Document

Use this method to simulate typing into an element, which may set its value.

Usage

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 2s . co m*/
 */
@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//from w ww.j  a v  a 2s.  co 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.jav a2s  .c o m*/
 */
@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//w  ww .  j ava 2  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 the test fails//from  w  ww  .  j  a va 2  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.HtmlPasswordInputTest.java

License:Apache License

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

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

License:Apache License

/**
 * @throws Exception if the test fails//  w w w  . j  av a2  s. c  om
 */
@Test
public void typeWhileDisabled() throws Exception {
    final String html = "<html><body><input type='password' 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.HtmlPasswordInputTest.java

License:Apache License

/**
 * @throws Exception if the test fails/* w  ww.  j a va  2s  . c o  m*/
 */
@Test
@Alerts({ "null", "null" })
public void typeDoesNotChangeValueAttribute() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n" + "  <input type='password' id='p'/>\n"
            + "  <button id='check' onclick='alert(document.getElementById(\"p\").getAttribute(\"value\"));'>"
            + "DoIt</button>\n" + "</body></html>";

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

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

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

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

License:Apache License

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

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

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

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

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

License:Apache License

/**
 * @throws Exception if an error occurs/* www .ja va 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='password' 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"));
}