Path: blob/trunk/javascript/webdriver/atoms/inputs.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 Synthetic events for fun and profit.19*/2021goog.provide('webdriver.atoms.inputs');2223goog.require('bot.Keyboard');24goog.require('bot.Mouse');25goog.require('bot.action');26goog.require('bot.dom');27goog.require('goog.dom');28goog.require('goog.math.Coordinate');29goog.require('goog.style');30goog.require('webdriver.atoms.element');313233/**34* Send keyboard input to a particular element.35*36* @param {?Element} element The element to send the keyboard input to, or37* `null` to use the document's active element.38* @param {!Array.<string>} keys The keys to type on the element.39* @param {bot.Keyboard.State=} opt_state The predefined keyboard state to use.40* @param {boolean=} opt_persistModifiers Whether modifier keys should remain41* pressed when this function ends.42* @return {bot.Keyboard.State} The keyboard state.43*/44webdriver.atoms.inputs.sendKeys = function(45element, keys, opt_state, opt_persistModifiers) {46var keyboard = new bot.Keyboard(opt_state);47if (!element) {48element = bot.dom.getActiveElement(document);49}50if (!element) {51throw Error('No element to send keys to');52}53webdriver.atoms.element.type(element, keys, keyboard, opt_persistModifiers);5455return keyboard.getState();56};575859/**60* Click on an element.61*62* @param {?Element} element The element to click.63* @param {bot.Mouse.State=} opt_state The serialized state of the mouse.64* @return {!bot.Mouse.State} The mouse state.65*/66webdriver.atoms.inputs.click = function(element, opt_state) {67var mouse = new bot.Mouse(opt_state);68if (!element) {69element = mouse.getState().element;70}71if (!element) {72throw Error('No element to send keys to');73}74bot.action.click(element, null, mouse);75return mouse.getState();76};777879/**80* Move the mouse to a specific element and/or coordinate location.81*82* @param {?Element} element The element to move the mouse to.83* @param {?number} xOffset The x coordinate to use as an offset.84* @param {?number} yOffset The y coordinate to use as an offset.85* @param {bot.Mouse.State=} opt_state The serialized state of the mouse.86* @return {!bot.Mouse.State} The mouse state.87* @suppress {reportUnknownTypes}88*/89webdriver.atoms.inputs.mouseMove = function(element, xOffset, yOffset,90opt_state) {91var mouse = new bot.Mouse(opt_state);92var target = element || mouse.getState()['element'];9394var offsetSpecified = (xOffset != null) && (yOffset != null);95xOffset = xOffset || 0;96yOffset = yOffset || 0;9798// If we have specified an element and no offset, we should99// move the mouse to the center of the specified element.100if (element) {101if (!offsetSpecified) {102var size = bot.action.getInteractableSize(element);103xOffset = Math.floor(size.width / 2);104yOffset = Math.floor(size.height / 2);105}106} else {107// Moving to an absolute offset from the current target element,108// so we have to account for the existing offset of the current109// mouse position to the element origin (upper-left corner).110var pos = goog.style.getClientPosition(target);111xOffset += (mouse.getState()['clientXY']['x'] - pos.x);112yOffset += (mouse.getState()['clientXY']['y'] - pos.y);113}114115var doc = goog.dom.getOwnerDocument(target);116goog.dom.getWindow(doc);117bot.action.scrollIntoView(118target, new goog.math.Coordinate(xOffset, yOffset));119120var coords = new goog.math.Coordinate(xOffset, yOffset);121mouse.move(target, coords);122return mouse.getState();123};124125126/**127* Presses the primary mouse button at the current location.128*129* @param {bot.Mouse.State=} opt_state The serialized state of the mouse.130* @return {!bot.Mouse.State} The mouse state.131*/132webdriver.atoms.inputs.mouseButtonDown = function(opt_state) {133var mouse = new bot.Mouse(opt_state);134mouse.pressButton(bot.Mouse.Button.LEFT);135return mouse.getState();136};137138139/**140* Releases the primary mouse button at the current location.141*142* @param {bot.Mouse.State=} opt_state The serialized state of the mouse.143* @return {!bot.Mouse.State} The mouse state.144*/145webdriver.atoms.inputs.mouseButtonUp = function(opt_state) {146var mouse = new bot.Mouse(opt_state);147mouse.releaseButton();148return mouse.getState();149};150151152/**153* Double-clicks primary mouse button at the current location.154*155* @param {bot.Mouse.State=} opt_state The state of the mouse.156* @return {!bot.Mouse.State} The mouse state.157*/158webdriver.atoms.inputs.doubleClick = function(opt_state) {159var mouse = new bot.Mouse(opt_state);160mouse.pressButton(bot.Mouse.Button.LEFT);161mouse.releaseButton();162mouse.pressButton(bot.Mouse.Button.LEFT);163mouse.releaseButton();164return mouse.getState();165};166167168/**169* Right-clicks mouse button at the current location.170*171* @param {bot.Mouse.State=} opt_state The serialized state of the mouse.172* @return {!bot.Mouse.State} The mouse state.173* @deprecated Use {@link webdriver.atoms.inputs.mouseClick}.174*/175webdriver.atoms.inputs.rightClick = function(opt_state) {176var mouse = new bot.Mouse(opt_state);177mouse.pressButton(bot.Mouse.Button.RIGHT);178mouse.releaseButton();179return mouse.getState();180};181182183/**184* Executes a mousedown/up with the given button at the current mouse185* location.186*187* @param {bot.Mouse.Button} button The button to press.188* @param {bot.Mouse.State=} opt_state The state of the mouse.189* @return {!bot.Mouse.State} The mouse state.190* @suppress {reportUnknownTypes}191*/192webdriver.atoms.inputs.mouseClick = function(button, opt_state) {193// If no target element is specified, try to find it from the194// client (x, y) location. No, this is not exact.195if (opt_state && opt_state['clientXY'] && !opt_state['element'] &&196document.elementFromPoint) {197opt_state['element'] = document.elementFromPoint(198opt_state['clientXY']['x'], opt_state['clientXY']['y']);199}200var mouse = new bot.Mouse(opt_state);201mouse.pressButton(button);202mouse.releaseButton();203return mouse.getState();204};205206207