List of usage examples for org.openqa.selenium WebDriver findElement
@Override WebElement findElement(By by);
From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java
License:Apache License
/** * @throws Exception if an error occurs/* www .ja v a 2s . c o m*/ */ @Test @Alerts(DEFAULT = "Content-Disposition: form-data; name=\"myInput\"; filename=\"realm.properties\"", IE = "Content-Disposition: form-data; name=\"myInput\";" + " filename=\".*test-classes[\\\\/]realm\\.properties\"") public void realFile() throws Exception { final Map<String, Class<? extends Servlet>> servlets = new HashMap<>(); servlets.put("/upload1", Upload1Servlet.class); servlets.put("/upload2", PrintRequestServlet.class); startWebServer("./", new String[0], servlets); final WebDriver driver = getWebDriver(); driver.get(URL_FIRST + "upload1"); String path = getClass().getClassLoader().getResource("realm.properties").toExternalForm(); if (driver instanceof InternetExplorerDriver || driver instanceof ChromeDriver) { path = path.substring(path.indexOf('/') + 1).replace('/', '\\'); } driver.findElement(By.name("myInput")).sendKeys(path); driver.findElement(By.id("mySubmit")).click(); String pageSource = driver.getPageSource(); // hack for selenium int count = 0; while (count < 100 && StringUtils.isEmpty(pageSource)) { pageSource = driver.getPageSource(); count++; } final Matcher matcher = Pattern.compile(getExpectedAlerts()[0]).matcher(pageSource); assertTrue(pageSource, matcher.find()); }
From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java
License:Apache License
/** * @throws Exception if an error occurs/* w ww .j a va 2s .com*/ */ @Test public void chunked() throws Exception { final Map<String, Class<? extends Servlet>> servlets = new HashMap<>(); servlets.put("/upload1", Upload1Servlet.class); servlets.put("/upload2", ChunkedUpload2Servlet.class); startWebServer("./", new String[0], servlets); final WebDriver driver = getWebDriver(); driver.get(URL_FIRST + "upload1"); driver.findElement(By.id("mySubmit")).click(); assertFalse(driver.getPageSource().contains("chunked")); }
From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java
License:Apache License
/** * Verifies that a asText() returns an empty string. * @throws Exception if the test fails// ww w .j a v a 2 s.com */ @Test public void asText() throws Exception { final String htmlContent = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n" + " <input type='file' 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.html.HtmlFileInputTest.java
License:Apache License
/** * @throws Exception if an error occurs/*from w w w . j a va 2 s .co m*/ */ @Test @Alerts(DEFAULT = { "exception", "-Hello world-Hello world" }, IE = "-Hello world-Hello world") public void setValueOnChange() throws Exception { final String html = "<html>\n" + "<head>\n" + "<script>\n" + " function test() {\n" + " var input = document.getElementById('f');\n" + " try{\n" + " input.value = 'HtmlUnit';\n" + " } catch(e) { alert('exception'); }\n" + " alert(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n" + " }\n" + "</script>\n" + "<body>\n" + " <input type='file' id='f' value='Hello world'" + " onChange='alert('foo');alert(event.type);'>\n" + " <button id='b'>some button</button>\n" + " <button id='set' onclick='test()'>setValue</button>\n" + "</body></html>"; final WebDriver driver = loadPage2(html); driver.findElement(By.id("set")).click(); assertEquals(getExpectedAlerts(), getCollectedAlerts(driver)); // trigger lost focus driver.findElement(By.id("b")).click(); assertTrue(getCollectedAlerts(driver, 1).isEmpty()); }
From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java
License:Apache License
/** * @throws Exception if an error occurs/*from w w w.ja va2s .c om*/ */ @Test @Alerts("-HtmlUnit-HtmlUnit") public void setDefaultValueOnChange() throws Exception { final String html = "<html>\n" + "<head>\n" + "<script>\n" + " function test() {\n" + " var input = document.getElementById('f');\n" + " try{\n" + " input.defaultValue = 'HtmlUnit';\n" + " } catch(e) { alert('exception'); }\n" + " alert(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n" + " }\n" + "</script>\n" + "</head>\n" + "<body>\n" + " <input type='file' id='f' value='Hello world'" + " onChange='alert(\"foo\");alert(event.type);'>\n" + " <button id='b'>some button</button>\n" + " <button id='set' onclick='test()'>setValue</button>\n" + "</body></html>"; final WebDriver driver = loadPage2(html); driver.findElement(By.id("set")).click(); assertEquals(getExpectedAlerts(), getCollectedAlerts(driver)); // trigger lost focus driver.findElement(By.id("b")).click(); assertTrue(getCollectedAlerts(driver, 1).isEmpty()); }
From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java
License:Apache License
/** * @throws Exception if the test fails//from w ww. j a va 2s . com */ @Test @Alerts({ "changed2", "changed" }) public void firingOnchange() throws Exception { final String html = "<html><body>\n" + "<form onchange='alert(\"changed\")'>\n" + " <input type='file' id='file1' onchange='alert(\"changed2\")' " + "onkeydown='alert(\"onkeydown2\")' " + "onkeypress='alert(\"onkeypress2\")' " + "onkeyup='alert(\"onkeyup2\")'>\n" + "</form>\n" + "</body></html>"; final WebDriver driver = loadPage2(html); final File tmpFile = File.createTempFile("htmlunit-test", ".txt"); driver.findElement(By.id("file1")).sendKeys(tmpFile.getAbsolutePath()); tmpFile.delete(); verifyAlerts(driver, getExpectedAlerts()); }
From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java
License:Apache License
/** * @throws Exception if an error occurs/*from w w w. j a v a2 s . c o m*/ */ @Test @Alerts(DEFAULT = "C:\\fakepath\\pom.xml--null", FF = "pom.xml--null", IE = "PATH--null") public void value2() throws Exception { final String html = "<html>\n" + "<head>\n" + "<script>\n" + " function test() {\n" + " var input = document.getElementById('f');\n" + " alert(input.value + '-' + input.defaultValue + '-' + input.getAttribute('value'));\n" + " }\n" + "</script>\n" + "</head>\n" + "<body>\n" + " <input type='file' id='f'>\n" + " <button id='clickMe' onclick='test()'>Click Me</button>\n" + "</body></html>"; final String absolutePath = new File("pom.xml").getAbsolutePath(); final WebDriver driver = loadPage2(html); driver.findElement(By.id("f")).sendKeys(absolutePath); driver.findElement(By.id("clickMe")).click(); final String[] expectedAlerts = getExpectedAlerts(); expectedAlerts[0] = expectedAlerts[0].replace("PATH", absolutePath); assertEquals(expectedAlerts, getCollectedAlerts(driver)); }
From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java
License:Apache License
/** * @throws Exception if an error occurs/*from ww w .jav a2s . co m*/ */ @Test @Alerts("foo, change") public void onchange() throws Exception { final String html = "<html>\n" + "<head>\n" + "</head>\n" + "<body>\n" + " <input type='file' id='f' value='Hello world'" + " onChange='alert(\"foo\");alert(event.type);'>\n" + " <button id='clickMe' onclick='test()'>Click Me</button>\n" + "</body></html>"; final String absolutePath = new File("pom.xml").getAbsolutePath(); final WebDriver driver = loadPage2(html); driver.findElement(By.id("f")).sendKeys(absolutePath); assertEquals(getExpectedAlerts(), getCollectedAlerts(driver, 2)); }
From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java
License:Apache License
/** * @throws Exception if an error occurs//from w w w .j a va2 s .c om */ @Test @Alerts(DEFAULT = "C:\\fakepath\\pom.xml", FF = "pom.xml", IE = "PATH") public void getAttribute() throws Exception { final String html = "<html><body>\n" + " <input type='file' id='f'>\n" + "</body></html>"; final String absolutePath = new File("pom.xml").getAbsolutePath(); final WebDriver driver = loadPage2(html); final WebElement e = driver.findElement(By.id("f")); e.sendKeys(absolutePath); final String[] expectedAlerts = getExpectedAlerts(); expectedAlerts[0] = expectedAlerts[0].replace("PATH", absolutePath); assertEquals(expectedAlerts[0], e.getAttribute("value")); }
From source file:com.gargoylesoftware.htmlunit.html.HtmlFontTest.java
License:Apache License
/** * @throws Exception if the test fails//from www. jav a 2 s . c o m */ @Test @Alerts("[object HTMLFontElement]") public void simpleScriptable() throws Exception { final String html = "<html><head>\n" + "<script>\n" + " function test() {\n" + " alert(document.getElementById('myId'));\n" + " }\n" + "</script>\n" + "</head><body onload='test()'>\n" + " <font id='myId'/>\n" + "</body></html>"; final WebDriver driver = loadPageWithAlerts2(html); if (driver instanceof HtmlUnitDriver) { final HtmlElement element = toHtmlElement(driver.findElement(By.id("myId"))); assertTrue(element instanceof HtmlFont); } }