|
20 | 20 | goog.provide('webdriver.atoms.inject.locators');
|
21 | 21 |
|
22 | 22 | goog.require('bot.locators');
|
| 23 | +goog.require('bot.inject'); |
23 | 24 | goog.require('webdriver.atoms.inject');
|
24 | 25 |
|
| 26 | + |
25 | 27 | /**
|
26 | 28 | * Finds an element by using the given lookup strategy.
|
27 | 29 | * @param {string} strategy The strategy to use to locate the element.
|
28 | 30 | * @param {string} using The locator to use.
|
29 |
| - * @param {(Document|Element)=} opt_root The document or element to perform |
30 |
| - * the search under. If not specified, will use {@code document} |
31 |
| - * as the root. |
32 |
| - * @return {string} The result wrapped |
33 |
| - * in a JSON string as defined by the WebDriver wire protocol. |
| 31 | + * @param {?{ELEMENT: string}=} opt_root The WebElement reference for the |
| 32 | + * element to perform the search under. If not specified, will use |
| 33 | + * {@code document} for the target page. |
| 34 | + * @param {{WINDOW:string}=} opt_window The serialized window object for the |
| 35 | + * page to find the element in. The referenced window must exist in the |
| 36 | + * page executing this script's cache. |
| 37 | + * @return {string} A JSON serialized {@link bot.response.ResponseObject}. |
34 | 38 | */
|
35 |
| -webdriver.atoms.inject.locators.findElement = |
36 |
| - function(strategy, using, opt_root) { |
37 |
| - var locator = {}; |
38 |
| - locator[strategy] = using; |
39 |
| - return webdriver.atoms.inject.executeScript(bot.locators.findElement, |
40 |
| - [locator, opt_root]); |
| 39 | +webdriver.atoms.inject.locators.findElement = function( |
| 40 | + strategy, using, opt_root, opt_window) { |
| 41 | + return webdriver.atoms.inject.locators.performSearch_( |
| 42 | + strategy, using, bot.locators.findElement, opt_root, opt_window); |
41 | 43 | };
|
42 | 44 |
|
43 | 45 |
|
44 | 46 | /**
|
45 | 47 | * Finds all elements by using the given lookup strategy.
|
46 | 48 | * @param {string} strategy The strategy to use to locate the element.
|
47 | 49 | * @param {string} using The locator to use.
|
48 |
| - * @param {(Document|Element)=} opt_root The document or element to perform |
49 |
| - * the search under. If not specified, will use {@code document} |
50 |
| - * as the root. |
51 |
| - * @return {string} The result wrapped |
52 |
| - * in a JSON string as defined by the WebDriver wire protocol. |
| 50 | + * @param {?{ELEMENT: string}=} opt_root The WebElement reference for the |
| 51 | + * element to perform the search under. If not specified, will use |
| 52 | + * {@code document} for the target page. |
| 53 | + * @param {{WINDOW:string}=} opt_window The serialized window object for the |
| 54 | + * page to find the element in. The referenced window must exist in the |
| 55 | + * page executing this script's cache. |
| 56 | + * @return {string} A JSON serialized {@link bot.response.ResponseObject}. |
53 | 57 | */
|
54 |
| -webdriver.atoms.inject.locators.findElements = |
55 |
| - function(strategy, using, opt_root) { |
56 |
| - var locator = {}; |
57 |
| - locator[strategy] = using; |
58 |
| - return webdriver.atoms.inject.executeScript(bot.locators.findElements, |
59 |
| - [locator, opt_root]); |
| 58 | +webdriver.atoms.inject.locators.findElements = function( |
| 59 | + strategy, using, opt_root, opt_window) { |
| 60 | + return webdriver.atoms.inject.locators.performSearch_( |
| 61 | + strategy, using, bot.locators.findElements, opt_root, opt_window); |
| 62 | +}; |
| 63 | + |
| 64 | + |
| 65 | +/** |
| 66 | + * Performs a search for one or more elements. |
| 67 | + * @param {string} strategy The strategy to use to locate the element. |
| 68 | + * @param {string} target The locator to use. |
| 69 | + * @param {(function(!Object, (Document|Element)=): Element| |
| 70 | + * function(!Object, (Document|Element)=): !goog.array.ArrayLike)} |
| 71 | + * searchFn The search function to invoke. |
| 72 | + * @param {?{ELEMENT: string}=} opt_root The WebElement reference for the |
| 73 | + * element to perform the search under. If not specified, will use |
| 74 | + * {@code document} for the target page. |
| 75 | + * @param {{WINDOW:string}=} opt_window The serialized window object for the |
| 76 | + * page to find the element in. The referenced window must exist in the |
| 77 | + * page executing this script's cache. |
| 78 | + * @return {string} A JSON serialized {@link bot.response.ResponseObject}. |
| 79 | + * @private |
| 80 | + */ |
| 81 | +webdriver.atoms.inject.locators.performSearch_ = function( |
| 82 | + strategy, target, searchFn, opt_root, opt_window) { |
| 83 | + var locator = {}; |
| 84 | + locator[strategy] = target; |
| 85 | + |
| 86 | + var response; |
| 87 | + try { |
| 88 | + // Step 1: find the window we are locating the element in. |
| 89 | + var targetWindow = webdriver.atoms.inject.getWindow(opt_window); |
| 90 | + |
| 91 | + // Step 2: decode the root of our search. |
| 92 | + var root; |
| 93 | + if (opt_root) { |
| 94 | + root = /** @type {!Element} */ (bot.inject.cache.getElement( |
| 95 | + opt_root[bot.inject.ELEMENT_KEY], targetWindow.document)); |
| 96 | + } else { |
| 97 | + root = targetWindow.document; |
| 98 | + } |
| 99 | + |
| 100 | + // Step 3: perform the search. |
| 101 | + var found = searchFn(locator, root); |
| 102 | + |
| 103 | + // Step 4: encode our response. |
| 104 | + response = bot.inject.wrapResponse(found); |
| 105 | + } catch (ex) { |
| 106 | + response = bot.inject.wrapError(ex); |
| 107 | + } |
| 108 | + return goog.json.serialize(response); |
60 | 109 | };
|
0 commit comments