|
29 | 29 | import org.openqa.selenium.JavascriptException; |
30 | 30 | import org.openqa.selenium.JavascriptExecutor; |
31 | 31 | import org.openqa.selenium.ScriptTimeoutException; |
| 32 | +import org.openqa.selenium.UnhandledAlertException; |
32 | 33 | import org.openqa.selenium.WebDriver; |
33 | 34 | import org.openqa.selenium.WebDriverException; |
34 | 35 | import org.openqa.selenium.WebElement; |
@@ -343,132 +344,118 @@ public void shouldBeAbleToPassMultipleArgumentsToAsyncScripts() throws Exception |
343 | 344 | } |
344 | 345 |
|
345 | 346 | /* 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 | + } |
472 | 459 |
|
473 | 460 | private static long getNumDivElements(final WebDriver driver) { |
474 | 461 | // Selenium does not support "findElements" yet, so we have to do this through a script. |
|
0 commit comments