Skip to content

Commit 73a52d8

Browse files
committed
more tests - we are able to support Alerts already even if Selenium does not (have to check the BiDi version)
1 parent 32da923 commit 73a52d8

File tree

2 files changed

+123
-126
lines changed

2 files changed

+123
-126
lines changed

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

Lines changed: 113 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.openqa.selenium.JavascriptException;
3030
import org.openqa.selenium.JavascriptExecutor;
3131
import org.openqa.selenium.ScriptTimeoutException;
32+
import org.openqa.selenium.UnhandledAlertException;
3233
import org.openqa.selenium.WebDriver;
3334
import org.openqa.selenium.WebDriverException;
3435
import org.openqa.selenium.WebElement;
@@ -343,132 +344,118 @@ public void shouldBeAbleToPassMultipleArgumentsToAsyncScripts() throws Exception
343344
}
344345

345346
/* TODO
346-
@Test
347-
void shouldBeAbleToMakeXMLHttpRequestsAndWaitForTheResponse() {
348-
String script =
349-
"var url = arguments[0];"
350-
+ "var callback = arguments[arguments.length - 1];"
351-
+
352-
// Adapted from http://www.quirksmode.org/js/xmlhttp.html
353-
"var XMLHttpFactories = ["
354-
+ " function () {return new XMLHttpRequest()},"
355-
+ " function () {return new ActiveXObject('Msxml2.XMLHTTP')},"
356-
+ " function () {return new ActiveXObject('Msxml3.XMLHTTP')},"
357-
+ " function () {return new ActiveXObject('Microsoft.XMLHTTP')}"
358-
+ "];"
359-
+ "var xhr = false;"
360-
+ "while (!xhr && XMLHttpFactories.length) {"
361-
+ " try {"
362-
+ " xhr = XMLHttpFactories.shift().call();"
363-
+ " } catch (e) {}"
364-
+ "}"
365-
+ "if (!xhr) throw Error('unable to create XHR object');"
366-
+ "xhr.open('GET', url, true);"
367-
+ "xhr.onreadystatechange = function() {"
368-
+ " if (xhr.readyState == 4) callback(xhr.responseText);"
369-
+ "};"
370-
+ "xhr.send('');"; // empty string to stop firefox 3 from choking
371-
372-
driver.get(pages.ajaxyPage);
373-
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(3));
374-
String response = (String) executor.executeAsyncScript(script, pages.sleepingPage + "?time=2");
375-
assertThat(response.trim())
376-
.isEqualTo("<html><head><title>Done</title></head><body>Slept for 2s</body></html>");
377-
}
378-
379-
@Test
380-
@Ignore(CHROME)
381-
@Ignore(EDGE)
382-
@Ignore(IE)
383-
@Ignore(FIREFOX)
384-
@Ignore(value = SAFARI, reason = "Does not support alerts yet")
385-
public void throwsIfScriptTriggersAlert() {
386-
driver.get(pages.simpleTestPage);
387-
driver.manage().timeouts().scriptTimeout(Duration.ofMillis(5000));
388-
assertThatExceptionOfType(UnhandledAlertException.class)
389-
.isThrownBy(
390-
() ->
391-
executor.executeAsyncScript(
392-
"setTimeout(arguments[0], 200) ; setTimeout(function() { window.alert('Look! An"
393-
+ " alert!'); }, 50);"));
394-
// Shouldn't throw
395-
driver.getTitle();
396-
}
397-
398-
@Test
399-
@Ignore(CHROME)
400-
@Ignore(EDGE)
401-
@Ignore(IE)
402-
@Ignore(FIREFOX)
403-
@Ignore(value = SAFARI, reason = "Does not support alerts yet")
404-
public void throwsIfAlertHappensDuringScript() {
405-
driver.get(pages.slowLoadingAlertPage);
406-
driver.manage().timeouts().scriptTimeout(Duration.ofMillis(5000));
407-
assertThatExceptionOfType(UnhandledAlertException.class)
408-
.isThrownBy(() -> executor.executeAsyncScript("setTimeout(arguments[0], 1000);"));
409-
// Shouldn't throw
410-
driver.getTitle();
411-
}
412-
413-
@Test
414-
@Ignore(CHROME)
415-
@Ignore(EDGE)
416-
@Ignore(IE)
417-
@Ignore(FIREFOX)
418-
@Ignore(value = SAFARI, reason = "Does not support alerts yet")
419-
public void throwsIfScriptTriggersAlertWhichTimesOut() {
420-
driver.get(pages.simpleTestPage);
421-
driver.manage().timeouts().scriptTimeout(Duration.ofMillis(5000));
422-
assertThatExceptionOfType(UnhandledAlertException.class)
423-
.isThrownBy(
424-
() ->
425-
executor.executeAsyncScript(
426-
"setTimeout(function() { window.alert('Look! An alert!'); }, 50);"));
427-
// Shouldn't throw
428-
driver.getTitle();
429-
}
430-
431-
@Test
432-
@Ignore(CHROME)
433-
@Ignore(EDGE)
434-
@Ignore(IE)
435-
@Ignore(FIREFOX)
436-
@Ignore(value = SAFARI, reason = "Does not support alerts yet")
437-
public void throwsIfAlertHappensDuringScriptWhichTimesOut() {
438-
driver.get(pages.slowLoadingAlertPage);
439-
driver.manage().timeouts().scriptTimeout(Duration.ofMillis(5000));
440-
assertThatExceptionOfType(UnhandledAlertException.class)
441-
.isThrownBy(() -> executor.executeAsyncScript(""));
442-
// Shouldn't throw
443-
driver.getTitle();
444-
}
445-
446-
@Test
447-
@Ignore(CHROME)
448-
@Ignore(EDGE)
449-
@Ignore(IE)
450-
@Ignore(FIREFOX)
451-
@Ignore(value = SAFARI, reason = "Does not support alerts yet")
452-
public void includesAlertTextInUnhandledAlertException() {
453-
driver.manage().timeouts().scriptTimeout(Duration.ofMillis(5000));
454-
String alertText = "Look! An alert!";
455-
assertThatExceptionOfType(UnhandledAlertException.class)
456-
.isThrownBy(
457-
() ->
458-
executor.executeAsyncScript(
459-
"setTimeout(arguments[0], 200) ; setTimeout(function() { window.alert('"
460-
+ alertText
461-
+ "'); }, 50);"))
462-
.satisfies(t -> assertThat(t.getAlertText()).isEqualTo(alertText));
463-
}
464-
465-
private long getNumDivElements() {
466-
// Selenium does not support "findElements" yet, so we have to do this through a script.
467-
return (Long)
468-
((JavascriptExecutor) driver)
469-
.executeScript("return document.getElementsByTagName('div').length;");
470-
}
471-
*/
347+
@Test
348+
void shouldBeAbleToMakeXMLHttpRequestsAndWaitForTheResponse() {
349+
String script =
350+
"var url = arguments[0];"
351+
+ "var callback = arguments[arguments.length - 1];"
352+
+
353+
// Adapted from http://www.quirksmode.org/js/xmlhttp.html
354+
"var XMLHttpFactories = ["
355+
+ " function () {return new XMLHttpRequest()},"
356+
+ " function () {return new ActiveXObject('Msxml2.XMLHTTP')},"
357+
+ " function () {return new ActiveXObject('Msxml3.XMLHTTP')},"
358+
+ " function () {return new ActiveXObject('Microsoft.XMLHTTP')}"
359+
+ "];"
360+
+ "var xhr = false;"
361+
+ "while (!xhr && XMLHttpFactories.length) {"
362+
+ " try {"
363+
+ " xhr = XMLHttpFactories.shift().call();"
364+
+ " } catch (e) {}"
365+
+ "}"
366+
+ "if (!xhr) throw Error('unable to create XHR object');"
367+
+ "xhr.open('GET', url, true);"
368+
+ "xhr.onreadystatechange = function() {"
369+
+ " if (xhr.readyState == 4) callback(xhr.responseText);"
370+
+ "};"
371+
+ "xhr.send('');"; // empty string to stop firefox 3 from choking
372+
373+
driver.get(pages.ajaxyPage);
374+
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(3));
375+
String response = (String) executor.executeAsyncScript(script, pages.sleepingPage + "?time=2");
376+
assertThat(response.trim())
377+
.isEqualTo("<html><head><title>Done</title></head><body>Slept for 2s</body></html>");
378+
}
379+
*/
380+
381+
@Test
382+
public void throwsIfScriptTriggersAlert() throws Exception {
383+
final String html = getFileContent("simpleTest.html");
384+
final WebDriver driver = loadPage2(html);
385+
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(5));
386+
387+
final JavascriptExecutor executor = (JavascriptExecutor) driver;
388+
final String js =
389+
"setTimeout(arguments[0], 200);"
390+
+ "setTimeout(function() { window.alert('Look! An alert!'); }, 50);";
391+
392+
// does not throw with selenium but already is supported in HtmlUnitDriver
393+
Assert.assertThrows(UnhandledAlertException.class, () -> executor.executeAsyncScript(js));
394+
395+
assertEquals("Hello WebDriver", driver.getTitle());
396+
}
397+
398+
@Test
399+
public void throwsIfAlertHappensDuringScript() throws Exception {
400+
final String html = getFileContent("slowLoadingAlert.html");
401+
final WebDriver driver = loadPage2(html);
402+
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(5));
403+
404+
final JavascriptExecutor executor = (JavascriptExecutor) driver;
405+
final String js = "setTimeout(arguments[0], 1000);";
406+
407+
// does not throw with selenium but already is supported in HtmlUnitDriver
408+
Assert.assertThrows(UnhandledAlertException.class, () -> executor.executeAsyncScript(js));
409+
410+
assertEquals("slowLoadingAlert", driver.getTitle());
411+
}
412+
413+
@Test
414+
public void throwsIfScriptTriggersAlertWhichTimesOut() throws Exception {
415+
final String html = getFileContent("simpleTest.html");
416+
final WebDriver driver = loadPage2(html);
417+
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(5));
418+
419+
final JavascriptExecutor executor = (JavascriptExecutor) driver;
420+
final String js = "setTimeout(function() { window.alert('Look! An alert!'); }, 50);";
421+
422+
// does not throw with selenium but already is supported in HtmlUnitDriver
423+
Assert.assertThrows(UnhandledAlertException.class, () -> executor.executeAsyncScript(js));
424+
425+
assertEquals("Hello WebDriver", driver.getTitle());
426+
}
427+
428+
@Test
429+
public void throwsIfAlertHappensDuringScriptWhichTimesOut() throws Exception {
430+
final String html = getFileContent("slowLoadingAlert.html");
431+
final WebDriver driver = loadPage2(html);
432+
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(5));
433+
434+
final JavascriptExecutor executor = (JavascriptExecutor) driver;
435+
436+
// does not throw with selenium but already is supported in HtmlUnitDriver
437+
Assert.assertThrows(UnhandledAlertException.class, () -> executor.executeAsyncScript(""));
438+
439+
assertEquals("slowLoadingAlert", driver.getTitle());
440+
}
441+
442+
@Test
443+
public void includesAlertTextInUnhandledAlertException() throws Exception {
444+
final WebDriver driver = loadPage2("<html><body></body></html>");
445+
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(5));
446+
447+
final JavascriptExecutor executor = (JavascriptExecutor) driver;
448+
449+
final String alertText = "Look! An alert!";
450+
final String js = "setTimeout(arguments[0], 200);"
451+
+ "setTimeout(function() { window.alert('" + alertText + "'); }, 50);";
452+
453+
// does not throw with selenium but already is supported in HtmlUnitDriver
454+
final WebDriverException ex =
455+
Assert.assertThrows(UnhandledAlertException.class, () -> executor.executeAsyncScript(js));
456+
457+
assertTrue(ex.getMessage().contains(alertText));
458+
}
472459

473460
private static long getNumDivElements(final WebDriver driver) {
474461
// Selenium does not support "findElements" yet, so we have to do this through a script.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>slowLoadingAlert</title>
5+
</head>
6+
7+
<body onload="window.setTimeout(function() { window.alert('Look, an alert!'); }, 200);">
8+
</body>
9+
10+
</html>

0 commit comments

Comments
 (0)