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.javascript.host.event.KeyboardEventTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from ww  w .  j av a  2s .c om
 */
@Test
@Alerts("32, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, ")
public void keyCodes_keypress() throws Exception {
    final String html = "<html><head>\n" + "<script>\n" + "function handleKey(e) {\n"
            + "  document.getElementById('log').value += e.charCode + ', ';\n" + "}\n" + "</script>\n"
            + "</head>\n" + "<body>\n" + "  <input id='t' onkeypress='handleKey(event)'/>\n"
            + "  <textarea id='log' rows=40 cols=80></textarea>\n" + "</body></html>";

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

    field.sendKeys(" 0123456789");

    final String log = driver.findElement(By.id("log")).getAttribute("value");
    assertEquals(getExpectedAlerts()[0], log);
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEventTest.java

License:Apache License

/**
 * @throws Exception if the test fails//  w w w  .  j a  va 2 s . c  om
 */
@Test
@Alerts("97, 98, 99, " + "100, 101, 102, 103, 104, 105, 106, 107, 108, 109, "
        + "110, 111, 112, 113, 114, 115, 116, 117, 118, 119, " + "120, 121, 122, ")
public void keyCodes2_keypress() throws Exception {
    final String html = "<html><head>\n" + "<script>\n" + "function handleKey(e) {\n"
            + "  document.getElementById('log').value += e.charCode + ', ';\n" + "}\n" + "</script>\n"
            + "</head>\n" + "<body>\n" + "  <input id='t' onkeypress='handleKey(event)'/>\n"
            + "  <textarea id='log' rows=40 cols=80></textarea>\n" + "</body></html>";

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

    field.sendKeys("abcdefghijklmnopqrstuvwxyz");

    final String log = driver.findElement(By.id("log")).getAttribute("value");
    assertEquals(getExpectedAlerts()[0], log);
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEventTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from  ww w .ja v  a2 s .  c  o  m
 */
@Test
@Alerts(DEFAULT = { "13", "13", "13" }, FF = { "0", "13", "13" })
public void keyCodeEnter_keypress() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "<script>\n" + "function handleKey(e) {\n"
            + "  alert(e.charCode);\n" + "  alert(e.keyCode);\n" + "  alert(e.which);\n" + "}\n" + "</script>\n"
            + "</head>\n" + "<body>\n" + "  <textarea id='t' onkeypress='handleKey(event)'></textarea>\n"
            + "</body>\n" + "</html>";

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

    field.sendKeys("\n");

    verifyAlerts(driver, getExpectedAlerts());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.Event2Test.java

License:Apache License

/**
 * Tests that event fires on key press.//from ww w.j a  va 2s.  c o  m
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = { "pass", "fail:66", "fail:undefined" }, CHROME = { "pass", "fail:66", "fail:0" })
public void testEventOnKeyDown() throws Exception {
    final String html = "<html><head></head>\n" + "<body>\n"
            + "  <button type='button' id='clickId'>Click Me</button>\n" + "  <script>\n"
            + "    function handler(_e) {\n" + "      var e = _e ? _e : window.event;\n"
            + "      if (e.keyCode == 65)\n" + "        alert('pass');\n" + "      else\n"
            + "        alert('fail:' + e.keyCode);\n" + "    }\n"
            + "    document.getElementById('clickId').onkeydown = handler;\n"
            + "    document.getElementById('clickId').onclick = handler;\n" + "  </script>\n"
            + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final WebElement element = driver.findElement(By.id("clickId"));
    element.sendKeys("a");
    element.sendKeys("b");
    element.click();

    assertEquals(getExpectedAlerts(), getCollectedAlerts(driver));
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*from w w w .  ja va2  s  .co m*/
 */
@Test
@Alerts("foo")
public void onChange() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title>\n"
            + "</head><body>\n" + "<p>hello world</p>\n" + "<form name='form1'>\n"
            + "  <input type='text' name='text1' onchange='alert(this.value)'>\n"
            + "<input name='myButton' type='button' onclick='document.form1.text1.value=\"from button\"'>\n"
            + "</form>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final WebElement textinput = driver.findElement(By.name("text1"));
    textinput.sendKeys("foo");
    final WebElement button = driver.findElement(By.name("myButton"));
    button.click();
    verifyAlerts(driver, getExpectedAlerts());
    Thread.sleep(10);
    assertEquals("from button", textinput.getAttribute("value"));
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails/* w ww  .j a v a 2s.c  om*/
 */
@Test
@Alerts("foo")
public void onChangeSetByJavaScript() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title>\n"
            + "</head><body>\n" + "<p>hello world</p>\n" + "<form name='form1'>\n"
            + "  <input type='text' name='text1' id='text1'>\n"
            + "<input name='myButton' type='button' onclick='document.form1.text1.value=\"from button\"'>\n"
            + "</form>\n" + "<script>\n"
            + "document.getElementById('text1').onchange = function(event) { alert(this.value) };\n"
            + "</script>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final WebElement textinput = driver.findElement(By.name("text1"));
    textinput.sendKeys("foo");
    final WebElement button = driver.findElement(By.name("myButton"));
    button.click();
    verifyAlerts(driver, getExpectedAlerts());
    Thread.sleep(10);
    assertEquals("from button", textinput.getAttribute("value"));
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*from   ww  w.j  a  v a2s.  c  o  m*/
 */
@Test
public void typeMaxLength() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><body>\n" + "<form>\n"
            + "<input type='text' id='text1' maxlength='5'/>\n"
            + "<input type='password' id='password1' maxlength='6'/>\n" + "</form></body></html>";

    final WebDriver webDriver = loadPage2(html);
    final WebElement textField = webDriver.findElement(By.id("text1"));
    textField.sendKeys("123456789");
    assertEquals("12345", textField.getAttribute("value"));
    textField.sendKeys("\b7");
    assertEquals("12347", textField.getAttribute("value"));

    final WebElement passwordField = webDriver.findElement(By.id("password1"));
    passwordField.sendKeys("123456789");
    assertEquals("123456", passwordField.getAttribute("value"));
    passwordField.sendKeys("\b7");
    assertEquals("123457", passwordField.getAttribute("value"));
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from  w  w w .  j a  va  2s  .c  o m
 */
@Test
public void typeMaxLengthZero() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><body>\n" + "<form>\n"
            + "<input type='text' id='text1' maxlength='0'/>\n"
            + "<input type='password' id='password1' maxlength='0'/>\n" + "</form></body></html>";

    final WebDriver webDriver = loadPage2(html);
    final WebElement textField = webDriver.findElement(By.id("text1"));
    textField.sendKeys("123456789");
    assertEquals("", textField.getAttribute("value"));
    textField.sendKeys("\b7");
    assertEquals("", textField.getAttribute("value"));

    final WebElement passwordField = webDriver.findElement(By.id("password1"));
    passwordField.sendKeys("123456789");
    assertEquals("", passwordField.getAttribute("value"));
    passwordField.sendKeys("\b7");
    assertEquals("", passwordField.getAttribute("value"));
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from w  w  w.  ja v a2 s.  c  om
 */
@Test
public void typeMaxLengthAndBlanks() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><body>\n" + "<form>\n"
            + "<input type='text' id='text1' maxlength=' 2 '/>\n"
            + "<input type='password' id='password1' maxlength='    4  '/>\n" + "</form></body></html>";

    final WebDriver webDriver = loadPage2(html);
    final WebElement textField = webDriver.findElement(By.id("text1"));
    textField.sendKeys("123456789");
    assertEquals("12", textField.getAttribute("value"));
    textField.sendKeys("\b7");
    assertEquals("17", textField.getAttribute("value"));

    final WebElement passwordField = webDriver.findElement(By.id("password1"));
    passwordField.sendKeys("123456789");
    assertEquals("1234", passwordField.getAttribute("value"));
    passwordField.sendKeys("\b7");
    assertEquals("1237", passwordField.getAttribute("value"));
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails/* ww  w.  ja  v a2s.c o m*/
 */
@Test
public void sendKeys() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title><script>\n"
            + "</script></head>\n" + "<body>\n"
            + "  <input id='myInput' value='Test' onchange=\"alert('changed')\">\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    final WebElement element = driver.findElement(By.id("myInput"));
    element.sendKeys("abc");
    verifyAlerts(driver);
}