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.DefaultCredentialsProvider2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs//w  w w. jav  a  2 s .  com
 */
@Test
public void basicAuthenticationTwice() throws Exception {
    ((DefaultCredentialsProvider) getWebClient().getCredentialsProvider()).addCredentials("jetty", "jetty");

    getMockWebConnection().setResponse(URL_SECOND, "Hello World");
    final WebDriver driver = loadPage2("Hi There");
    assertTrue(driver.getPageSource().contains("Hi There"));
    driver.get(URL_SECOND.toExternalForm());
    assertTrue(driver.getPageSource().contains("Hello World"));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs// w w w.  j ava  2 s.  c  o  m
 */
@Test
@Alerts(FF = "SecRet")
public void basicAuthenticationUserFromUrl() throws Exception {
    final String html = "<html><body onload='alert(\"SecRet\")'></body></html>";
    getMockWebConnection().setDefaultResponse(html);

    getWebClient().getCredentialsProvider().clear();

    // no credentials
    final WebDriver driver = loadPage2(html, new URL("http://localhost:" + PORT + "/"));
    assertTrue(driver.getPageSource().contains("HTTP ERROR 401"));

    // now a url with credentials
    URL url = new URL("http://jetty:jetty@localhost:" + PORT + "/");
    loadPageWithAlerts2(url);

    // next step without credentials but the credentials are still known
    url = new URL("http://localhost:" + PORT + "/");
    loadPageWithAlerts2(url);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from  ww w . ja  v a 2  s .c o  m
 */
@Test
@Alerts(FF = "SecRet")
public void basicAuthenticationUserFromUrlUsedForNextSteps() throws Exception {
    final String html = "<html><body onload='alert(\"SecRet\")'></body></html>";
    getMockWebConnection().setDefaultResponse(html);

    getWebClient().getCredentialsProvider().clear();

    // no credentials
    final WebDriver driver = loadPage2(html, new URL("http://localhost:" + PORT + "/"));
    assertTrue(driver.getPageSource().contains("HTTP ERROR 401"));

    // now a url with credentials
    URL url = new URL("http://jetty:jetty@localhost:" + PORT + "/");
    loadPageWithAlerts2(url);

    // next step without credentials but the credentials are still known
    url = new URL("http://localhost:" + PORT + "/");
    loadPageWithAlerts2(url);

    // different path
    url = new URL("http://localhost:" + PORT + "/somewhere");
    loadPageWithAlerts2(url);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/* ww w  .ja  va  2s  .c  om*/
 */
@Test
@Alerts(FF = "SecRet")
public void basicAuthenticationUserFromUrlOverwrite() throws Exception {
    final String html = "<html><body onload='alert(\"SecRet\")'></body></html>";
    getMockWebConnection().setDefaultResponse(html);

    getWebClient().getCredentialsProvider().clear();

    // no credentials
    final WebDriver driver = loadPage2(html, new URL("http://localhost:" + PORT + "/"));
    assertTrue(driver.getPageSource().contains("HTTP ERROR 401"));

    // now a url with credentials
    URL url = new URL("http://jetty:jetty@localhost:" + PORT + "/");
    loadPageWithAlerts2(url);

    // next step without credentials but the credentials are still known
    url = new URL("http://localhost:" + PORT + "/");
    loadPageWithAlerts2(url);

    // and now with wrong credentials
    url = new URL("http://jetty:wrong@localhost:" + PORT + "/");
    loadPage2(html, url);
    assertTrue(driver.getPageSource().contains("HTTP ERROR 401"));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from   w  w w  .j av  a2  s . c  o m*/
 */
@Test
@Alerts("SecRet")
public void basicAuthenticationUserFromUrlOverwriteDefaultCredentials() throws Exception {
    final String html = "<html><body onload='alert(\"SecRet\")'></body></html>";
    getMockWebConnection().setDefaultResponse(html);

    getWebClient().getCredentialsProvider().clear();
    ((DefaultCredentialsProvider) getWebClient().getCredentialsProvider()).addCredentials("jetty", "jetty");

    // use default credentials
    URL url = new URL("http://localhost:" + PORT + "/");
    final WebDriver driver = loadPageWithAlerts2(url);

    // now a url with wrong credentials
    url = new URL("http://joe:jetty@localhost:" + PORT + "/");
    loadPage2(html, url);

    if (getBrowserVersion().isIE()) {
        assertTrue(driver.getPageSource().contains("SecRet"));
    } else {
        assertTrue(driver.getPageSource().contains("HTTP ERROR 401"));
    }
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from w  w  w  .  j  a v  a  2 s  .c om*/
 */
@Test
@Alerts(FF = "SecRet")
public void basicAuthenticationUserFromUrlOverwriteWrongDefaultCredentials() throws Exception {
    final String html = "<html><body onload='alert(\"SecRet\")'></body></html>";
    getMockWebConnection().setDefaultResponse(html);

    getWebClient().getCredentialsProvider().clear();
    ((DefaultCredentialsProvider) getWebClient().getCredentialsProvider()).addCredentials("joe", "hack");

    // use default wrong credentials
    URL url = new URL("http://localhost:" + PORT + "/");
    final WebDriver driver = loadPage2(html, url);
    assertTrue(driver.getPageSource().contains("HTTP ERROR 401"));

    // now a url with correct credentials
    url = new URL("http://jetty:jetty@localhost:" + PORT + "/");
    loadPageWithAlerts2(url);
}

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

License:Apache License

/**
 * Tests going in history {@link History#back()} with {@code POST} request.
 *
 * @throws Exception if an error occurs/*from w  w w.  j av  a 2 s  . c om*/
 */
@Test
public void post() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    servlets.put("/post1", Post1Servlet.class);
    servlets.put("/post2", Post2Servlet.class);
    servlets.put("/post3", Post3Servlet.class);
    startWebServer("./", new String[0], servlets);

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

    driver.findElement(By.id("mySubmit")).click();
    assertEquals(URL_FIRST + "post2", driver.getCurrentUrl());
    assertTrue(driver.getPageSource().contains("POST"));
    assertTrue(driver.getPageSource().contains("para1=value1"));

    driver.findElement(By.linkText("Go to GET")).click();
    assertEquals(URL_FIRST + "post3", driver.getCurrentUrl());
    assertTrue(driver.getPageSource().contains("GET"));

    driver.navigate().back();
    assertEquals(URL_FIRST + "post2", driver.getCurrentUrl());
    assertTrue(driver.getPageSource().contains("POST"));
    assertTrue(driver.getPageSource().contains("para1=value1"));
}

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

License:Apache License

/**
 * Tests going in history {@link History#back()} with {@code POST} request.
 *
 * @throws Exception if an error occurs//ww  w .  j a  v a 2s  .  c o m
 */
@Test
@Alerts("49")
// limit varies for IE
public void historyCache() throws Exception {
    final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
    final int testDeep = 70;

    for (int i = 0; i < testDeep; i++) {
        servlets.put("/post" + i, Post1Servlet.class);
    }
    startWebServer("./", new String[0], servlets);

    final WebDriver driver = getWebDriver();

    int count = Post1Servlet.Count_;
    for (int i = 0; i < testDeep; i++) {
        driver.get(URL_FIRST + "post" + i);
        assertTrue(driver.getPageSource(), driver.getPageSource().contains("Call: " + (i + count)));
    }

    count = Post1Servlet.Count_;
    for (int i = 0; i < testDeep - 1; i++) {
        driver.navigate().back();
        if (!driver.getPageSource().contains("Call: " + (count - i - 2))) {
            assertEquals(Integer.parseInt(getExpectedAlerts()[0]), i);
            return;
        }

        if (count != Post1Servlet.Count_) {
            Assert.fail("Server called for " + i);
            break;
        }
    }

    assertEquals(getExpectedAlerts()[0], "done");
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from w w  w  .j  a va  2s.  c o  m*/
 */
@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/*  ww  w  .  j  a  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"));
}