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();
}
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());
}
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());
}
Aggregations