List of usage examples for org.openqa.selenium WebElement getText
String getText();
From source file:com.gargoylesoftware.htmlunit.html.HtmlTextArea2Test.java
License:Apache License
/** * @throws Exception if the test fails//from w ww . j a v a 2 s .c o m */ @Test @Alerts(" foo \n bar <p>html snippet</p>") public void parentAsText() throws Exception { final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n" + "<textarea name='textArea1'> foo \n bar " + "<p>html snippet</p>\n" + "</textarea>\n" + "</form></body></html>"; final WebDriver driver = loadPage2(html); final WebElement textArea = driver.findElement(By.id("form1")); assertEquals(getExpectedAlerts()[0], textArea.getText()); }
From source file:com.gargoylesoftware.htmlunit.html.HtmlTextInputTest.java
License:Apache License
/** * Verifies that a asText() returns an empty string. * @throws Exception if the test fails//from w ww . ja va2 s .c om */ @Test public void asText() throws Exception { final String htmlContent = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n" + " <input type='text' name='foo' id='foo' value='bla'>\n" + "</form></body></html>"; final WebDriver driver = loadPage2(htmlContent); final WebElement input = driver.findElement(By.id("foo")); assertEquals("", input.getText()); }
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();/* w w w.j a v a2s .c o 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
/** * Test the use of innerHTML to set new HTML code. * @throws Exception if the test fails/*from www.j a v a 2s .c o m*/ */ @Test @Alerts({ "Old = <b>Old innerHTML</b><!-- old comment -->", "New = <b><i id=\"newElt\">New cell value</i></b>", "I" }) public void getSetInnerHTMLComplex() throws Exception { final String html = "<html>\n" + "<head>\n" + " <title>test</title>\n" + " <script>\n" + " function doTest() {\n" + " var myNode = document.getElementById('myNode');\n" + " alert('Old = ' + myNode.innerHTML);\n" + " myNode.innerHTML = ' <b><i id=\"newElt\">New cell value</i></b>';\n" + " alert('New = ' + myNode.innerHTML);\n" + " alert(document.getElementById('newElt').tagName);\n" + " }\n" + " </script>\n" + "</head>\n" + "<body onload='doTest()'>\n" + "<p id='myNode'><b>Old innerHTML</b><!-- old comment --></p>\n" + "</body>\n" + "</html>"; final WebDriver driver = loadPageWithAlerts2(html); final WebElement pElt = driver.findElement(By.id("myNode")); assertEquals("p", pElt.getTagName()); final WebElement elt = driver.findElement(By.id("newElt")); assertEquals("New cell value", elt.getText()); assertEquals(1, driver.getWindowHandles().size()); }
From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement2Test.java
License:Apache License
/** * Test the use of outerHTML to set new HTML code. * @throws Exception if the test fails/*w ww . ja va 2 s. c o m*/ */ @Test @Alerts({ "Old = <b id=\"innerNode\">Old outerHTML</b>", "New = <b><i id=\"newElt\">New cell value</i></b>", "I" }) public void getSetOuterHTMLComplex() throws Exception { final String html = "<html>\n" + "<head>\n" + " <title>test</title>\n" + " <script>\n" + " function doTest() {\n" + " var myNode = document.getElementById('myNode');\n" + " var innerNode = document.getElementById('innerNode');\n" + " alert('Old = ' + innerNode.outerHTML);\n" + " innerNode.outerHTML = ' <b><i id=\"newElt\">New cell value</i></b>';\n" + " alert('New = ' + myNode.innerHTML);\n" + " alert(document.getElementById('newElt').tagName);\n" + " }\n" + " </script>\n" + "</head>\n" + "<body onload='doTest()'>\n" + "<p id='myNode'><b id='innerNode'>Old outerHTML</b></p>\n" + "</body>\n" + "</html>"; final WebDriver driver = loadPageWithAlerts2(html); final WebElement pElt = driver.findElement(By.id("myNode")); assertEquals("p", pElt.getTagName()); final WebElement elt = driver.findElement(By.id("newElt")); assertEquals("New cell value", elt.getText()); assertEquals(1, driver.getWindowHandles().size()); }
From source file:com.gargoylesoftware.htmlunit.javascript.host.WebSocketTest.java
License:Apache License
/** * Test case taken from <a href="http://angelozerr.wordpress.com/2011/07/23/websockets_jetty_step1/">here</a>. * @throws Exception if the test fails//ww w.j a v a2 s . c om */ @Test public void chat() throws Exception { final String firstResponse = "Browser: has joined!"; final String secondResponse = "Browser: Hope you are fine!"; startWebServer("src/test/resources/com/gargoylesoftware/htmlunit/javascript/host", null, null, new ChatWebSocketHandler()); final WebDriver driver = getWebDriver(); driver.get(URL_FIRST + "WebSocketTest_chat.html"); driver.findElement(By.id("username")).sendKeys("Browser"); driver.findElement(By.id("joinB")).click(); assertVisible("joined", driver); final WebElement chatE = driver.findElement(By.id("chat")); int counter = 0; do { Thread.sleep(100); } while (chatE.getText().isEmpty() && counter++ < 10); assertEquals(firstResponse, chatE.getText()); driver.findElement(By.id("phrase")).sendKeys("Hope you are fine!"); driver.findElement(By.id("sendB")).click(); counter = 0; do { Thread.sleep(100); } while (!chatE.getText().contains(secondResponse) && counter++ < 10); assertEquals(firstResponse + "\n" + secondResponse, chatE.getText()); }
From source file:com.gargoylesoftware.htmlunit.javascript.host.WebSocketTest.java
License:Apache License
/** * @throws Exception if the test fails//from ww w .jav a 2s . c o m */ @Test @Alerts({ ": myname=My value!1", ": myname=My value!2" }) public void cookies() throws Exception { startWebServer("src/test/resources/com/gargoylesoftware/htmlunit/javascript/host", null, null, new CookiesWebSocketHandler()); final WebDriver driver = getWebDriver(); driver.get(URL_FIRST + "WebSocketTest_cookies.html"); driver.findElement(By.id("username")).sendKeys("Browser"); driver.findElement(By.id("joinB")).click(); final WebElement chatE = driver.findElement(By.id("chat")); int counter = 0; do { Thread.sleep(100); } while (chatE.getText().isEmpty() && counter++ < 10); final String[] expected = getExpectedAlerts(); assertEquals(expected[0], chatE.getText()); driver.findElement(By.id("phrase")).sendKeys("Hope you are fine!"); driver.findElement(By.id("sendB")).click(); counter = 0; do { Thread.sleep(100); } while (!chatE.getText().contains(expected[1]) && counter++ < 10); assertEquals(expected[0] + "\n" + expected[1], chatE.getText()); }
From source file:com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine2Test.java
License:Apache License
/** * All browsers except Opera seem to have a single JS execution thread for all windows, * but it is not the case of HtmlUnit-2.6! * Tested browsers (21.09.09):// w w w. j a v a 2 s .c o m * - single JS thread: FF2, FF3.1, FF3.5, IE6 (and for info: Konqueror 4.2.2, Chrome Linux dev build) * - multiple JS threads: none of HtmlUnit's simulated browsers (for info Opera 10.00) * @throws Exception if the test fails */ @Test public void jsRunSingleThreadedBrowserWide() throws Exception { final String html = "<html><head><script>\n" + "function test(prefix) {\n" + " parent.document.getElementById('theArea').value += prefix + ' start\\n';\n" + " var end = new Date().valueOf() + 1 * 1000;\n" + " var t = [];\n" + " while (new Date().valueOf() < end) {\n" + " var x = document.createElement('iframe');\n" + " t.push(x);\n" + " }\n" + " parent.document.getElementById('theArea').value += prefix + ' end\\n';\n" + "}\n" + "function checkResults() {\n" + " var value = document.getElementById('theArea').value;\n" + " var lines = value.split('\\n');\n" + " if (lines.length < 5)\n" + " setTimeout(checkResults, 100); // not yet ready, check later\n" + " value = value.replace(/frame \\d /gi, '').replace(/\\W/gi, '');\n" + " var singleThreaded = (value == 'startendstartend');\n" + " document.getElementById('result').innerHTML = (singleThreaded ? 'single threaded' : 'in parallel');\n" + "}\n" + "function doTest() {\n" + " parent.document.getElementById('theArea').value = '';\n" + " document.getElementById('frame1').contentWindow.setTimeout(function() {test('frame 1'); }, 10);\n" + " document.getElementById('frame2').contentWindow.setTimeout(function() {test('frame 2'); }, 10);\n" + " setTimeout(checkResults, 1000);\n" + "}\n" + "</script></head>\n" + "<body onload='doTest()'>\n" + "<iframe id='frame1' src='about:blank'></iframe>\n" + "<iframe id='frame2' src='about:blank'></iframe>\n" + "<textarea id='theArea' rows='5'></textarea>\n" + "script execution occured: <span id='result'></span>\n" + "</body></html>"; final WebDriver driver = loadPage2(html); final WebElement element = driver.findElement(By.id("result")); // give time to the script to execute: normally ~2 seconds when scripts are run sequentially int nbWait = 0; while (element.getText().isEmpty()) { Thread.sleep(100); if (nbWait++ > 50) { break; } } assertEquals("single threaded", element.getText()); }
From source file:com.gargoylesoftware.htmlunit.libraries.DojoTestBase.java
License:Apache License
void test(final String module) throws Exception { try {//from w ww . ja v a2 s . c om final WebDriver webdriver = getWebDriver(); final String url = URL_FIRST + "util/doh/runner.html?testModule=" + module; webdriver.get(url); final long runTime = 60 * DEFAULT_WAIT_TIME; final long endTime = System.currentTimeMillis() + runTime; // wait a bit to let the tests start Thread.sleep(DEFAULT_WAIT_TIME); String status = getResultElementText(webdriver); while (!"Stopped".equals(status)) { Thread.sleep(DEFAULT_WAIT_TIME); if (System.currentTimeMillis() > endTime) { fail("Test runs too long (longer than " + runTime / 1000 + "s)"); } status = getResultElementText(webdriver); } Thread.sleep(100); // to make tests a bit more stable final WebElement output = webdriver.findElement(By.id("logBody")); final List<WebElement> lines = output.findElements(By.xpath(".//div")); final StringBuilder result = new StringBuilder(); for (WebElement webElement : lines) { final String text = webElement.getText(); if (StringUtils.isNotBlank(text)) { result.append(text); result.append("\n"); } } String expFileName = StringUtils.replace(module, ".", ""); expFileName = StringUtils.replace(expFileName, "_", ""); String expected = loadExpectation(expFileName); expected = StringUtils.replace(expected, "\r\n", "\n"); assertEquals(normalize(expected), normalize(result.toString())); // assertEquals(expected, result.toString()); } catch (final Exception e) { e.printStackTrace(); Throwable t = e; while ((t = t.getCause()) != null) { t.printStackTrace(); } throw e; } }
From source file:com.gargoylesoftware.htmlunit.libraries.DojoTestBase.java
License:Apache License
private static String getResultElementText(final WebDriver webdriver) { // if the elem is not available or stale we return an empty string // this will force a second try try {/*w ww .j ava 2s . c om*/ final WebElement elem = webdriver.findElement(By.id("runningStatus")); try { return elem.getText(); } catch (final StaleElementReferenceException e) { return ""; } } catch (final NoSuchElementException e) { return ""; } }