Path: blob/trunk/javascript/webdriver/atoms/inject/dom.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 for querying the DOM.19*/2021goog.provide('webdriver.atoms.inject.dom');2223goog.require('bot.dom');24goog.require('bot.inject');25goog.require('bot.userAgent');26goog.require('goog.json');27goog.require('webdriver.atoms.element');28goog.require('webdriver.atoms.inject');293031/**32* Gets the visible text for the given element.33* @param {{bot.inject.ELEMENT_KEY: string}} element The element to query.34* @param {{WINDOW: string}=} opt_window The optional window35* containing the element.36* @return {string} The visible text wrapped in a JSON string as defined by the37* WebDriver wire protocol.38*/39webdriver.atoms.inject.dom.getText = function(element, opt_window) {40return webdriver.atoms.inject.dom.executeDomFunction_(41bot.dom.getVisibleText, [element], opt_window);42};434445/**46* @param {{bot.inject.ELEMENT_KEY: string}} element The element to query.47* @param {{WINDOW: string}=} opt_window The optional window48* containing the element.49* @return {string} A boolean describing whether the element is50* checked or selected wrapped in a JSON string as defined by51* the wire protocol.52*/53webdriver.atoms.inject.dom.isSelected = function(element, opt_window) {54return webdriver.atoms.inject.dom.executeDomFunction_(55bot.dom.isSelected, [element], opt_window);56};575859/**60* @param {{bot.inject.ELEMENT_KEY: string}} element The element to query.61* @param {{WINDOW: string}=} opt_window The optional window62* containing the element.63* @return {string} The coordinates of the top left corner in a JSON64* string as defined by the wire protocol.65*/66webdriver.atoms.inject.dom.getTopLeftCoordinates =67function(element, opt_window) {68return webdriver.atoms.inject.dom.executeDomFunction_(69webdriver.atoms.element.getLocationInView, [element], opt_window);70};717273/**74* @param {{bot.inject.ELEMENT_KEY: string}} element The element to query.75* @param {string} attribute The attribute to look up.76* @param {{WINDOW: string}=} opt_window The optional window77* containing the element.78* @return {string} The requested attribute value in a JSON string79* as defined by the wire protocol.80*/81webdriver.atoms.inject.dom.getAttributeValue =82function(element, attribute, opt_window) {83return webdriver.atoms.inject.dom.executeDomFunction_(84webdriver.atoms.element.getAttribute, [element, attribute], opt_window);85};868788/**89* @param {{bot.inject.ELEMENT_KEY: string}} element The element to query.90* @param {{WINDOW: string}=} opt_window The optional window91* containing the element.92* @return {string} The element size in a JSON string as93* defined by the wire protocol.94*/95webdriver.atoms.inject.dom.getSize = function(element, opt_window) {96return webdriver.atoms.inject.dom.executeDomFunction_(97getSize, [element], opt_window);9899function getSize(e) {100var rect = bot.dom.getClientRect(e);101var height = rect.height;102var width = rect.width;103if (!bot.userAgent.IE_DOC_PRE10) {104// On IE10, getBoundingClientRect returns floating point values.105width = Math.floor(width);106height = Math.floor(height);107}108return { 'width': width, 'height': height };109}110};111112113/**114* @param {{bot.inject.ELEMENT_KEY: string}} element The element to query.115* @param {string} property The property to look up.116* @param {{WINDOW: string}=} opt_window The optional window117* containing the element.118* @return {string} The value of the requested CSS property in a JSON119* string as defined by the wire protocol.120*/121webdriver.atoms.inject.dom.getValueOfCssProperty =122function(element, property, opt_window) {123return webdriver.atoms.inject.dom.executeDomFunction_(124bot.dom.getEffectiveStyle, [element, property], opt_window);125};126127128/**129* @param {{bot.inject.ELEMENT_KEY: string}} element The element to query.130* @param {{WINDOW: string}=} opt_window The optional window131* containing the element.132* @return {string} A boolean describing whether the element is enabled133* in a JSON string as defined by the wire protocol.134*/135webdriver.atoms.inject.dom.isEnabled = function(element, opt_window) {136return webdriver.atoms.inject.dom.executeDomFunction_(137bot.dom.isEnabled, [element], opt_window);138};139140141/**142* @param {{bot.inject.ELEMENT_KEY: string}} element The element to check.143* @param {{WINDOW: string}=} opt_window The optional window144* containing the element.145* @return {string} true if the element is visible, false otherwise.146* The result is wrapped in a JSON string as defined by the wire147* protocol.148*/149webdriver.atoms.inject.dom.isDisplayed = function(element, opt_window) {150return webdriver.atoms.inject.dom.executeDomFunction_(151bot.dom.isShown, [element, /*ignoreOpacity=*/true], opt_window);152};153154155/**156* @param {Function} fn The function to call.157* @param {Array.<*>} args An array of function arguments for the function.158* @param {{WINDOW: string}=} opt_window The window context for159* the execution of the function.160* @return {string} The serialized JSON wire protocol result of the function.161* @private162*/163webdriver.atoms.inject.dom.executeDomFunction_ =164function(fn, args, opt_window) {165var response;166try {167var targetWindow = webdriver.atoms.inject.getWindow(opt_window);168var unwrappedArgs = /**@type {Object}*/(bot.inject.unwrapValue(args,169targetWindow.document));170var functionResult = fn.apply(null, unwrappedArgs);171response = bot.inject.wrapResponse(functionResult);172} catch (ex) {173response = bot.inject.wrapError(ex);174}175return goog.json.serialize(response);176};177178179