| <!DOCTYPE html> |
| <!-- |
| Copyright 2011 Software Freedom Conservancy. All Rights Reserved. |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| <title>execute_async_script_test.html</title> |
| <script src="../test/test_bootstrap.js"></script> |
| <script> |
| goog.require('bot.userAgent'); |
| goog.require('goog.userAgent'); |
| goog.require('goog.userAgent.product'); |
| goog.require('webdriver.Builder'); |
| goog.require('webdriver.testing.jsunit'); |
| </script> |
| <iframe src="ajaxy_page.html" width="100%" height="300px"></iframe> |
| <script> |
| var driver; |
| |
| function setUp() { |
| driver = new webdriver.Builder().build(); |
| // Not sure what the driver is focused on, so start over. |
| driver.switchTo().defaultContent(); |
| driver.switchTo().frame(0); |
| |
| document.getElementsByTagName('iframe')[0].src = 'ajaxy_page.html'; |
| } |
| |
| function testShouldNotTimeoutIfCallbackInvokedImmediately() { |
| assertThat(driver.executeAsyncScript('arguments[arguments.length - 1](123);'), |
| equals(123)); |
| } |
| |
| function testShouldBeAbleToReturnJavaScriptPrimitiesFromAsyncScripts_neitherNullNorUndefined() { |
| assertThat(driver.executeAsyncScript('arguments[arguments.length - 1](123);'), |
| equals(123)); |
| assertThat(driver.executeAsyncScript('arguments[arguments.length - 1]("abc");'), |
| equals('abc')); |
| assertThat(driver.executeAsyncScript('arguments[arguments.length - 1](false);'), |
| equals(false)); |
| } |
| |
| function testShouldBeAbleToReturnJavaScriptPrimitiesFromAsyncScripts_nullAndUndefined() { |
| assertThat(driver.executeAsyncScript('arguments[arguments.length - 1](null);'), |
| equals(null)); |
| assertThat(driver.executeAsyncScript('arguments[arguments.length - 1]();'), |
| equals(null)); |
| } |
| |
| function testShouldBeAbleToReturnAnArrayLiteralFromAnAsyncScript() { |
| driver.executeAsyncScript('arguments[arguments.length - 1]([]);'). |
| addCallback(function(result) { |
| assertNotNull(result); |
| assertTrue(goog.typeOf(result), goog.isArray(result)); |
| assertEquals(0, result.length); |
| }); |
| } |
| |
| function testShouldBeAbleToReturnAnArrayObjectFromAnAsyncScript() { |
| driver.executeAsyncScript('arguments[arguments.length - 1](new Array());'). |
| addCallback(function(result) { |
| assertNotNull(result); |
| assertTrue(goog.typeOf(result), goog.isArray(result)); |
| assertEquals(0, result.length); |
| }); |
| } |
| |
| function testShouldBeAbleToReturnArraysOfPrimitivesFromAsyncScripts() { |
| var result = driver.executeAsyncScript( |
| "arguments[arguments.length - 1]([null, 123, 'abc', true, false]);"); |
| result.addCallback(function(results) { |
| assertNotNull(results); |
| assertEquals(5, results.length); |
| |
| assertNull(results.shift()); |
| assertEquals(123, results.shift()); |
| assertEquals('abc', results.shift()); |
| assertTrue(results.shift()); |
| assertFalse(results.shift()); |
| assertEquals(0, results.length); |
| }); |
| } |
| |
| |
| function testShouldBeAbleToReturnWebElementsFromAsyncScripts() { |
| driver.executeAsyncScript("arguments[arguments.length - 1](document.body);"). |
| addCallback(function(result) { |
| assertThat(result.getTagName(), matchesRegex(/^body$/i)); |
| }); |
| } |
| |
| |
| function testShouldBeAbleToReturnArraysOfWebElementsFromAsyncScripts() { |
| driver.executeAsyncScript( |
| "arguments[arguments.length - 1]([document.body, document.body]);"). |
| addCallback(function(results) { |
| assertNotNull(results); |
| assertEquals(2, results.length); |
| assertThat(results[0].getTagName(), matchesRegex(/^body$/i)); |
| assertThat(results[1].getTagName(), matchesRegex(/^body$/i)); |
| assertThat( |
| driver.executeScript('return arguments[0] == arguments[1]', |
| results[0], results[1]), |
| equals(true)); |
| }); |
| } |
| |
| function testShouldTimeoutIfScriptDoesNotInvokeCallback() { |
| // Script is expected to be async and explicitly callback, so this should timeout. |
| driver.executeAsyncScript("return 1 + 2;"). |
| addCallbacks( |
| function() { |
| fail('Should have timed out!'); |
| }, |
| function() { |
| return true; // Expected. |
| }); |
| } |
| |
| function testShouldTimeoutIfScriptDoesNotInvokeCallbackWithAZeroTimeout() { |
| driver.executeAsyncScript("window.setTimeout(function() {}, 0);"). |
| addCallbacks( |
| function() { |
| fail('Should have timed out!'); |
| }, |
| function() { |
| return true; // Expected. |
| }); |
| } |
| |
| function testShouldNotTimeoutIfScriptCallsbackInsideAZeroTimeout() { |
| driver.executeAsyncScript( |
| "var callback = arguments[arguments.length - 1];" + |
| "window.setTimeout(function() { callback(123); }, 0)"); |
| } |
| |
| |
| function testShouldTimeoutIfScriptDoesNotInvokeCallbackWithLongTimeout() { |
| driver.manage().timeouts().setScriptTimeout(500); |
| driver.executeAsyncScript( |
| "var callback = arguments[arguments.length - 1];" + |
| "window.setTimeout(callback, 1500);"). |
| addCallbacks( |
| function() { |
| fail('Should have timed out!'); |
| }, |
| function() { |
| return true; // Expected. |
| }); |
| } |
| |
| function testShouldDetectPageLoadsWhileWaitingOnAnAsyncScriptAndReturnAnError() { |
| // Avoid actually triggering an unload event and keep things as stable as |
| // possible. Unfortunately, we can't fire an unload event manually for IE, |
| // so we force one with doc.open/close. See: |
| // http://msdn.microsoft.com/en-us/library/ms536973(v=vs.85).aspx |
| var script; |
| if (bot.userAgent.IE_DOC_PRE9) { |
| script = 'document.open(); document.close();'; |
| } else { |
| script = [ |
| 'var event = frameWin.document.createEvent("HTMLEvents");', |
| 'event.initEvent("unload", true, true);', |
| 'frameWin.dispatchEvent(event);' |
| ].join(''); |
| } |
| |
| driver.manage().timeouts().setScriptTimeout(100); |
| driver.executeAsyncScript(script).addCallbacks( |
| function() { |
| fail('Page load should have triggered an error.'); |
| }, |
| function() { |
| return true; // Expected. |
| }); |
| } |
| |
| function testShouldCatchErrorsWhenExecutingInitialScript() { |
| driver.executeAsyncScript("throw Error('you should catch this!');"). |
| addCallbacks(fail, function() { |
| return true; // Expected. |
| }); |
| } |
| |
| function testShouldBeAbleToPassMultipleArgumentsToAsyncScripts() { |
| var result = driver.executeAsyncScript( |
| "arguments[arguments.length - 1](arguments[0] + arguments[1]);", 1, 2); |
| assertThat(result, equals(3)); |
| } |
| |
| function testShouldBeAbleToExecuteAsynchronousScripts() { |
| driver.wait(function() { |
| return driver.isElementPresent(By.name('typer')); |
| }, 1000, 'Typing target not found; page load error?'); |
| |
| var typer = driver.findElement(By.name('typer')); |
| typer.sendKeys("bob"); |
| assertThat(typer.getAttribute('value'), equals('bob')); |
| |
| driver.findElement(By.id("red")).click(); |
| driver.findElement(By.name("submit")).click(); |
| |
| function getNumDivElements() { |
| return driver.executeScript( |
| 'return document.getElementsByTagName("div").length;'); |
| } |
| |
| assertThat( |
| "There should only be 1 DIV at this point, which is used for " + |
| "the butter message", |
| getNumDivElements(), equals(1)); |
| |
| driver.manage().timeouts().setScriptTimeout(15 * 1000); |
| assertThat( |
| driver.executeAsyncScript( |
| "var callback = arguments[arguments.length - 1];" + |
| "window.registerListener(arguments[arguments.length - 1]);"), |
| equals("bob")); |
| assertThat(typer.getAttribute('value'), equals("")); |
| |
| assertThat( |
| "There should be 1 DIV (for the butter message) + 1 " + |
| "DIV (for the new label)", |
| getNumDivElements(), equals(2)); |
| } |
| </script> |