List of usage examples for org.openqa.selenium WebElement getAttribute
String getAttribute(String name);
From source file:com.gargoylesoftware.htmlunit.html.HtmlTextInputTest.java
License:Apache License
/** * @throws Exception if an error occurs/* www. j av a 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/* w w w . j a v a 2 s .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.HtmlUrlInputTest.java
License:Apache License
/** * @throws Exception if the test fails//from w w w . j a v a2 s .c o 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.NodeTest.java
License:Apache License
/** * Verifies that attributes belonging to cloned nodes are available via JavaScript. * http://sourceforge.net/p/htmlunit/bugs/659/ * @throws Exception if an error occurs/*from ww w .java 2s. c o m*/ */ @Test @Alerts("id=bar") public void cloneAttributesAvailable() throws Exception { final String html = "<html>\n" + " <head>\n" + " <script>\n" + " function go() {\n" + " var node = document.getElementById('foo');\n" + " var clone = node.cloneNode(true);\n" + " clone.id = 'bar';\n" + " node.appendChild(clone);\n" + " alert(clone.attributes['id'].nodeName + '=' + clone.attributes['id'].nodeValue);\n" + " }\n" + " </script>\n" + " </head>\n" + " <body onload='go()'>\n" + " <div id='foo'></div>\n" + " </body>\n" + "</html>"; final WebDriver driver = loadPageWithAlerts2(html); final WebElement element = driver.findElement(By.id("bar")); final String value = element.getAttribute("id"); assertEquals("bar", value); }
From source file:com.gargoylesoftware.htmlunit.javascript.host.event.EventTest.java
License:Apache License
private void document_input(final String event) throws Exception { final String html = "<html>\n" + "<head>\n" + "<script>\n" + " function test() {\n" + " handle(document);\n" + " }\n" + " function handle(obj) {\n" + " obj.addEventListener('" + event + "', handler, true);\n" + " }\n" + " function handler(e) {\n" + " var src = e.srcElement;\n" + " if (!src)\n" + " src = e.target;\n" + " log(e.type + ' ' + src.nodeName);\n" + " }\n" + " function log(x) {\n" + " document.getElementById('log').value += x + '\\n';\n" + " }\n" + "</script>\n" + "</head>\n" + "<body onload='test()'>\n" + " <div id=\"div\">\n" + " <input id=\"input1\" type=\"text\">\n" + " <input id=\"input2\" type=\"text\">\n" + " </div>\n" + "<textarea id='log' cols='80' rows='40'></textarea>\n" + "</body></html>"; final WebDriver driver = loadPage2(html); final WebElement logElement = driver.findElement(By.id("log")); final String initialValue = logElement.getAttribute("value"); driver.findElement(By.id("input1")).click(); driver.findElement(By.id("input2")).click(); final String addedValue = logElement.getAttribute("value").substring(initialValue.length()); final String text = addedValue.trim().replaceAll("\r", ""); assertEquals(String.join("\n", getExpectedAlerts()), text); }
From source file:com.gargoylesoftware.htmlunit.javascript.host.event.MouseEventTest.java
License:Apache License
private void eventCoordinates(final String id) throws Exception { final String html = getFileContent("event_coordinates.html"); final String[] expected = getExpectedAlerts(); setExpectedAlerts();/*from w w w . j a v a 2 s .co m*/ final WebDriver driver = loadPageWithAlerts2(html); assertEquals("Mouse Event coordinates", driver.getTitle()); final WebElement textarea = driver.findElement(By.id("myTextarea")); assertEquals("", textarea.getText()); driver.findElement(By.id(id)).click(); assertEquals(expected[0], textarea.getAttribute("value").trim()); }
From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement2Test.java
License:Apache License
private void events(final String type) throws Exception { final String html = "<html><head>\n" + "</head>\n" + "<body>\n" + "<div id='div' on" + type + "='log(\"div handler\")'>\n" + "<input id='input' on" + type + "='log(\"input handler\")'>\n" + "</div>\n" + "<textarea id='log'></textarea>\n" + "<script>\n" + "function log(x) {\n" + " document.getElementById('log').value += x + '\\n';\n" + "}\n" + "function addListener(id, event) {\n" + " var handler = function(e) { log(event + ' ' + id) };\n" + " var e = document.getElementById(id);\n" + " e.addEventListener(event, handler, false);\n" + "}\n" + "var eventType = '" + type + "';\n" + "addListener('div', eventType);\n" + "addListener('input', eventType);\n" + "</script>\n" + "</body></html>"; final WebDriver driver = loadPage2(html); driver.findElement(By.id("input")).click(); final WebElement log = driver.findElement(By.id("log")); log.click();//from w ww . ja va2s. c o m final String text = log.getAttribute("value").trim().replaceAll("\r", ""); assertEquals(String.join("\n", getExpectedAlerts()), text); }
From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLFormElementTest.java
License:Apache License
private void doTestProperty(final String jsProperty, final String htmlProperty, final String oldValue, final String newValue) throws Exception { final String html = "<html><head><title>foo</title><script>\n" + "function doTest() {\n" + " alert(document.forms[0]." + jsProperty + ");\n" + " try {\n" + " document.forms[0]." + jsProperty + "='" + newValue + "';\n" + " alert(document.forms[0]." + jsProperty + ");\n" + " alert(document.forms[0].getAttribute('" + htmlProperty + "'));\n" + " } catch(e) { alert('exception'); }\n" + "}\n" + "</script></head><body onload='doTest()'>\n" + "<p>hello world</p>\n" + "<form " + htmlProperty + "='" + oldValue + "'>\n" + " <input type='button' name='button1' />\n" + "</form>\n" + "</body></html>"; final WebDriver wd = loadPageWithAlerts2(html); final WebElement form = wd.findElement(By.xpath("//form")); if (wd instanceof HtmlUnitDriver && getExpectedAlerts().length >= 3) { // form.getAttribute("enctype") returns form.getAttribute("encoding") with the FF driver. Bug or feature? assertEquals(getExpectedAlerts()[2], form.getAttribute(htmlProperty)); }/*from ww w . j a va 2s . com*/ }
From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLImageElementTest.java
License:Apache License
/** * This test verifies that when JavaScript is used to modify the <tt>src</tt> attribute, the value is * persisted to the corresponding <tt><img></tt> node in the DOM tree. * @throws Exception if the test fails/* w w w . ja va 2s .c om*/ */ @Test public void setSrc() throws Exception { final String html = "<html><head><script>\n" + "function doTest() {\n" + " document.getElementById('anImage').src = 'bar.gif';\n" + "}\n" + "</script></head><body onload='doTest()'>\n" + "<img src='foo.gif' id='anImage'/>\n" + "</body></html>"; final WebDriver driver = loadPage2(html); final WebElement image = driver.findElement(By.id("anImage")); assertEquals(URL_FIRST + "bar.gif", image.getAttribute("src")); }
From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLInputElementTest.java
License:Apache License
/** * @throws Exception if the test fails/* w w w. j av a 2s . com*/ */ @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")); }