Skip to content

Commit 32da923

Browse files
committed
make our impl more compatible
1 parent ec53553 commit 32da923

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/main/java/org/openqa/selenium/htmlunit/AsyncScriptExecutor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.htmlunit.corejs.javascript.Function;
2626
import org.htmlunit.corejs.javascript.NativeJavaObject;
2727
import org.htmlunit.html.HtmlPage;
28+
import org.openqa.selenium.JavascriptException;
2829
import org.openqa.selenium.ScriptTimeoutException;
2930
import org.openqa.selenium.UnhandledAlertException;
3031
import org.openqa.selenium.WebDriverException;
@@ -186,7 +187,7 @@ Object waitForResult(final long timeoutMillis) throws InterruptedException {
186187
}
187188

188189
if (unloadDetected_) {
189-
throw new WebDriverException(
190+
throw new JavascriptException(
190191
"Detected a page unload event; executeAsyncScript does not work across page loads");
191192
}
192193
return value_;

src/test/java/org/openqa/selenium/htmlunit/ExecutingAsyncJavascriptTest.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import java.util.Iterator;
2222
import java.util.List;
2323

24+
import org.htmlunit.corejs.javascript.JavaScriptException;
2425
import org.junit.Assert;
2526
import org.junit.Test;
2627
import org.junit.runner.RunWith;
2728
import org.openqa.selenium.By;
29+
import org.openqa.selenium.JavascriptException;
2830
import org.openqa.selenium.JavascriptExecutor;
2931
import org.openqa.selenium.ScriptTimeoutException;
3032
import org.openqa.selenium.WebDriver;
@@ -202,13 +204,27 @@ public void shouldDetectPageLoadsWhileWaitingOnAnAsyncScriptAndReturnAnError() t
202204
getMockWebConnection().setResponse(URL_SECOND, "<html><body></body></html>");
203205

204206
final WebDriver driver = loadPage2("<html><body></body></html>");
205-
driver.manage().timeouts().scriptTimeout(Duration.ofMillis(100));
207+
// driver.manage().timeouts().scriptTimeout(Duration.ofMillis(100));
206208

207209
final JavascriptExecutor executor = (JavascriptExecutor) driver;
208-
// TODO real FF creates JavascriptException
210+
// TODO real FF creates JavascriptException, Chrome/Edge ScriptTimeoutException
211+
/*
209212
Assert.assertThrows(
210213
ScriptTimeoutException.class,
211214
() -> executor.executeAsyncScript("window.location = '" + URL_SECOND + "';"));
215+
*/
216+
final Throwable t = Assert.assertThrows(
217+
Throwable.class,
218+
() -> executor.executeAsyncScript("window.location = '" + URL_SECOND + "';"));
219+
220+
assertTrue(t instanceof ScriptTimeoutException || t instanceof JavascriptException);
221+
222+
if (t instanceof ScriptTimeoutException) {
223+
assertTrue(t.getMessage().startsWith("script timeout"));
224+
}
225+
else if (t instanceof JavaScriptException) {
226+
assertEquals("Document was unloaded", t.getMessage());
227+
}
212228
}
213229

214230
@Test
@@ -261,7 +277,7 @@ public void shouldCatchErrorsWithMessageAndStacktraceWhenExecutingInitialScript(
261277
assertTrue(ex.getMessage().contains("errormessage"));
262278

263279
final Throwable rootCause = ex.getCause();
264-
assertTrue(result instanceof Throwable);
280+
assertTrue(rootCause instanceof Throwable);
265281
// does not work with real browsers because root cause is null
266282
assertTrue(rootCause.getMessage().contains("errormessage"));
267283

0 commit comments

Comments
 (0)