Search in sources :

Example 1 with History

use of com.gargoylesoftware.htmlunit.History 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 History

use of com.gargoylesoftware.htmlunit.History 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 3 with History

use of com.gargoylesoftware.htmlunit.History 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)

Aggregations

History (com.gargoylesoftware.htmlunit.History)3 TopLevelWindow (com.gargoylesoftware.htmlunit.TopLevelWindow)3 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)2 Test (org.junit.Test)2 ElementNotFoundException (com.gargoylesoftware.htmlunit.ElementNotFoundException)1 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)1 Page (com.gargoylesoftware.htmlunit.Page)1 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)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 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 DOMException (org.w3c.dom.DOMException)1