Search in sources :

Example 1 with TopLevelWindow

use of com.gargoylesoftware.htmlunit.TopLevelWindow in project htmlunit by HtmlUnit.

the class HtmlPage method initialize.

/**
 * Initialize this page.
 * @throws IOException if an IO problem occurs
 * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
 * {@link com.gargoylesoftware.htmlunit.WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is set
 * to true.
 */
@Override
public void initialize() throws IOException, FailingHttpStatusCodeException {
    final WebWindow enclosingWindow = getEnclosingWindow();
    final boolean isAboutBlank = getUrl() == UrlUtils.URL_ABOUT_BLANK;
    if (isAboutBlank) {
        // a frame contains first a faked "about:blank" before its real content specified by src gets loaded
        if (enclosingWindow instanceof FrameWindow && !((FrameWindow) enclosingWindow).getFrameElement().isContentLoaded()) {
            return;
        }
        // save the URL that should be used to resolve relative URLs in this page
        if (enclosingWindow instanceof TopLevelWindow) {
            final TopLevelWindow topWindow = (TopLevelWindow) enclosingWindow;
            final WebWindow openerWindow = topWindow.getOpener();
            if (openerWindow != null && openerWindow.getEnclosedPage() != null) {
                baseUrl_ = openerWindow.getEnclosedPage().getWebResponse().getWebRequest().getUrl();
            }
        }
    }
    if (!isAboutBlank) {
        setReadyState(READY_STATE_INTERACTIVE);
        getDocumentElement().setReadyState(READY_STATE_INTERACTIVE);
    }
    executeDeferredScriptsIfNeeded();
    executeEventHandlersIfNeeded(Event.TYPE_DOM_DOCUMENT_LOADED);
    loadFrames();
    // see Node.initInlineFrameIfNeeded()
    if (!isAboutBlank) {
        if (hasFeature(FOCUS_BODY_ELEMENT_AT_START)) {
            setElementWithFocus(getBody());
        }
        setReadyState(READY_STATE_COMPLETE);
        getDocumentElement().setReadyState(READY_STATE_COMPLETE);
    }
    // frame initialization has a different order
    boolean isFrameWindow = enclosingWindow instanceof FrameWindow;
    boolean isFirstPageInFrameWindow = false;
    if (isFrameWindow) {
        isFrameWindow = ((FrameWindow) enclosingWindow).getFrameElement() instanceof HtmlFrame;
        final History hist = enclosingWindow.getHistory();
        if (hist.getLength() > 0 && UrlUtils.URL_ABOUT_BLANK == hist.getUrl(0)) {
            isFirstPageInFrameWindow = hist.getLength() <= 2;
        } else {
            isFirstPageInFrameWindow = enclosingWindow.getHistory().getLength() < 2;
        }
    }
    if (isFrameWindow && !isFirstPageInFrameWindow) {
        executeEventHandlersIfNeeded(Event.TYPE_LOAD);
    }
    for (final FrameWindow frameWindow : getFrames()) {
        if (frameWindow.getFrameElement() instanceof HtmlFrame) {
            final Page page = frameWindow.getEnclosedPage();
            if (page != null && page.isHtmlPage()) {
                ((HtmlPage) page).executeEventHandlersIfNeeded(Event.TYPE_LOAD);
            }
        }
    }
    if (!isFrameWindow) {
        executeEventHandlersIfNeeded(Event.TYPE_LOAD);
        if (!isAboutBlank && enclosingWindow.getWebClient().isJavaScriptEnabled() && hasFeature(EVENT_FOCUS_ON_LOAD)) {
            final HtmlElement body = getBody();
            if (body != null) {
                final Event event = new Event((Window) enclosingWindow.getScriptableObject(), Event.TYPE_FOCUS);
                body.fireEvent(event);
            }
        }
    }
    try {
        while (!afterLoadActions_.isEmpty()) {
            final PostponedAction action = afterLoadActions_.remove(0);
            action.execute();
        }
    } catch (final IOException e) {
        throw e;
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
    executeRefreshIfNeeded();
}
Also used : Page(com.gargoylesoftware.htmlunit.Page) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) IOException(java.io.IOException) History(com.gargoylesoftware.htmlunit.History) ElementNotFoundException(com.gargoylesoftware.htmlunit.ElementNotFoundException) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) DOMException(org.w3c.dom.DOMException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) BeforeUnloadEvent(com.gargoylesoftware.htmlunit.javascript.host.event.BeforeUnloadEvent) PostponedAction(com.gargoylesoftware.htmlunit.javascript.PostponedAction) TopLevelWindow(com.gargoylesoftware.htmlunit.TopLevelWindow)

Example 2 with TopLevelWindow

use of com.gargoylesoftware.htmlunit.TopLevelWindow in project htmlunit by HtmlUnit.

the class HTMLCollectionFrames method initialize.

