Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlPage#getUrl()

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlPage#getUrl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HTMLDocument.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxGetter
public String getCookie() {
    final HtmlPage page = getPage();

    final URL url = page.getUrl();

    final StringBuilder builder = new StringBuilder();
    final Set<Cookie> cookies = page.getWebClient().getCookies(url);
    for (final Cookie cookie : cookies) {
        if (cookie.isHttpOnly()) {
            continue;
        }
        if (builder.length() != 0) {
            builder.append("; ");
        }
        if (!HtmlUnitBrowserCompatCookieSpec.EMPTY_COOKIE_NAME.equals(cookie.getName())) {
            builder.append(cookie.getName());
            builder.append('=');
        }
        builder.append(cookie.getValue());
    }

    return builder.toString();
}
 
Example 2
Source File: HTMLDocument.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxGetter
public String getCookie() {
    final HtmlPage page = getPage();

    final URL url = page.getUrl();

    final StringBuilder builder = new StringBuilder();
    final Set<Cookie> cookies = page.getWebClient().getCookies(url);
    for (final Cookie cookie : cookies) {
        if (cookie.isHttpOnly()) {
            continue;
        }
        if (builder.length() != 0) {
            builder.append("; ");
        }
        if (!HtmlUnitBrowserCompatCookieSpec.EMPTY_COOKIE_NAME.equals(cookie.getName())) {
            builder.append(cookie.getName());
            builder.append("=");
        }
        builder.append(cookie.getValue());
    }

    return builder.toString();
}
 
Example 3
Source File: HTMLDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
@JsxFunction({FF, FF68, FF60})
public void close() throws IOException {
    if (writeInCurrentDocument_) {
        LOG.warn("close() called when document is not open.");
    }
    else {
        final HtmlPage page = getPage();
        final URL url = page.getUrl();
        final StringWebResponse webResponse = new StringWebResponse(writeBuilder_.toString(), url);
        webResponse.setFromJavascript(true);
        writeInCurrentDocument_ = true;
        writeBuilder_.setLength(0);

        final WebClient webClient = page.getWebClient();
        final WebWindow window = page.getEnclosingWindow();
        // reset isAttachedToPageDuringOnload_ to trigger the onload event for chrome also
        if (window instanceof FrameWindow) {
            final BaseFrameElement frame = ((FrameWindow) window).getFrameElement();
            final ScriptableObject scriptable = frame.getScriptableObject();
            if (scriptable instanceof HTMLIFrameElement) {
                ((HTMLIFrameElement) scriptable).onRefresh();
            }
        }
        webClient.loadWebResponseInto(webResponse, window);
    }
}
 
Example 4
Source File: HTMLDocument.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxFunction(FF)
public void close() throws IOException {
    if (writeInCurrentDocument_) {
        LOG.warn("close() called when document is not open.");
    }
    else {
        final HtmlPage page = getPage();
        final URL url = page.getUrl();
        final StringWebResponse webResponse = new StringWebResponse(writeBuilder_.toString(), url);
        webResponse.setFromJavascript(true);
        writeInCurrentDocument_ = true;
        writeBuilder_.setLength(0);

        final WebClient webClient = page.getWebClient();
        final WebWindow window = page.getEnclosingWindow();
        // reset isAttachedToPageDuringOnload_ to trigger the onload event for chrome also
        if (window instanceof FrameWindow) {
            final BaseFrameElement frame = ((FrameWindow) window).getFrameElement();
            final ScriptableObject scriptable = frame.getScriptableObject();
            if (scriptable instanceof HTMLIFrameElement) {
                ((HTMLIFrameElement) scriptable).onRefresh();
            }
        }
        webClient.loadWebResponseInto(webResponse, window);
    }
}
 
Example 5
Source File: HtmlUnitNekoHtmlParser.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Parses the HTML content from the given string into an object tree representation.
 *
 * @param parent where the new parsed nodes will be added to
 * @param context the context to build the fragment context stack
 * @param source the (X)HTML to be parsed
 * @throws SAXException if a SAX error occurs
 * @throws IOException if an IO error occurs
 */
@Override
public void parseFragment(final DomNode parent, final DomNode context, final String source)
    throws SAXException, IOException {
    final Page page = parent.getPage();
    if (!(page instanceof HtmlPage)) {
        return;
    }
    final HtmlPage htmlPage = (HtmlPage) page;
    final URL url = htmlPage.getUrl();

    final HtmlUnitNekoDOMBuilder domBuilder = new HtmlUnitNekoDOMBuilder(this, parent, url, source);
    domBuilder.setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment", true);
    // build fragment context stack
    DomNode node = context;
    final List<QName> ancestors = new ArrayList<>();
    while (node != null && node.getNodeType() != Node.DOCUMENT_NODE) {
        ancestors.add(0, new QName(null, node.getNodeName(), null, null));
        node = node.getParentNode();
    }
    if (ancestors.isEmpty() || !"html".equals(ancestors.get(0).localpart)) {
        ancestors.add(0, new QName(null, "html", null, null));
    }
    if (ancestors.size() == 1 || !"body".equals(ancestors.get(1).localpart)) {
        ancestors.add(1, new QName(null, "body", null, null));
    }

    domBuilder.setFeature(HTMLScanner.ALLOW_SELFCLOSING_TAGS, true);
    domBuilder.setProperty(HTMLTagBalancer.FRAGMENT_CONTEXT_STACK, ancestors.toArray(new QName[ancestors.size()]));

    final XMLInputSource in = new XMLInputSource(null, url.toString(), null, new StringReader(source), null);

    htmlPage.registerParsingStart();
    htmlPage.registerSnippetParsingStart();
    try {
        domBuilder.parse(in);
    }
    finally {
        htmlPage.registerParsingEnd();
        htmlPage.registerSnippetParsingEnd();
    }
}
 
Example 6
Source File: UseRecipesWithJenkinsRuleTest.java    From jenkins-test-harness with MIT License 4 votes vote down vote up
private void verifyNotError(WebClient wc) throws IOException, SAXException {
    HtmlPage p = wc.goTo("loginError");
    URL url = p.getUrl();
    System.out.println(url);
    assertFalse(url.toExternalForm().contains("login"));
}