Path: blob/trunk/javascript/webdriver/atoms/inject/find_element.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 Ready to inject atoms to find elements in the page.19*/2021goog.provide('webdriver.atoms.inject.locators');2223goog.require('bot.inject');24goog.require('bot.inject.cache');25goog.require('bot.locators');26goog.require('goog.json');27goog.require('webdriver.atoms.inject');282930/**31* Finds an element by using the given lookup strategy.32* @param {string} strategy The strategy to use to locate the element.33* @param {string} using The locator to use.34* @param {?{ELEMENT: string}=} opt_root The WebElement reference for the35* element to perform the search under. If not specified, will use36* `document` for the target page.37* @param {{WINDOW: string}=} opt_window The serialized window object for the38* page to find the element in. The referenced window must exist in the39* page executing this script's cache.40* @return {string} A JSON serialized {@link bot.response.ResponseObject}.41*/42webdriver.atoms.inject.locators.findElement = function(43strategy, using, opt_root, opt_window) {44return webdriver.atoms.inject.locators.performSearch_(45strategy, using, bot.locators.findElement, opt_root, opt_window);46};474849/**50* Finds all elements by using the given lookup strategy.51* @param {string} strategy The strategy to use to locate the element.52* @param {string} using The locator to use.53* @param {?{ELEMENT: string}=} opt_root The WebElement reference for the54* element to perform the search under. If not specified, will use55* `document` for the target page.56* @param {{WINDOW: string}=} opt_window The serialized window object for the57* page to find the element in. The referenced window must exist in the58* page executing this script's cache.59* @return {string} A JSON serialized {@link bot.response.ResponseObject}.60*/61webdriver.atoms.inject.locators.findElements = function(62strategy, using, opt_root, opt_window) {63return webdriver.atoms.inject.locators.performSearch_(64strategy, using, bot.locators.findElements, opt_root, opt_window);65};666768/**69* Performs a search for one or more elements.70* @param {string} strategy The strategy to use to locate the element.71* @param {string} target The locator to use.72* @param {(function(!Object, (Document|Element)=): Element|73* function(!Object, (Document|Element)=): !IArrayLike)}74* searchFn The search function to invoke.75* @param {?{ELEMENT: string}=} opt_root The WebElement reference for the76* element to perform the search under. If not specified, will use77* `document` for the target page.78* @param {{WINDOW: string}=} opt_window The serialized window object for the79* page to find the element in. The referenced window must exist in the80* page executing this script's cache.81* @return {string} A JSON serialized {@link bot.response.ResponseObject}.82* @private83*/84webdriver.atoms.inject.locators.performSearch_ = function(85strategy, target, searchFn, opt_root, opt_window) {86var locator = {};87locator[strategy] = target;8889var response;90try {91// Step 1: find the window we are locating the element in.92var targetWindow = webdriver.atoms.inject.getWindow(opt_window);9394// Step 2: decode the root of our search.95var root;96if (opt_root) {97root = /** @type {!Element} */ (bot.inject.cache.getElement(98opt_root[bot.inject.ELEMENT_KEY], targetWindow.document));99} else {100root = targetWindow.document;101}102103// Step 3: perform the search.104var found = searchFn(locator, root);105106// Step 4: encode our response.107response = bot.inject.wrapResponse(found);108} catch (ex) {109response = bot.inject.wrapError(ex);110}111return goog.json.serialize(response);112};113114115