Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlPage#getWebClient()
The following examples show how to use
com.gargoylesoftware.htmlunit.html.HtmlPage#getWebClient() .
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: HTMLFormElement.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Submits the form (at the end of the current script execution). */ @JsxFunction public void submit() { final HtmlPage page = (HtmlPage) getDomNodeOrDie().getPage(); final WebClient webClient = page.getWebClient(); final String action = getHtmlForm().getActionAttribute().trim(); if (StringUtils.startsWithIgnoreCase(action, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) { final String js = action.substring(JavaScriptURLConnection.JAVASCRIPT_PREFIX.length()); webClient.getJavaScriptEngine().execute(page, js, "Form action", 0); } else { // download should be done ASAP, response will be loaded into a window later final WebRequest request = getHtmlForm().getWebRequest(null); final String target = page.getResolvedTarget(getTarget()); final boolean forceDownload = webClient.getBrowserVersion().hasFeature(JS_FORM_SUBMIT_FORCES_DOWNLOAD); final boolean checkHash = !webClient.getBrowserVersion().hasFeature(FORM_SUBMISSION_DOWNLOWDS_ALSO_IF_ONLY_HASH_CHANGED); webClient.download(page.getEnclosingWindow(), target, request, checkHash, forceDownload, "JS form.submit()"); } }
Example 2
Source File: HTMLFormElement.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Submits the form (at the end of the current script execution). */ @JsxFunction public void submit() { final HtmlPage page = (HtmlPage) getDomNodeOrDie().getPage(); final WebClient webClient = page.getWebClient(); final String action = getHtmlForm().getActionAttribute().trim(); if (StringUtils.startsWithIgnoreCase(action, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) { final String js = action.substring(JavaScriptURLConnection.JAVASCRIPT_PREFIX.length()); webClient.getJavaScriptEngine().execute(page, js, "Form action", 0); } else { // download should be done ASAP, response will be loaded into a window later final WebRequest request = getHtmlForm().getWebRequest(null); final String target = page.getResolvedTarget(getTarget()); final boolean forceDownload = webClient.getBrowserVersion().hasFeature(JS_FORM_SUBMIT_FORCES_DOWNLOAD); final boolean checkHash = !webClient.getBrowserVersion().hasFeature(FORM_SUBMISSION_DOWNLOWDS_ALSO_IF_ONLY_HASH_CHANGED); webClient.download(page.getEnclosingWindow(), target, request, checkHash, forceDownload, "JS form.submit()"); } }
Example 3
Source File: HTMLDocument.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Adds a cookie, as long as cookies are enabled. * @see <a href="http://msdn.microsoft.com/en-us/library/ms533693.aspx">MSDN documentation</a> * @param newCookie in the format "name=value[;expires=date][;domain=domainname][;path=path][;secure] */ @JsxSetter public void setCookie(final String newCookie) { final HtmlPage page = getPage(); final WebClient client = page.getWebClient(); client.addCookie(newCookie, getPage().getUrl(), this); }
Example 4
Source File: HTMLDocument.java From htmlunit with Apache License 2.0 | 5 votes |
@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 5
Source File: HtmlUnitNekoDOMBuilder.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Creates a new builder for parsing the specified response contents. * @param node the location at which to insert the new content * @param url the page's URL */ HtmlUnitNekoDOMBuilder(final HTMLParser htmlParser, final DomNode node, final URL url, final String htmlContent) { super(createConfiguration(node.getPage().getWebClient().getBrowserVersion())); htmlParser_ = htmlParser; page_ = (HtmlPage) node.getPage(); currentNode_ = node; for (final Node ancestor : currentNode_.getAncestors()) { stack_.push((DomNode) ancestor); } final WebClient webClient = page_.getWebClient(); final HTMLParserListener listener = webClient.getHTMLParserListener(); final boolean reportErrors = listener != null; if (reportErrors) { fConfiguration.setErrorHandler(new HtmlUnitNekoHTMLErrorHandler(listener, url, htmlContent)); } try { setFeature(FEATURE_AUGMENTATIONS, true); if (!webClient.getBrowserVersion().hasFeature(HTML_ATTRIBUTE_LOWER_CASE)) { setProperty("http://cyberneko.org/html/properties/names/attrs", "no-change"); } setFeature("http://cyberneko.org/html/features/report-errors", reportErrors); setFeature(FEATURE_PARSE_NOSCRIPT, !webClient.isJavaScriptEnabled()); setFeature(HTMLScanner.ALLOW_SELFCLOSING_IFRAME, false); setContentHandler(this); setLexicalHandler(this); //comments and CDATA } catch (final SAXException e) { throw new ObjectInstantiationException("unable to create HTML parser", e); } initialSize_ = stack_.size(); }
Example 6
Source File: HTMLDocument.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Adds a cookie, as long as cookies are enabled. * @see <a href="http://msdn.microsoft.com/en-us/library/ms533693.aspx">MSDN documentation</a> * @param newCookie in the format "name=value[;expires=date][;domain=domainname][;path=path][;secure] */ @JsxSetter public void setCookie(final String newCookie) { final HtmlPage page = getPage(); final WebClient client = page.getWebClient(); client.addCookie(newCookie, getPage().getUrl(), this); }
Example 7
Source File: HTMLDocument.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * {@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 8
Source File: WebClientTest.java From htmlunit with Apache License 2.0 | 4 votes |
/** * Tests that the JavaScript parent scope is set correctly when shuffling windows around. * @throws Exception if test fails */ @Test public void maintainJavaScriptParentScope() throws Exception { final String basicContent = "<html><head>\n" + "<title>basicContentTitle</title>\n" + "</head><body>\n" + "<p>Hello World</p>\n" + "</body></html>"; final String jsContent = "<html><head>\n" + "<title>jsContentTitle</title>\n" + "<script>function foo() {alert('Ran Here')}</script>\n" + "<script>function bar() {}</script>\n" + "</head><body onload='bar()'>\n" + "<input type='button' id='button' onclick='foo()'/>" + "</body></html>"; final HtmlPage jsPage = loadPage(jsContent); final WebClient webClient = jsPage.getWebClient(); final WebWindow firstWindow = webClient.getCurrentWindow(); getMockConnection(jsPage).setResponse(URL_SECOND, basicContent); final CollectingAlertHandler alertHandler = new CollectingAlertHandler(); webClient.setAlertHandler(alertHandler); final HtmlButtonInput buttonBefore = jsPage.getHtmlElementById("button"); final WebWindow secondWindow = webClient.openWindow(null, "second window"); webClient.setCurrentWindow(secondWindow); webClient.getPage(URL_SECOND); webClient.setCurrentWindow(firstWindow); final HtmlPage currentPage = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage(); final HtmlButtonInput buttonAfter = currentPage.getHtmlElementById("button"); assertSame(buttonBefore, buttonAfter); buttonAfter.click(); assertEquals(1, alertHandler.getCollectedAlerts().size()); assertEquals("Ran Here", alertHandler.getCollectedAlerts().get(0)); }