Example usage for org.openqa.selenium WebDriver getPageSource

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

Introduction

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

Prototype

String getPageSource();

Source Link

Document

Get the source of the last loaded page.

Usage

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

License:Apache License

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

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 {/*  ww w .j  ava 2 s  . c om*/
        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 w w  .  j a  va  2  s . c  om
 */
@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//w  ww . ja va 2 s.  co  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(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());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//w ww.  ja  v  a  2 s  .co  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//from w w  w  .  j  ava  2s.c  o  m
 */
@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.HtmlUnknownElementTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*from w  w w .  j  ava  2  s.c o m*/
 */
@Test
public void asXml() throws Exception {
    final String html = "<html><body><title>foo</title>\n" + "<foo></foo>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);

    final String xml = driver.getPageSource();
    assertTrue("Node not expanded in: " + xml, xml.contains("</foo>"));
}

From source file:com.gargoylesoftware.htmlunit.HttpWebConnection2Test.java

License:Apache License

/**
 * Test for broken gzip content.//from   www .ja v  a2 s.  co m
 * @throws Exception if the test fails
 */
@Test
public void brokenGzip() throws Exception {
    final byte[] content = new byte[] { -1 };
    final List<NameValuePair> headers = new ArrayList<>();
    headers.add(new NameValuePair("Content-Encoding", "gzip"));
    headers.add(new NameValuePair("Content-Length", String.valueOf(content.length)));

    final MockWebConnection conn = getMockWebConnection();
    conn.setResponse(URL_FIRST, content, 404, "OK", "text/html", headers);

    // only check that no exception is thrown
    final WebDriver driver = loadPageWithAlerts2(URL_FIRST);
    assertTrue(driver.getPageSource().length() > 100);
}