Path: blob/trunk/javascript/webdriver/atoms/inject/action.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 manipulating the DOM.19*/2021goog.provide('webdriver.atoms.inject.action');2223goog.require('bot.action');24goog.require('bot.inject');25goog.require('goog.json');26goog.require('webdriver.atoms.element');27goog.require('webdriver.atoms.inject');28goog.require('webdriver.atoms.inputs');293031/**32* Sends key events to simulating typing on an element.33*34* @param {bot.inject.JsonElement} element The element to submit.35* @param {!Array.<string>} keys The keys to type.36* @param {bot.inject.JsonWindow=} opt_window The optional window37* containing the element.38* @return {string} A stringified {@link bot.response.ResponseObject}.39*/40webdriver.atoms.inject.action.type = function(element, keys, opt_window) {41return webdriver.atoms.inject.action.executeActionFunction_(42webdriver.atoms.element.type, [element, keys], opt_window);43};444546/**47* Submits the form containing the given element.48*49* @param {bot.inject.JsonElement} element The element to submit.50* @param {bot.inject.JsonWindow=} opt_window The optional window51* containing the element.52* @return {string} A stringified {@link bot.response.ResponseObject}.53* @deprecated Click on a submit button or type ENTER in a text box instead.54*/55webdriver.atoms.inject.action.submit = function(element, opt_window) {56return webdriver.atoms.inject.action.executeActionFunction_(bot.action.submit,57[element], opt_window);58};596061/**62* Clear an element.63*64* @param {bot.inject.JsonElement} element The element to clear.65* @param {bot.inject.JsonWindow=} opt_window The optional window66* containing the element.67* @return {string} A stringified {@link bot.response.ResponseObject}.68* @see bot.action.clear69*/70webdriver.atoms.inject.action.clear = function(element, opt_window) {71return webdriver.atoms.inject.action.executeActionFunction_(bot.action.clear,72[element], opt_window);73};747576/**77* Click an element.78*79* @param {bot.inject.JsonElement} element The element to click.80* @param {bot.inject.JsonWindow=} opt_window The optional window81* containing the element.82* @return {string} A stringified {@link bot.response.ResponseObject}.83* @see bot.action.click84*/85webdriver.atoms.inject.action.click = function (element, opt_window) {86return webdriver.atoms.inject.action.executeActionFunction_(bot.action.click,87[element], opt_window);88};899091/**92* JSON representation of a {@link bot.Mouse.State} object.93* @typedef {{buttonPressed: ?bot.Mouse.Button,94* elementPressed: ?bot.inject.JsonElement,95* clientXY: {x: number, y: number},96* nextClickIsDoubleClick: boolean,97* hasEverInteracted: boolean,98* element: ?bot.inject.JsonElement}}99*/100webdriver.atoms.inject.action.JsonMouseState;101102103/**104* Clicks a mouse button.105*106* @param {bot.Mouse.Button} button The button to press.107* @param {webdriver.atoms.inject.action.JsonMouseState=} opt_mouseState The108* current state of the mouse.109* @param {bot.inject.JsonWindow=} opt_window The window context for110* the execution of the function.111* @return {string} A stringified {@link bot.response.ResponseObject}. The112* mouse's new state, as a113* {@link webdriver.atoms.inject.action.JsonMouseState} will be included114* as the response value.115*/116webdriver.atoms.inject.action.mouseClick = function(117button, opt_mouseState, opt_window) {118return webdriver.atoms.inject.action.executeActionFunction_(119webdriver.atoms.inputs.mouseClick,120[button, opt_mouseState], opt_window);121};122123124/**125* Types a sequence of key strokes on the active element.126* @param {!Array.<string>} keys The keys to type.127* @param {bot.Keyboard.State=} opt_keyboardState The keyboard's state.128* @param {bot.inject.JsonWindow=} opt_window The window context for129* the execution of the function.130* @return {string} A stringified {@link bot.response.ResponseObject}. The131* keyboard's new state, as a {@link bot.Keyboard.State} will be included132* as the response value.133*/134webdriver.atoms.inject.action.sendKeysToActiveElement = function(135keys, opt_keyboardState, opt_window) {136var persistModifiers = true;137return webdriver.atoms.inject.action.executeActionFunction_(138webdriver.atoms.inputs.sendKeys,139[null, keys, opt_keyboardState, persistModifiers], opt_window);140};141142/**143* Moves the mouse to a specific element and/or coordinate location.144*145* @param {?bot.inject.JsonElement} element The element to move the mouse146* relative to, or `null` to use the mouse's current position.147* @param {?number} xOffset A horizontal offset, relative to the left edge of148* the given element, or the mouse's current position if no element is149* specified.150* @param {?number} yOffset A vertical offset, relative to the top edge of151* the given element, or the mouse's current position if no element152* is specified.153* @param {webdriver.atoms.inject.action.JsonMouseState=} opt_mouseState The154* current state of the mouse.155* @param {bot.inject.JsonWindow=} opt_window The window context for156* the execution of the function.157* @return {string} A stringified {@link bot.response.ResponseObject}. The158* mouse's new state, as a159* {@link webdriver.atoms.inject.action.JsonMouseState} will be included160* as the response value.161*/162webdriver.atoms.inject.action.mouseMove = function(163element, xOffset, yOffset, opt_mouseState, opt_window) {164return webdriver.atoms.inject.action.executeActionFunction_(165webdriver.atoms.inputs.mouseMove,166[element, xOffset, yOffset, opt_mouseState], opt_window);167};168169170/**171* Presses the primary mouse button at the current location.172*173* @param {webdriver.atoms.inject.action.JsonMouseState=} opt_mouseState The174* current state of the mouse.175* @param {bot.inject.JsonWindow=} opt_window The window context for176* the execution of the function.177* @return {string} A stringified {@link bot.response.ResponseObject}. The178* mouse's new state, as a179* {@link webdriver.atoms.inject.action.JsonMouseState} will be included180* as the response value.181*/182webdriver.atoms.inject.action.mouseButtonDown = function(opt_mouseState, opt_window) {183return webdriver.atoms.inject.action.executeActionFunction_(184webdriver.atoms.inputs.mouseButtonDown,185[opt_mouseState], opt_window);186};187188189/**190* Releases the primary mouse button at the current location.191*192* @param {webdriver.atoms.inject.action.JsonMouseState=} opt_mouseState The193* current state of the mouse.194* @param {bot.inject.JsonWindow=} opt_window The window context for195* the execution of the function.196* @return {string} A stringified {@link bot.response.ResponseObject}. The197* mouse's new state, as a198* {@link webdriver.atoms.inject.action.JsonMouseState} will be included199* as the response value.200*/201webdriver.atoms.inject.action.mouseButtonUp = function(opt_mouseState, opt_window) {202return webdriver.atoms.inject.action.executeActionFunction_(203webdriver.atoms.inputs.mouseButtonUp,204[opt_mouseState], opt_window);205};206207/**208* Double-clicks the primary mouse button.209*210* @param {webdriver.atoms.inject.action.JsonMouseState=} opt_mouseState The211* current state of the mouse.212* @param {bot.inject.JsonWindow=} opt_window The window context for213* the execution of the function.214* @return {string} A stringified {@link bot.response.ResponseObject}. The215* mouse's new state, as a216* {@link webdriver.atoms.inject.action.JsonMouseState} will be included217* as the response value.218*/219webdriver.atoms.inject.action.doubleClick = function (220opt_mouseState, opt_window) {221return webdriver.atoms.inject.action.executeActionFunction_(222webdriver.atoms.inputs.doubleClick,223[opt_mouseState], opt_window);224};225226227/**228* @param {!Function} fn The function to call.229* @param {!Array.<*>} args An array of function arguments for the function.230* @param {bot.inject.JsonWindow=} opt_window The window context for231* the execution of the function.232* @return {string} The serialized JSON wire protocol result of the function.233* @private234*/235webdriver.atoms.inject.action.executeActionFunction_ = function (236fn, args, opt_window) {237var response;238try {239var targetWindow = webdriver.atoms.inject.getWindow(opt_window);240var unwrappedArgs = /** @type {Array} */(bot.inject.unwrapValue(241args, targetWindow.document));242var functionResult = fn.apply(null, unwrappedArgs);243response = bot.inject.wrapResponse(functionResult);244} catch (ex) {245response = bot.inject.wrapError(ex);246}247return goog.json.serialize(response);248};249250251