Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlPage#getEnclosingWindow()
The following examples show how to use
com.gargoylesoftware.htmlunit.html.HtmlPage#getEnclosingWindow() .
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 |
private void scheduleImplicitClose() { if (!closePostponedAction_) { closePostponedAction_ = true; final HtmlPage page = (HtmlPage) getDomNodeOrDie(); final WebWindow enclosingWindow = page.getEnclosingWindow(); page.getWebClient().getJavaScriptEngine().addPostponedAction(new PostponedAction(page) { @Override public void execute() throws Exception { if (writeBuilder_.length() != 0) { close(); } closePostponedAction_ = false; } @Override public boolean isStillAlive() { return !enclosingWindow.isClosed(); } }); } }
Example 2
Source File: TopLevelWindowTest.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Test that no JavaScript error is thrown when using the setTimeout() JS function * while the page is unloading. * Regression test for bug 2956550. * @throws Exception if an error occurs */ @Test @Alerts("closing") public void setTimeoutDuringOnUnload() throws Exception { final String html = "<html><head>\n" + "<script>\n" + "function f() {\n" + " alert('closing');\n" + " setTimeout(function() { alert('started in onunload'); }, 0);\n" + "}\n" + "window.addEventListener('unload', f, true);\n" + "</script></head>\n" + "<body></body></html>"; final List<String> alerts = new ArrayList<>(); final HtmlPage page = loadPage(html, alerts); assertTrue(alerts.isEmpty()); final TopLevelWindow w = (TopLevelWindow) page.getEnclosingWindow(); w.close(); getWebClient().waitForBackgroundJavaScript(1000); assertEquals(getExpectedAlerts(), alerts); }
Example 3
Source File: HTMLDocument.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
private void scheduleImplicitClose() { if (!closePostponedAction_) { closePostponedAction_ = true; final HtmlPage page = (HtmlPage) getDomNodeOrDie(); final WebWindow enclosingWindow = page.getEnclosingWindow(); page.getWebClient().getJavaScriptEngine().addPostponedAction(new PostponedAction(page) { @Override public void execute() throws Exception { if (writeBuilder_.length() != 0) { close(); } closePostponedAction_ = false; } @Override public boolean isStillAlive() { return !enclosingWindow.isClosed(); } }); } }
Example 4
Source File: JavaScriptEngine.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Handles an exception that occurred during execution of JavaScript code. * @param scriptException the exception * @param triggerOnError if true, this triggers the onerror handler */ protected void handleJavaScriptException(final ScriptException scriptException, final boolean triggerOnError) { // Trigger window.onerror, if it has been set. final HtmlPage page = scriptException.getPage(); if (triggerOnError && page != null) { final WebWindow window = page.getEnclosingWindow(); if (window != null) { final Window w = window.getScriptableObject(); if (w != null) { try { w.triggerOnError(scriptException); } catch (final Exception e) { handleJavaScriptException(new ScriptException(page, e, null), false); } } } } getWebClient().getJavaScriptErrorListener().scriptException(page, scriptException); // Throw a Java exception if the user wants us to. if (getWebClient().getOptions().isThrowExceptionOnScriptError()) { throw scriptException; } // Log the error; ScriptException instances provide good debug info. LOG.info("Caught script exception", scriptException); }
Example 5
Source File: AbstractHtmlEngine.java From ats-framework with Apache License 2.0 | 6 votes |
private boolean isAlreadyLoadedByAncestor( final URL url, HtmlPage page ) { WebWindow window = page.getEnclosingWindow(); while (window != null) { if (url.sameFile(window.getEnclosedPage().getWebResponse().getWebRequest().getUrl())) { return true; } if (window == window.getParentWindow()) { window = null; } else { window = window.getParentWindow(); } } return false; }
Example 6
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 7
Source File: JavaScriptEngine.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Handles an exception that occurred during execution of JavaScript code. * @param scriptException the exception * @param triggerOnError if true, this triggers the onerror handler */ protected void handleJavaScriptException(final ScriptException scriptException, final boolean triggerOnError) { // shutdown was already called final WebClient webClient = getWebClient(); if (webClient == null) { if (LOG.isInfoEnabled()) { LOG.info("handleJavaScriptException('" + scriptException.getMessage() + "') called after the shutdown of the Javascript engine - exception ignored."); } return; } // Trigger window.onerror, if it has been set. final HtmlPage page = scriptException.getPage(); if (triggerOnError && page != null) { final WebWindow window = page.getEnclosingWindow(); if (window != null) { final Window w = window.getScriptableObject(); if (w != null) { try { w.triggerOnError(scriptException); } catch (final Exception e) { handleJavaScriptException(new ScriptException(page, e, null), false); } } } } webClient.getJavaScriptErrorListener().scriptException(page, scriptException); // Throw a Java exception if the user wants us to. if (webClient.getOptions().isThrowExceptionOnScriptError()) { throw scriptException; } }
Example 8
Source File: HtmlUnitNekoHtmlParser.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Adds a body element to the current page, if necessary. Strictly speaking, this should * probably be done by NekoHTML. See the bug linked below. If and when that bug is fixed, * we may be able to get rid of this code. * * http://sourceforge.net/p/nekohtml/bugs/15/ * @param page * @param originalCall * @param checkInsideFrameOnly true if the original page had body that was removed by JavaScript */ private void addBodyToPageIfNecessary( final HtmlPage page, final boolean originalCall, final boolean checkInsideFrameOnly) { // IE waits for the whole page to load before initializing bodies for frames. final boolean waitToLoad = page.hasFeature(PAGE_WAIT_LOAD_BEFORE_BODY); if (page.getEnclosingWindow() instanceof FrameWindow && originalCall && waitToLoad) { return; } // Find out if the document already has a body element (or frameset). final Element doc = page.getDocumentElement(); boolean hasBody = false; for (Node child = doc.getFirstChild(); child != null; child = child.getNextSibling()) { if (child instanceof HtmlBody || child instanceof HtmlFrameSet) { hasBody = true; break; } } // If the document does not have a body, add it. if (!hasBody && !checkInsideFrameOnly) { final DomElement body = getFactory("body").createElement(page, "body", null); doc.appendChild(body); } // If this is IE, we need to initialize the bodies of any frames, as well. // This will already have been done when emulating FF (see above). if (waitToLoad) { for (final FrameWindow frame : page.getFrames()) { final Page containedPage = frame.getEnclosedPage(); if (containedPage != null && containedPage.isHtmlPage()) { addBodyToPageIfNecessary((HtmlPage) containedPage, false, false); } } } }
Example 9
Source File: TopLevelWindowTest.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Regression test for bug 2808520: onbeforeunload not called when window.close() is called. * @throws Exception if an error occurs */ @Test public void onBeforeUnloadCalledOnClose() throws Exception { final String html = "<html><body onbeforeunload='alert(7)'>abc</body></html>"; final List<String> alerts = new ArrayList<>(); final HtmlPage page = loadPage(html, alerts); assertTrue(alerts.isEmpty()); final TopLevelWindow w = (TopLevelWindow) page.getEnclosingWindow(); w.close(); assertEquals(Arrays.asList("7"), alerts); }
Example 10
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 11
Source File: WindowTest.java From htmlunit with Apache License 2.0 | 4 votes |
/** * @throws Exception if the test fails */ @Test public void openWindow_base() throws Exception { final WebClient webClient = getWebClient(); final MockWebConnection webConnection = new MockWebConnection(); final List<String> collectedAlerts = new ArrayList<>(); webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); final String firstContent = "<html><head><title>First</title><base target='MyNewWindow'></head><body>\n" + "<form name='form1'>\n" + " <a id='link' href='" + URL_SECOND + "'>Click me</a>\n" + "</form>\n" + "</body></html>"; final String secondContent = "<html><head><title>Second</title></head><body>\n" + "<script>alert(self.name)</script>\n" + "</body></html>"; webConnection.setResponse(URL_FIRST, firstContent); webConnection.setResponse(URL_SECOND, secondContent); webClient.setWebConnection(webConnection); final HtmlPage firstPage = webClient.getPage(URL_FIRST); assertEquals("First", firstPage.getTitleText()); final WebWindow firstWebWindow = firstPage.getEnclosingWindow(); assertEquals(firstWebWindow, firstWebWindow.getTopWindow()); final HtmlAnchor anchor = firstPage.getHtmlElementById("link"); final HtmlPage secondPage = anchor.click(); assertEquals("Second", secondPage.getTitleText()); assertNotSame(firstPage, secondPage); final WebWindow secondWebWindow = secondPage.getEnclosingWindow(); assertNotSame(firstWebWindow, secondWebWindow); assertEquals("MyNewWindow", secondWebWindow.getName()); assertEquals(secondWebWindow, secondWebWindow.getTopWindow()); assertEquals(new String[] {"MyNewWindow"}, collectedAlerts); }
Example 12
Source File: WindowTest.java From htmlunit with Apache License 2.0 | 4 votes |
/** * Basic test for the <tt>showModalDialog</tt> method. See bug #703. * @throws Exception if an error occurs */ @Test @Alerts(DEFAULT = {"undefined", "Jane", "Smith", "sdg", "finished"}, CHROME = "not available", FF = "not available", FF68 = "not available", FF60 = "not available") public void showModalDialog() throws Exception { final String html1 = "<html><head><script>\n" + " function test() {\n" + " if (!window.showModalDialog) {alert('not available'); return; }\n" + " alert(window.returnValue);\n" + " var o = new Object();\n" + " o.firstName = 'Jane';\n" + " o.lastName = 'Smith';\n" + " var ret = showModalDialog('myDialog.html', o, 'dialogHeight:300px; dialogLeft:200px;');\n" + " alert(ret);\n" + " alert('finished');\n" + " }\n" + "</script></head><body>\n" + " <button onclick='test()' id='b'>Test</button>\n" + "</body></html>"; final String html2 = "<html><head><script>\n" + " var o = window.dialogArguments;\n" + " alert(o.firstName);\n" + " alert(o.lastName);\n" + " window.returnValue = 'sdg';\n" + "</script></head>\n" + "<body>foo</body></html>"; final WebClient client = getWebClient(); final List<String> actual = new ArrayList<>(); client.setAlertHandler(new CollectingAlertHandler(actual)); final MockWebConnection conn = new MockWebConnection(); conn.setResponse(URL_FIRST, html1); conn.setResponse(new URL(URL_FIRST, "myDialog.html"), html2); client.setWebConnection(conn); final HtmlPage page = client.getPage(URL_FIRST); final HtmlElement button = page.getHtmlElementById("b"); final HtmlPage dialogPage = button.click(); if (getExpectedAlerts().length > 1) { final DialogWindow dialog = (DialogWindow) dialogPage.getEnclosingWindow(); dialog.close(); } assertEquals(getExpectedAlerts(), actual); }