List of usage examples for org.openqa.selenium WebElement sendKeys
void sendKeys(CharSequence... keysToSend);
From source file:com.gargoylesoftware.htmlunit.html.HtmlTextInputTest.java
License:Apache License
/** * @throws Exception if an error occurs//from w w w . j a 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='text' 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.HtmlTextInputTest.java
License:Apache License
/** * @throws Exception if an error occurs//www .j a v 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='text' 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.HtmlTextInputTest.java
License:Apache License
/** * @throws Exception if an error occurs//from w ww . ja va 2 s . c o m */ @Test public void typeOnChange() throws Exception { final String html = "<html><head></head><body>\n" + "<input type='text' 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.HtmlUrlInputTest.java
License:Apache License
/** * @throws Exception if the test fails/*from w ww.j av a 2 s . co m*/ */ @Test public void typing() throws Exception { final String htmlContent = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n" + " <input type='url' 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.javascript.host.dom.EventNodeTest.java
License:Apache License
/** * Test event order.//from w w w.ja v a 2 s .co m * @throws Exception if the test fails */ @Test public void eventOrder() throws Exception { final String html = "<html>\n" + "<head>\n" + " <script>\n" + " function log(text) {\n" + " var textarea = document.getElementById('myTextarea');\n" + " textarea.value += text + ',';\n" + " }\n" + " </script>\n" + "</head><body>\n" + "<form>\n" + " <input name='foo' id='foo' onfocus=\"log('focus')\" onblur=\"log('blur')\" onchange=\"log('change')\"" + " onkeydown=\"log('keydown')\" onkeypress=\"log('keypress')\" onkeyup=\"log('keyup')\">\n" + " <input name='other' id='other'>\n" + "</form>\n" + " <textarea id='myTextarea' cols='80'></textarea>\n" + "</body></html>"; final WebDriver webDriver = loadPageWithAlerts2(html); final WebElement textField = webDriver.findElement(By.id("foo")); textField.click(); // to give focus textField.sendKeys("a"); webDriver.findElement(By.id("other")).click(); final String expected = "focus,keydown,keypress,keyup,change,blur,"; assertEquals(expected, webDriver.findElement(By.id("myTextarea")).getAttribute("value")); }
From source file:com.gargoylesoftware.htmlunit.javascript.host.event.Event2Test.java
License:Apache License
/** * Tests that event fires on key press.// ww w.ja va 2s . c o m * @throws Exception if the test fails */ @Test @Alerts({ "pass", "fail:66", "fail:undefined" }) public void eventOnKeyDown() 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"); verifyAlerts(driver, getExpectedAlerts()[0]); element.sendKeys("b"); verifyAlerts(driver, getExpectedAlerts()[1]); element.click(); verifyAlerts(driver, getExpectedAlerts()[2]); }
From source file:com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEventTest.java
License:Apache License
/** * @throws Exception if the test fails//from w w w. j a va 2 s .c om */ @Test @Alerts("32, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, ") public void keyCodes_keyup() throws Exception { final String html = "<html><head>\n" + "<script>\n" + "function handleKey(e) {\n" + " document.getElementById('log').value += e.keyCode + ', ';\n" + "}\n" + "</script>\n" + "</head>\n" + "<body>\n" + " <input id='t' onkeyup='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//from w w w. ja va 2s .co 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 keyCodes2_keyup() throws Exception { final String html = "<html><head>\n" + "<script>\n" + "function handleKey(e) {\n" + " document.getElementById('log').value += e.keyCode + ', ';\n" + "}\n" + "</script>\n" + "</head>\n" + "<body>\n" + " <input id='t' onkeyup='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//w w w.j a va 2 s.co m */ @Test @Alerts("32, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, ") public void keyCodes_keydown() throws Exception { final String html = "<html><head>\n" + "<script>\n" + "function handleKey(e) {\n" + " document.getElementById('log').value += e.keyCode + ', ';\n" + "}\n" + "</script>\n" + "</head>\n" + "<body>\n" + " <input id='t' onkeydown='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//from w ww .jav 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 keyCodes2_keydown() throws Exception { final String html = "<html><head>\n" + "<script>\n" + "function handleKey(e) {\n" + " document.getElementById('log').value += e.keyCode + ', ';\n" + "}\n" + "</script>\n" + "</head>\n" + "<body>\n" + " <input id='t' onkeydown='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); }