List of usage examples for org.openqa.selenium WebElement sendKeys
void sendKeys(CharSequence... keysToSend);
From source file:com.gargoylesoftware.htmlunit.html.HtmlPasswordInputTest.java
License:Apache License
/** * @throws Exception if an error occurs/*from w w w. j a v a 2s. com*/ */ @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='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")); }
From source file:com.gargoylesoftware.htmlunit.html.HtmlPasswordInputTest.java
License:Apache License
/** * @throws Exception if an error occurs/* w ww . j a v a2 s . com*/ */ @Test public void typeOnChange() throws Exception { final String html = "<html><head></head><body>\n" + "<input type='password' id='p' value='Hello world'" + " 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("HtmlUnit"); assertTrue(getCollectedAlerts(driver, 1).isEmpty()); // 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.HtmlSearchInputTest.java
License:Apache License
/** * @throws Exception if the test fails//from www . j a va2s .co m */ @Test public void type() throws Exception { final String html = "<html><head></head><body><input id='t' type='search'/></body></html>"; final WebDriver webDriver = loadPage2(html); final WebElement input = webDriver.findElement(By.id("t")); input.sendKeys("abc"); assertEquals("abc", input.getAttribute("value")); input.sendKeys("\b"); assertEquals("ab", input.getAttribute("value")); input.sendKeys("\b"); assertEquals("a", input.getAttribute("value")); input.sendKeys("\b"); assertEquals("", input.getAttribute("value")); input.sendKeys("\b"); assertEquals("", input.getAttribute("value")); }
From source file:com.gargoylesoftware.htmlunit.html.HtmlTelInputTest.java
License:Apache License
/** * @throws Exception if the test fails// w ww . j a v a2s . c om */ @Test public void typing() throws Exception { final String htmlContent = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n" + " <input type='tel' id='foo'>\n" + "</form></body></html>"; final WebDriver driver = loadPage2(htmlContent); final WebElement input = driver.findElement(By.id("foo")); input.sendKeys("hello"); assertEquals("hello", input.getAttribute("value")); }
From source file:com.gargoylesoftware.htmlunit.html.HtmlTextInput2Test.java
License:Apache License
/** * @throws Exception if the test fails//from w ww. j a va 2s . com */ @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// w w w . j a v a 2s .com */ @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
/** * @throws Exception if the test fails//from www . j a v a 2s. c om */ @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// w w w. j a v a2 s. co 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 ww w . j a va 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='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/* w w w . j a va 2 s . c o m*/ */ @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]); }