Path: blob/trunk/javascript/webdriver/atoms/inject/execute_script.js
2868 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617/**18* @fileoverview Wrapping execute script to use a serialized window object.19*20*/2122goog.provide('webdriver.atoms.inject');2324goog.require('bot.inject');25goog.require('bot.inject.cache');262728/**29* Wrapper to allow passing a serialized window object to executeScript.30*31* @param {!(string|Function)} fn The function to execute.32* @param {Array.<*>} args Array of arguments to pass to fn.33* @param {{WINDOW: string}=} opt_window The serialized window object to be34* read from the cache.35* @return {string} The response object, serialized and returned in string36* format.37*/38webdriver.atoms.inject.executeScript = function(fn, args, opt_window) {39return /**@type {string}*/(bot.inject.executeScript(fn, args, true,40webdriver.atoms.inject.getWindow(opt_window)));41};424344/**45*46* @param {!(string|Function)} fn The function to execute.47* @param {Array.<*>} args Array of arguments to pass to fn.48* @param {number} timeout The timeout to wait up to in millis.49* @param {function(string)|function(!bot.response.ResponseObject)} onDone50* The function to call when the given `fn` invokes its callback,51* or when an exception or timeout occurs. This will always be called.52* @param {{WINDOW: string}=} opt_window The serialized window53* object to be read from the cache.54*/55webdriver.atoms.inject.executeAsyncScript =56function(fn, args, timeout, onDone, opt_window) {57bot.inject.executeAsyncScript(58fn, args, timeout, onDone, true,59webdriver.atoms.inject.getWindow(opt_window));60};616263/**64* Decodes a serialized {WINDOW: string} object using the current document's65* cache.66*67* @param {{WINDOW: string}=} opt_window The serialized window object to be68* read from the cache. If undefined, this function will trivially return69* the current window.70* @return {!Window} A reference to a window.71* @throws {bot.Error} If the serialized window cannot be found in the current72* document's cache.73*/74webdriver.atoms.inject.getWindow = function(opt_window) {75var win;76if (opt_window) {77win = bot.inject.cache.getElement(opt_window[bot.inject.WINDOW_KEY]);78} else {79win = window;80}81return /**@type {!Window}*/(win);82};838485