Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/webdriver/atoms/inject/execute_script.js
2868 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
/**
19
* @fileoverview Wrapping execute script to use a serialized window object.
20
*
21
*/
22
23
goog.provide('webdriver.atoms.inject');
24
25
goog.require('bot.inject');
26
goog.require('bot.inject.cache');
27
28
29
/**
30
* Wrapper to allow passing a serialized window object to executeScript.
31
*
32
* @param {!(string|Function)} fn The function to execute.
33
* @param {Array.<*>} args Array of arguments to pass to fn.
34
* @param {{WINDOW: string}=} opt_window The serialized window object to be
35
* read from the cache.
36
* @return {string} The response object, serialized and returned in string
37
* format.
38
*/
39
webdriver.atoms.inject.executeScript = function(fn, args, opt_window) {
40
return /**@type {string}*/(bot.inject.executeScript(fn, args, true,
41
webdriver.atoms.inject.getWindow(opt_window)));
42
};
43
44
45
/**
46
*
47
* @param {!(string|Function)} fn The function to execute.
48
* @param {Array.<*>} args Array of arguments to pass to fn.
49
* @param {number} timeout The timeout to wait up to in millis.
50
* @param {function(string)|function(!bot.response.ResponseObject)} onDone
51
* The function to call when the given `fn` invokes its callback,
52
* or when an exception or timeout occurs. This will always be called.
53
* @param {{WINDOW: string}=} opt_window The serialized window
54
* object to be read from the cache.
55
*/
56
webdriver.atoms.inject.executeAsyncScript =
57
function(fn, args, timeout, onDone, opt_window) {
58
bot.inject.executeAsyncScript(
59
fn, args, timeout, onDone, true,
60
webdriver.atoms.inject.getWindow(opt_window));
61
};
62
63
64
/**
65
* Decodes a serialized {WINDOW: string} object using the current document's
66
* cache.
67
*
68
* @param {{WINDOW: string}=} opt_window The serialized window object to be
69
* read from the cache. If undefined, this function will trivially return
70
* the current window.
71
* @return {!Window} A reference to a window.
72
* @throws {bot.Error} If the serialized window cannot be found in the current
73
* document's cache.
74
*/
75
webdriver.atoms.inject.getWindow = function(opt_window) {
76
var win;
77
if (opt_window) {
78
win = bot.inject.cache.getElement(opt_window[bot.inject.WINDOW_KEY]);
79
} else {
80
win = window;
81
}
82
return /**@type {!Window}*/(win);
83
};
84
85