Path: blob/trunk/javascript/selenium-webdriver/bidi/input.js
2884 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// type: module added to package.json18// import { WebElement } from '../lib/webdriver'19const { WebElement } = require('../lib/webdriver')20const { RemoteReferenceType, ReferenceValue } = require('./protocolValue')2122/**23* Represents commands and events related to the Input module (simulated user input).24* Described in https://w3c.github.io/webdriver-bidi/#module-input.25*/26class Input {27constructor(driver) {28this._driver = driver29}3031async init() {32if (!(await this._driver.getCapabilities()).get('webSocketUrl')) {33throw Error('WebDriver instance must support BiDi protocol')34}3536this.bidi = await this._driver.getBidi()37}3839/**40* Performs the specified actions on the given browsing context.41*42* @param {string} browsingContextId - The ID of the browsing context.43* @param {Array} actions - The actions to be performed.44* @returns {Promise} A promise that resolves with the response from the server.45*/46async perform(browsingContextId, actions) {47const _actions = await updateActions(actions)4849const command = {50method: 'input.performActions',51params: {52context: browsingContextId,53actions: _actions,54},55}5657return await this.bidi.send(command)58}5960/**61* Resets the input state in the specified browsing context.62*63* @param {string} browsingContextId - The ID of the browsing context.64* @returns {Promise} A promise that resolves when the release actions are sent.65*/66async release(browsingContextId) {67const command = {68method: 'input.releaseActions',69params: {70context: browsingContextId,71},72}73return await this.bidi.send(command)74}7576/**77* Sets the files property of a given input element.78*79* @param {string} browsingContextId - The ID of the browsing context.80* @param {string | ReferenceValue} element - The ID of the element or a ReferenceValue object representing the element.81* @param {string | string[]} files - The file path or an array of file paths to be set.82* @throws {Error} If the element is not a string or a ReferenceValue.83* @returns {Promise<void>} A promise that resolves when the files are set.84*/85async setFiles(browsingContextId, element, files) {86if (typeof element !== 'string' && !(element instanceof ReferenceValue)) {87throw Error(`Pass in a WebElement id as a string or a ReferenceValue. Received: ${element}`)88}8990const command = {91method: 'input.setFiles',92params: {93context: browsingContextId,94element:95typeof element === 'string'96? new ReferenceValue(RemoteReferenceType.SHARED_ID, element).asMap()97: element.asMap(),98files: typeof files === 'string' ? [files] : files,99},100}101await this.bidi.send(command)102}103}104105async function updateActions(actions) {106const _actions = []107for (const action of actions) {108const sequenceList = action.actions109let updatedSequenceList = []110111if (action.type === 'pointer' || action.type === 'wheel') {112for (const sequence of sequenceList) {113if ((sequence.type === 'pointerMove' || sequence.type === 'scroll') && sequence.origin instanceof WebElement) {114const element = sequence.origin115const elementId = await element.getId()116sequence.origin = {117type: 'element',118element: { sharedId: elementId },119}120}121updatedSequenceList.push(sequence)122}123124const updatedAction = { ...action, actions: updatedSequenceList }125_actions.push(updatedAction)126} else {127_actions.push(action)128}129}130131return _actions132}133134async function getInputInstance(driver) {135let instance = new Input(driver)136await instance.init()137return instance138}139140module.exports = getInputInstance141142143