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.html.HTMLTextAreaElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails//from   w  w w . j a  v  a2  s.c  om
 */
@Test
@Alerts("foo")
public void onChange() throws Exception {
    final String html = "<html>\n" + "<head><title>foo</title></head>\n" + "<body>\n" + "  <p>hello world</p>\n"
            + "  <form name='form1'>\n"
            + "    <textarea name='textarea1' onchange='alert(this.value)'></textarea>\n"
            + "    <input name='myButton' type='button' onclick='document.form1.textarea1.value=\"from button\"'>\n"
            + "  </form>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final WebElement textarea = driver.findElement(By.name("textarea1"));
    textarea.sendKeys("foo");
    driver.findElement(By.name("myButton")).click();

    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from w w w.j  a v a2 s  .  c  o m*/
 */
@Test
@Alerts({ "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81",
        "82", "83", "84", "85", "86", "87", "88", "89", "90" })
public void keyCodes() throws Exception {
    final String html = "<html><head>" + "<script>" + "function handleKey(e) {\n" + "  alert(e.keyCode);" + "}"
            + "</script>\n" + "</head><body>\n" + "<input id='t' onkeyup='handleKey(event)'/>\n"
            + "</body></html>";

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

    field.sendKeys("abcdefghijklmnopqrstuvwxyz");
    assertEquals(getExpectedAlerts(), getCollectedAlerts(driver));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test.//from w  ww .  j  a v a 2  s.c  o m
 */
@Test
public void shouldReportKeyCodeOfArrowKeys() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement result = driver.findElement(By.id("result"));
    final WebElement element = driver.findElement(By.id("keyReporter"));

    element.sendKeys(Keys.ARROW_DOWN);
    checkRecordedKeySequence(result, 40);

    element.sendKeys(Keys.ARROW_UP);
    checkRecordedKeySequence(result, 38);

    element.sendKeys(Keys.ARROW_LEFT);
    checkRecordedKeySequence(result, 37);

    element.sendKeys(Keys.ARROW_RIGHT);
    checkRecordedKeySequence(result, 39);

    // And leave no rubbish/printable keys in the "keyReporter"
    assertThat(element.getAttribute("value"), is(""));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test./*from  w w w .  ja  v a  2 s .co m*/
 */
@Test
public void shouldReportKeyCodeOfArrowKeysUpDownEvents() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement result = driver.findElement(By.id("result"));
    final WebElement element = driver.findElement(By.id("keyReporter"));

    element.sendKeys(Keys.ARROW_DOWN);
    assertThat(result.getText().trim(), containsString("down: 40"));
    assertThat(result.getText().trim(), containsString("up: 40"));

    element.sendKeys(Keys.ARROW_UP);
    assertThat(result.getText().trim(), containsString("down: 38"));
    assertThat(result.getText().trim(), containsString("up: 38"));

    element.sendKeys(Keys.ARROW_LEFT);
    assertThat(result.getText().trim(), containsString("down: 37"));
    assertThat(result.getText().trim(), containsString("up: 37"));

    element.sendKeys(Keys.ARROW_RIGHT);
    assertThat(result.getText().trim(), containsString("down: 39"));
    assertThat(result.getText().trim(), containsString("up: 39"));

    // And leave no rubbish/printable keys in the "keyReporter"
    assertThat(element.getAttribute("value"), is(""));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test.//w  w  w  .  ja  va  2  s  .c o  m
 */
@Test
public void numericShiftKeys() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement result = driver.findElement(By.id("result"));
    final WebElement element = driver.findElement(By.id("keyReporter"));

    final String numericShiftsEtc = "~!@#$%^&*()_+{}:\"<>?|END~";
    element.sendKeys(numericShiftsEtc);

    assertThat(element.getAttribute("value"), is(numericShiftsEtc));
    assertThat(result.getText().trim(), containsString(" up: 16"));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test./*  ww w .  ja  v a2s .co m*/
 */
@Test
public void uppercaseAlphaKeys() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement result = driver.findElement(By.id("result"));
    final WebElement element = driver.findElement(By.id("keyReporter"));

    final String upperAlphas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    element.sendKeys(upperAlphas);

    assertThat(element.getAttribute("value"), is(upperAlphas));
    assertThat(result.getText().trim(), containsString(" up: 16"));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test./*from  w  ww. j a  v a 2s . c o  m*/
 */
@Test
public void allPrintableKeys() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement result = driver.findElement(By.id("result"));
    final WebElement element = driver.findElement(By.id("keyReporter"));

    final String allPrintable = "!\"#$%&'()*+,-./0123456789:;<=>?@ ABCDEFGHIJKLMNO"
            + "PQRSTUVWXYZ [\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
    element.sendKeys(allPrintable);

    assertThat(element.getAttribute("value"), is(allPrintable));
    assertThat(result.getText().trim(), containsString(" up: 16"));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test./*ww  w  .j  av  a  2s . c  o m*/
 */
@Test
public void testArrowKeysAndPageUpAndDown() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement element = driver.findElement(By.id("keyReporter"));

    element.sendKeys(
            "a" + Keys.LEFT + "b" + Keys.RIGHT + Keys.UP + Keys.DOWN + Keys.PAGE_UP + Keys.PAGE_DOWN + "1");
    assertThat(element.getAttribute("value"), is("ba1"));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test.//from ww  w  .  jav  a 2s . c o m
 */
@Test
public void homeAndEndAndPageUpAndPageDownKeys() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement element = driver.findElement(By.id("keyReporter"));

    element.sendKeys("abc" + Keys.HOME + "0" + Keys.LEFT + Keys.RIGHT + Keys.PAGE_UP + Keys.PAGE_DOWN + Keys.END
            + "1" + Keys.HOME + "0" + Keys.PAGE_UP + Keys.END + "111" + Keys.HOME + "00");
    assertThat(element.getAttribute("value"), is("0000abc1111"));
}

From source file:com.gargoylesoftware.htmlunit.selenium.TypingTest.java

License:Apache License

/**
 * A test./* w w w . ja va2s .  com*/
 */
@Test
public void deleteAndBackspaceKeys() {
    final WebDriver driver = getWebDriver("/javascriptPage.html");

    final WebElement element = driver.findElement(By.id("keyReporter"));

    element.sendKeys("abcdefghi");
    assertThat(element.getAttribute("value"), is("abcdefghi"));

    element.sendKeys(Keys.LEFT, Keys.LEFT, Keys.DELETE);
    assertThat(element.getAttribute("value"), is("abcdefgi"));

    element.sendKeys(Keys.LEFT, Keys.LEFT, Keys.BACK_SPACE);
    assertThat(element.getAttribute("value"), is("abcdfgi"));
}