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:org.ocpsoft.rewrite.faces.jsession.JSessionIdTest.java

License:Apache License

@Test
public void withoutCookiesAndStandardOutcome() throws Exception {

    // GIVEN a browser with cookies disabled
    WebDriver driver = createBrowserAndLoadPage(false);
    assertThat(driver.getPageSource(), Matchers.containsString("jsessionid"));

    // WHEN a JSF action redirects with a standard outcome
    driver.findElement(By.id("form:standardOutcome")).click();

    // THEN the browser should be redirected
    assertThat(driver.getCurrentUrl(), Matchers.containsString("redirected=true"));

    // AND there should be a jsessionid in the URL
    assertThat(driver.getCurrentUrl(), Matchers.containsString("jsessionid"));

}

From source file:org.ocpsoft.rewrite.faces.jsession.JSessionIdTest.java

License:Apache License

@Test
public void withoutCookiesAndNavigateOutcome() throws Exception {

    // GIVEN a browser with cookies disabled
    WebDriver driver = createBrowserAndLoadPage(false);
    assertThat(driver.getPageSource(), Matchers.containsString("jsessionid"));

    // WHEN a JSF action redirects with a standard outcome
    driver.findElement(By.id("form:navigateOutcome")).click();

    // THEN the browser should be redirected
    assertThat(driver.getCurrentUrl(), Matchers.containsString("redirected=true"));

    // AND there should be a jsessionid in the URL
    assertThat(driver.getCurrentUrl(), Matchers.containsString("jsessionid"));

}

From source file:org.polimi.zarathustra.webdriver.WebdriverWorker.java

License:Open Source License

private String getPage(String url, WebDriver driver) {
    driver.get(url);
    String pageSource = driver.getPageSource();
    return pageSource;
}

From source file:org.qe4j.web.OpenWebDriverTest.java

License:Open Source License

@Test
public void getPageSource() throws IOException {
    Properties properties = getProperties();
    WebDriver driver = new OpenWebDriver(properties);
    driver.get(URL);// w  ww  . j a  v  a2s  . co m
    Assert.assertTrue(driver.getPageSource().contains("</div>"), "page source");
}

From source file:org.sample.SessionTest.app.PageSource.java

License:Apache License

public void saveForced(WebDriver webDriver, String subTitle) {

    if (StringUtils.isEmpty(subTitle)) {
        subTitle = "";
    } else {/*from  ww w. j  a  v  a 2s  .c  o m*/
        subTitle = "-" + subTitle;
    }

    int sequenceNo = sequence.incrementAndGet();
    String evidenceFile = String.format("page_source_%03d%s.txt", sequenceNo, subTitle);
    File pageSourceFile = new File(evidenceSavingDirectory, evidenceFile);

    try {
        FileUtils.writeStringToFile(pageSourceFile, webDriver.getPageSource());

    } catch (IOException e) {
        logger.error(e.toString());
    }

}

From source file:org.seasar.robot.client.http.WebDriverClient.java

License:Apache License

@Override
public ResponseData execute(final RequestData request) {
    WebDriver webDriver = null;
    try {//from ww w  .  j av  a 2s . com
        webDriver = webDriverPool.borrowObject();

        Map<String, String> paramMap = null;
        String url = request.getUrl();
        String metaData = request.getMetaData();
        if (StringUtil.isNotBlank(metaData)) {
            paramMap = parseParamMap(metaData);
        }

        if (!url.equals(webDriver.getCurrentUrl())) {
            webDriver.get(url);
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Base URL: " + url + "\nContent: " + webDriver.getPageSource());
        }

        if (paramMap != null) {
            final String processorName = paramMap.get(UrlAction.URL_ACTION);
            final UrlAction urlAction = urlActionMap.get(processorName);
            if (urlAction == null) {
                throw new RobotSystemException("Unknown processor: " + processorName);
            }
            urlAction.navigate(webDriver, paramMap);
        }

        final String source = webDriver.getPageSource();

        final ResponseData responseData = new ResponseData();

        responseData.setUrl(webDriver.getCurrentUrl());
        responseData.setMethod(request.getMethod().name());
        responseData.setContentLength(source.length());

        final String charSet = getCharSet(webDriver);
        responseData.setCharSet(charSet);
        responseData.setHttpStatusCode(getStatusCode(webDriver));
        responseData.setLastModified(getLastModified(webDriver));
        responseData.setMimeType(getContentType(webDriver));

        responseData.setResponseBody(new ByteArrayInputStream(source.getBytes(charSet)));

        for (final UrlAction urlAction : urlActionMap.values()) {
            urlAction.collect(url, webDriver, responseData);
        }

        return responseData;
    } catch (final Exception e) {
        throw new RobotSystemException("Failed to access " + request.getUrl(), e);
    } finally {
        if (webDriver != null) {
            try {
                webDriverPool.returnObject(webDriver);
            } catch (final Exception e) {
                logger.warn("Failed to return a returned object.", e);
            }
        }
    }
}

