Skip to content

Commit b0ba772

Browse files
duonglaiquangrbri
authored andcommitted
HtmlPage: fix a bug where async script may not be executed
1 parent a304530 commit b0ba772

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/main/java/org/htmlunit/html/HtmlPage.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ public void initialize() throws IOException, FailingHttpStatusCodeException {
260260
executeEventHandlersIfNeeded(Event.TYPE_READY_STATE_CHANGE);
261261
}
262262

263+
executePostponedScriptsIfNeeded();
264+
263265
executeDeferredScriptsIfNeeded();
264266

265267
executeEventHandlersIfNeeded(Event.TYPE_DOM_DOCUMENT_LOADED);
@@ -1477,6 +1479,13 @@ private String getRefreshStringOrNull() {
14771479
return getWebResponse().getResponseHeaderValue("Refresh");
14781480
}
14791481

1482+
private void executePostponedScriptsIfNeeded() {
1483+
if (!getWebClient().isJavaScriptEnabled()) {
1484+
return;
1485+
}
1486+
getWebClient().getJavaScriptEngine().processPostponedActions();
1487+
}
1488+
14801489
/**
14811490
* Executes any deferred scripts, if necessary.
14821491
*/

src/test/java/org/htmlunit/html/HtmlPageTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,4 +1973,25 @@ public void getBaseUrl() throws Exception {
19731973
page = loadPage(getBrowserVersion(), html, null, new URL(URL_FIRST + path));
19741974
assertEquals(URL_FIRST.toExternalForm() + path, page.getBaseURL().toExternalForm());
19751975
}
1976+
1977+
/**
1978+
* @throws Exception if an error occurs
1979+
*/
1980+
@Test
1981+
public void asyncScriptExecutedWithoutEventHandlers() throws Exception {
1982+
final String html = DOCTYPE_HTML
1983+
+ "<html><head>\n"
1984+
+ "<script async src='async.js'></script>\n"
1985+
+ "</head><body></body></html>";
1986+
1987+
final WebClient client = getWebClient();
1988+
final MockWebConnection webConnection = new MockWebConnection();
1989+
webConnection.setDefaultResponse(html);
1990+
webConnection.setResponse(new URL(URL_FIRST, "async.js"),
1991+
"document.title = 'async-executed';", "text/javascript");
1992+
client.setWebConnection(webConnection);
1993+
1994+
final HtmlPage page = client.getPage(URL_FIRST);
1995+
assertEquals("async-executed", page.getTitleText());
1996+
}
19761997
}

0 commit comments

Comments
 (0)