/**
 * Initializes this window.
 * @param webWindow the web window corresponding to this window
 * @param pageToEnclose the page that will become the enclosing page
 */
public void initialize(final WebWindow webWindow, final Page pageToEnclose) {
    webWindow_ = webWindow;
    webWindow_.setScriptableObject(this);
    windowProxy_ = new WindowProxy(webWindow_);
    if (pageToEnclose instanceof XmlPage) {
        document_ = new XMLDocument();
    } else {
        document_ = new HTMLDocument();
    }
    document_.setParentScope(this);
    document_.setPrototype(getPrototype(document_.getClass()));
    document_.setWindow(this);
    if (pageToEnclose instanceof SgmlPage) {
        final SgmlPage page = (SgmlPage) pageToEnclose;
        document_.setDomNode(page);
        if (page.isHtmlPage()) {
            final HtmlPage htmlPage = (HtmlPage) page;
            htmlPage.addAutoCloseable(this);
        }
    }
    documentProxy_ = new DocumentProxy(webWindow_);
    navigator_ = new Navigator();
    navigator_.setParentScope(this);
    navigator_.setPrototype(getPrototype(navigator_.getClass()));
    screen_ = new Screen(getWebWindow().getScreen());
    screen_.setParentScope(this);
    screen_.setPrototype(getPrototype(screen_.getClass()));
    history_ = new History();
    history_.setParentScope(this);
    history_.setPrototype(getPrototype(history_.getClass()));
    location_ = new Location();
    location_.setParentScope(this);
    location_.setPrototype(getPrototype(location_.getClass()));
    location_.initialize(this, pageToEnclose);
    final Console console = new Console();
    console.setWebWindow(webWindow_);
    console.setParentScope(this);
    console.setPrototype(getPrototype(console.getClass()));
    console_ = console;
    applicationCache_ = new ApplicationCache();
    applicationCache_.setParentScope(this);
    applicationCache_.setPrototype(getPrototype(applicationCache_.getClass()));
    // like a JS new Object()
    final Context ctx = Context.getCurrentContext();
    controllers_ = ctx.newObject(this);
    if (webWindow_ instanceof TopLevelWindow) {
        final WebWindow opener = ((TopLevelWindow) webWindow_).getOpener();
        if (opener != null) {
            opener_ = opener.getScriptableObject();
        }
    }
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) XMLDocument(com.gargoylesoftware.htmlunit.javascript.host.xml.XMLDocument) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) XmlPage(com.gargoylesoftware.htmlunit.xml.XmlPage) DocumentProxy(com.gargoylesoftware.htmlunit.javascript.host.html.DocumentProxy) TopLevelWindow(com.gargoylesoftware.htmlunit.TopLevelWindow)

Example 3 with TopLevelWindow

use of com.gargoylesoftware.htmlunit.TopLevelWindow in project htmlunit by HtmlUnit.

the class HistoryTest method go.

/**
 * @throws Exception if an error occurs
 */
