Example usage for org.openqa.selenium WebDriver findElement

List of usage examples for org.openqa.selenium WebDriver findElement

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriver findElement.

Prototype

@Override
WebElement findElement(By by);

Source Link

Document

Find the first WebElement using the given method.

Usage

From source file:com.gargoylesoftware.htmlunit.html.HtmlFieldSetTest.java

License:Apache License

/**
 * @throws Exception if the test fails//w ww .ja v a 2 s  . co m
 */
@Test
@Alerts({ "[object HTMLFieldSetElement]", "[object HTMLFormElement]" })
public void simpleScriptable() throws Exception {
    final String html = "<html><head>\n" + "<script>\n" + "  function test() {\n"
            + "    var fs = document.getElementById('fs');\n" + "    alert(fs);\n" + "    alert(fs.form);\n"
            + "  }\n" + "</script>\n" + "</head><body onload='test()'>\n" + "  <form>\n"
            + "    <fieldset id='fs'>\n" + "      <legend>Legend</legend>\n" + "    </fieldset>\n"
            + "  </form>\n" + "</body></html>";
    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlElement element = toHtmlElement(driver.findElement(By.id("fs")));
        assertTrue(element instanceof HtmlFieldSet);
    }
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInput2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs/*from   ww w  . j a v  a 2s  .c om*/
 */
@Test
@Alerts(DEFAULT = { "CONTENT_TYPE:application/octet-stream", "charset" }, IE = { "CONTENT_TYPE:text/plain",
        "charset" })
@NotYetImplemented(IE)
public void contentType() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/upload1", Upload1Servlet.class);
    servlets.put("/upload2", ContentTypeUpload2Servlet.class);
    startWebServer("./", new String[0], servlets);

    final WebDriver driver = getWebDriver();
    driver.get("http://localhost:" + PORT + "/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();

    final String pageSource = driver.getPageSource();
    assertTrue(pageSource, pageSource.contains(getExpectedAlerts()[0]));
    assertFalse(pageSource, pageSource.contains(getExpectedAlerts()[1]));
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInput2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs/*from   www. ja  v  a 2s.  c  o  m*/
 */
@Test
public void contentTypeHeader() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/upload1", Upload1Servlet.class);
    servlets.put("/upload2", ContentTypeHeaderUpload2Servlet.class);
    startWebServer("./", new String[0], servlets);

    final WebDriver driver = getWebDriver();
    driver.get("http://localhost:" + PORT + "/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();
    final String source = driver.getPageSource();
    assertTrue(source.contains("CONTENT_TYPE:"));
    assertFalse(source.contains("charset"));
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInput2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs/*from ww  w  . j av a2 s .  c o m*/
 */
@Test
@Alerts("Content-Disposition: form-data; name=\"myInput\"; filename=\"\"")
public void empty() 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("http://localhost:" + PORT + "/upload1");
    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.HtmlFileInput2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs/* w w  w  . j  a  v a2s . com*/
 */
@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("http://localhost:" + PORT + "/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.HtmlFileInput2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs/*from   ww w  .  j  ava  2  s  .  c om*/
 */
@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("http://localhost:" + PORT + "/upload1");
    driver.findElement(By.id("mySubmit")).click();
    assertFalse(driver.getPageSource().contains("chunked"));
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInput2Test.java

License:Apache License

/**
 * @throws Exception if the test fails/*from  ww  w . j a  v a 2  s  . c  o m*/
 */
@Test
@Alerts("changed")
public void firingOnchange() throws Exception {
    final String html = "<html><body>\n" + "<form onchange='alert(\"changed\")'>\n"
            + "  <input type='file' id='file1'>\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();
    driver.findElement(By.tagName("body")).click();

    verifyAlerts(driver, getExpectedAlerts());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java

License:Apache License

private void contentType(final String extension) throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/upload1", Upload1Servlet.class);
    servlets.put("/upload2", ContentTypeUpload2Servlet.class);
    startWebServer("./", new String[0], servlets);

    final WebDriver driver = getWebDriver();
    driver.get(URL_FIRST + "upload1");

    final File tmpFile = File.createTempFile("htmlunit-test", "." + extension);
    try {/*from   w  w  w . j  av a  2  s. c o  m*/
        String path = tmpFile.getAbsolutePath();
        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();
    } finally {
        tmpFile.delete();
    }

    final String pageSource = driver.getPageSource();
    assertTrue(pageSource, pageSource.contains(getExpectedAlerts()[0]));
    assertFalse(pageSource, pageSource.contains(getExpectedAlerts()[1]));
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java

License:Apache License

/**
 * @throws Exception if an error occurs//from w ww. ja v a2s .  com
 */
@Test
public void contentTypeHeader() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/upload1", Upload1Servlet.class);
    servlets.put("/upload2", ContentTypeHeaderUpload2Servlet.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();
    final String source = driver.getPageSource();
    assertTrue(source.contains("CONTENT_TYPE:"));
    assertFalse(source.contains("charset"));
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlFileInputTest.java

License:Apache License

/**
 * @throws Exception if an error occurs//from   w  w w .  j  av a 2s  .com
 */
@Test
@Alerts("Content-Disposition: form-data; name=\"myInput\"; filename=\"\"")
public void empty() 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");
    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());
}