From source file:org.specrunner.webdriver.result.WritableWebDriver.java

License:Open Source License

/**
 * Dump a screen shot of web driver which implements
 * <code>TakesScreenshot</code>.
 * //w ww .  java2  s .c  om
 * @param source
 *            The web driver source.
 * @param driver
 *            The web driver casted to screenshot object.
 * @throws IOException
 *             On writing errors.
 */
protected void dumpScreenshot(WebDriver source, TakesScreenshot driver) throws IOException {
    Window window = source.manage().window();
    Dimension d = null;
    Point p = null;
    try {
        d = window.getSize();
        p = window.getPosition();
    } catch (Exception e) {
        if (UtilLog.LOG.isInfoEnabled()) {
            UtilLog.LOG.info("Could not get size and position.");
        }
    }
    try {
        // write screen
        File scrFile = driver.getScreenshotAs(OutputType.FILE);
        tmpDump = File.createTempFile("srunnerw", getExtension(scrFile));
        tmpDump.delete();
        FileUtils.copyFile(scrFile, tmpDump);
        if (UtilLog.LOG.isDebugEnabled()) {
            UtilLog.LOG.debug("Saved page screen to temporary file " + tmpDump);
        }

        // write source
        tmpSource = File.createTempFile("srunnerw", ".html");
        tmpSource.delete();
        FileUtils.writeStringToFile(tmpSource, source.getPageSource());
        if (UtilLog.LOG.isDebugEnabled()) {
            UtilLog.LOG.debug("Saved page source to temporary file " + tmpSource);
        }

    } catch (WebDriverException e) {
        if (UtilLog.LOG.isInfoEnabled()) {
            UtilLog.LOG.info("Could not dump screenshot.");
        }
    } finally {
        if (d != null && p != null) {
            if (UtilLog.LOG.isInfoEnabled()) {
                UtilLog.LOG.info("Restore size and location.");
            }
            window.setPosition(p);
            window.setSize(d);
        }
    }
}

From source file:org.springframework.security.htmlunit.server.WebTestClientHtmlUnitDriverBuilderTests.java

License:Apache License

@Test
public void helloWorld() {
    WebTestClient webTestClient = WebTestClient.bindToController(new HelloWorldController()).build();
    WebDriver driver = WebTestClientHtmlUnitDriverBuilder.webTestClientSetup(webTestClient).build();

    driver.get("http://localhost/");

    assertThat(driver.getPageSource()).contains("Hello World");
}

From source file:org.springframework.security.htmlunit.server.WebTestClientHtmlUnitDriverBuilderTests.java

License:Apache License

@Test
public void cookies() {
    WebTestClient webTestClient = WebTestClient.bindToController(new CookieController()).build();
    WebDriver driver = WebTestClientHtmlUnitDriverBuilder.webTestClientSetup(webTestClient).build();

    driver.get("http://localhost/cookie");

    assertThat(driver.getPageSource()).contains("theCookie");

    driver.get("http://localhost/cookie/delete");

    assertThat(driver.getPageSource()).contains("null");
}

From source file:org.surfnet.oaaas.selenium.AuthorizationCodeTestIT.java

License:Apache License

@Test
public void invalidParams() {
    final WebDriver webdriver = getWebDriver();
    webdriver.get(baseUrlWith("/oauth2/authorize"));

    String pageSource = webdriver.getPageSource();
    assertThat(pageSource, containsString("The supported response_type values are 'token' and 'code'"));
}