@Test
public void go() throws Exception {
    final WebClient client = getWebClient();
    final TopLevelWindow window = (TopLevelWindow) client.getCurrentWindow();
    final History history = window.getHistory();
    final String urlA = URL_FIRST + "HistoryTest_a.html";
    final String urlB = URL_FIRST + "HistoryTest_b.html";
    final String urlBX = URL_FIRST + "HistoryTest_b.html#x";
    final String urlC = URL_FIRST + "HistoryTest_c.html";
    HtmlPage page = client.getPage(urlA);
    assertEquals(1, history.getLength());
    assertEquals(0, history.getIndex());
    assertEquals(urlA, page.getUrl());
    page = page.getAnchorByName("b").click();
    assertEquals(2, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());
    page = page.getAnchorByName("x").click();
    assertEquals(3, history.getLength());
    assertEquals(2, history.getIndex());
    assertEquals(urlBX, page.getUrl());
    page = page.getAnchorByName("minusTwo").click();
    assertEquals(3, history.getLength());
    assertEquals(0, history.getIndex());
    assertEquals(urlA, page.getUrl());
    page = page.getAnchorByName("plusOne").click();
    assertEquals(3, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());
    page = page.getAnchorByName("c").click();
    assertEquals(3, history.getLength());
    assertEquals(2, history.getIndex());
    assertEquals(urlC, page.getUrl());
    page = page.getAnchorByName("minusOne").click();
    assertEquals(3, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());
    page = page.getAnchorByName("plusTwo").click();
    assertEquals(3, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) History(com.gargoylesoftware.htmlunit.History) WebClient(com.gargoylesoftware.htmlunit.WebClient) TopLevelWindow(com.gargoylesoftware.htmlunit.TopLevelWindow) Test(org.junit.Test)

Example 4 with TopLevelWindow

use of com.gargoylesoftware.htmlunit.TopLevelWindow in project htmlunit by HtmlUnit.

the class HistoryTest method backAndForward.

/**
 * @throws Exception if an error occurs
 */
@Test
public void backAndForward() throws Exception {
    final WebClient client = getWebClient();
    final TopLevelWindow window = (TopLevelWindow) client.getCurrentWindow();
    final History history = window.getHistory();
    final String urlA = URL_FIRST + "HistoryTest_a.html";
    final String urlB = URL_FIRST + "HistoryTest_b.html";
    final String urlBX = URL_FIRST + "HistoryTest_b.html#x";
    final String urlC = URL_FIRST + "HistoryTest_c.html";
    HtmlPage page = client.getPage(urlA);
    assertEquals(1, history.getLength());
    assertEquals(0, history.getIndex());
    assertEquals(urlA, page.getUrl());
    page = page.getAnchorByName("b").click();
    assertEquals(2, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());
    page = page.getAnchorByName("x").click();
    assertEquals(3, history.getLength());
    assertEquals(2, history.getIndex());
    assertEquals(urlBX, page.getUrl());
    page = page.getAnchorByName("back").click();
    assertEquals(3, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());
    page = page.getAnchorByName("back").click();
    assertEquals(3, history.getLength());
    assertEquals(0, history.getIndex());
    assertEquals(urlA, page.getUrl());
    page = page.getAnchorByName("forward").click();
    assertEquals(3, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());
    page = page.getAnchorByName("c").click();
    assertEquals(3, history.getLength());
    assertEquals(2, history.getIndex());
    assertEquals(urlC, page.getUrl());
    page = page.getAnchorByName("back").click();
    assertEquals(3, history.getLength());
    assertEquals(1, history.getIndex());
    assertEquals(urlB, page.getUrl());
    page = page.getAnchorByName("forward").click();
    assertEquals(3, history.getLength());
    assertEquals(2, history.getIndex());
    assertEquals(urlC, page.getUrl());
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) History(com.gargoylesoftware.htmlunit.History) WebClient(com.gargoylesoftware.htmlunit.WebClient) TopLevelWindow(com.gargoylesoftware.htmlunit.TopLevelWindow) Test(org.junit.Test)

Example 5 with TopLevelWindow

use of com.gargoylesoftware.htmlunit.TopLevelWindow in project htmlunit by HtmlUnit.

the class JavaScriptJobManagerTest method interruptAllWithRecursiveSetTimeout.

/**
 * Test for Bug #487 that makes sure closing a window prevents a
 * recursive setTimeout from continuing forever.
 *
 * @throws Exception if the test fails
 */
@Test
public void interruptAllWithRecursiveSetTimeout() throws Exception {
    final String content = "<html>\n" + "<head>\n" + "  <title>test</title>\n" + "  <script>\n" + "    var threadID;\n" + "    function test() {\n" + "      alert('ping');\n" + "      threadID = setTimeout(test, 5);\n" + "    }\n" + "  </script>\n" + "</head>\n" + "<body onload='test()'>\n" + "</body>\n" + "</html>";
    final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
    final HtmlPage page = loadPage(content, collectedAlerts);
    final JavaScriptJobManager jobManager = page.getEnclosingWindow().getJobManager();
    assertNotNull(jobManager);
    // Not perfect, but 100 chances to start should be enough for a loaded system
    Thread.sleep(500);
    assertFalse("At least one alert should have fired by now", collectedAlerts.isEmpty());
    ((TopLevelWindow) page.getEnclosingWindow()).close();
    // 100 chances to stop
    jobManager.waitForJobs(500);
    final int finalValue = collectedAlerts.size();
    // 100 chances to fail
    jobManager.waitForJobs(500);
    assertEquals("No new alerts should have happened", finalValue, collectedAlerts.size());
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) TopLevelWindow(com.gargoylesoftware.htmlunit.TopLevelWindow) Test(org.junit.Test)

Aggregations

TopLevelWindow (com.gargoylesoftware.htmlunit.TopLevelWindow)8 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)6 WebClient (com.gargoylesoftware.htmlunit.WebClient)5 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)5 IOException (java.io.IOException)4 History (com.gargoylesoftware.htmlunit.History)3 Test (org.junit.Test)3 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)2 ElementNotFoundException (com.gargoylesoftware.htmlunit.ElementNotFoundException)1 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)1 Page (com.gargoylesoftware.htmlunit.Page)1 PostponedAction (com.gargoylesoftware.htmlunit.javascript.PostponedAction)1 BeforeUnloadEvent (com.gargoylesoftware.htmlunit.javascript.host.event.BeforeUnloadEvent)1 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)1 DocumentProxy (com.gargoylesoftware.htmlunit.javascript.host.html.DocumentProxy)1 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)1 XMLDocument (com.gargoylesoftware.htmlunit.javascript.host.xml.XMLDocument)1 XmlPage (com.gargoylesoftware.htmlunit.xml.XmlPage)1 MalformedURLException (java.net.MalformedURLException)1 Context (net.sourceforge.htmlunit.corejs.javascript.Context)1