|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +// type: module added to package.json |
| 19 | +// import { WebElement } from '../lib/webdriver' |
| 20 | +const { WebElement } = require('../lib/webdriver') |
| 21 | + |
| 22 | +class Input { |
| 23 | + constructor(driver) { |
| 24 | + this._driver = driver |
| 25 | + } |
| 26 | + |
| 27 | + async init() { |
| 28 | + if (!(await this._driver.getCapabilities()).get('webSocketUrl')) { |
| 29 | + throw Error('WebDriver instance must support BiDi protocol') |
| 30 | + } |
| 31 | + |
| 32 | + this.bidi = await this._driver.getBidi() |
| 33 | + } |
| 34 | + |
| 35 | + async perform(browsingContextId, actions) { |
| 36 | + const _actions = await updateActions(actions) |
| 37 | + |
| 38 | + const command = { |
| 39 | + method: 'input.performActions', |
| 40 | + params: { |
| 41 | + context: browsingContextId, |
| 42 | + actions: _actions, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + let response = await this.bidi.send(command) |
| 47 | + |
| 48 | + return response |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +async function updateActions(actions) { |
| 53 | + const _actions = [] |
| 54 | + for (const action of actions) { |
| 55 | + const sequenceList = action.actions |
| 56 | + let updatedSequenceList = [] |
| 57 | + |
| 58 | + if (action.type === 'pointer' || action.type === 'wheel') { |
| 59 | + for (const sequence of sequenceList) { |
| 60 | + if ( |
| 61 | + (sequence.type === 'pointerMove' || sequence.type === 'scroll') && |
| 62 | + sequence.origin instanceof WebElement |
| 63 | + ) { |
| 64 | + const element = sequence.origin |
| 65 | + const elementId = await element.getId() |
| 66 | + sequence.origin = { |
| 67 | + type: 'element', |
| 68 | + element: { sharedId: elementId }, |
| 69 | + } |
| 70 | + } |
| 71 | + updatedSequenceList.push(sequence) |
| 72 | + } |
| 73 | + |
| 74 | + const updatedAction = { ...action, actions: updatedSequenceList } |
| 75 | + _actions.push(updatedAction) |
| 76 | + } else { |
| 77 | + _actions.push(action) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return _actions |
| 82 | +} |
| 83 | + |
| 84 | +async function getInputInstance(driver) { |
| 85 | + let instance = new Input(driver) |
| 86 | + await instance.init() |
| 87 | + return instance |
| 88 | +} |
| 89 | + |
| 90 | +module.exports = getInputInstance |
0 commit comments