Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/bidi/input.js
2884 views
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
const { RemoteReferenceType, ReferenceValue } = require('./protocolValue')
22
23
/**
24
* Represents commands and events related to the Input module (simulated user input).
25
* Described in https://w3c.github.io/webdriver-bidi/#module-input.
26
*/
27
class Input {
28
constructor(driver) {
29
this._driver = driver
30
}
31
32
async init() {
33
if (!(await this._driver.getCapabilities()).get('webSocketUrl')) {
34
throw Error('WebDriver instance must support BiDi protocol')
35
}
36
37
this.bidi = await this._driver.getBidi()
38
}
39
40
/**
41
* Performs the specified actions on the given browsing context.
42
*
43
* @param {string} browsingContextId - The ID of the browsing context.
44
* @param {Array} actions - The actions to be performed.
45
* @returns {Promise} A promise that resolves with the response from the server.
46
*/
47
async perform(browsingContextId, actions) {
48
const _actions = await updateActions(actions)
49
50
const command = {
51
method: 'input.performActions',
52
params: {
53
context: browsingContextId,
54
actions: _actions,
55
},
56
}
57
58
return await this.bidi.send(command)
59
}
60
61
/**
62
* Resets the input state in the specified browsing context.
63
*
64
* @param {string} browsingContextId - The ID of the browsing context.
65
* @returns {Promise} A promise that resolves when the release actions are sent.
66
*/
67
async release(browsingContextId) {
68
const command = {
69
method: 'input.releaseActions',
70
params: {
71
context: browsingContextId,
72
},
73
}
74
return await this.bidi.send(command)
75
}
76
77
/**
78
* Sets the files property of a given input element.
79
*
80
* @param {string} browsingContextId - The ID of the browsing context.
81
* @param {string | ReferenceValue} element - The ID of the element or a ReferenceValue object representing the element.
82
* @param {string | string[]} files - The file path or an array of file paths to be set.
83
* @throws {Error} If the element is not a string or a ReferenceValue.
84
* @returns {Promise<void>} A promise that resolves when the files are set.
85
*/
86
async setFiles(browsingContextId, element, files) {
87
if (typeof element !== 'string' && !(element instanceof ReferenceValue)) {
88
throw Error(`Pass in a WebElement id as a string or a ReferenceValue. Received: ${element}`)
89
}
90
91
const command = {
92
method: 'input.setFiles',
93
params: {
94
context: browsingContextId,
95
element:
96
typeof element === 'string'
97
? new ReferenceValue(RemoteReferenceType.SHARED_ID, element).asMap()
98
: element.asMap(),
99
files: typeof files === 'string' ? [files] : files,
100
},
101
}
102
await this.bidi.send(command)
103
}
104
}
105
106
async function updateActions(actions) {
107
const _actions = []
108
for (const action of actions) {
109
const sequenceList = action.actions
110
let updatedSequenceList = []
111
112
if (action.type === 'pointer' || action.type === 'wheel') {
113
for (const sequence of sequenceList) {
114
if ((sequence.type === 'pointerMove' || sequence.type === 'scroll') && sequence.origin instanceof WebElement) {
115
const element = sequence.origin
116
const elementId = await element.getId()
117
sequence.origin = {
118
type: 'element',
119
element: { sharedId: elementId },
120
}
121
}
122
updatedSequenceList.push(sequence)
123
}
124
125
const updatedAction = { ...action, actions: updatedSequenceList }
126
_actions.push(updatedAction)
127
} else {
128
_actions.push(action)
129
}
130
}
131
132
return _actions
133
}
134
135
async function getInputInstance(driver) {
136
let instance = new Input(driver)
137
await instance.init()
138
return instance
139
}
140
141
module.exports = getInputInstance
142
143