Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/bidi/generated/input_test.js
11822 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
'use strict'
19
20
const assert = require('node:assert')
21
const { suite, ignore, Pages } = require('../../../lib/test')
22
const { Browser } = require('selenium-webdriver')
23
24
const { Input } = require('selenium-webdriver/bidi/generated/input')
25
const { Script } = require('selenium-webdriver/bidi/generated/script')
26
27
suite(
28
function (env) {
29
let driver
30
let input
31
let script
32
33
beforeEach(async function () {
34
driver = await env.builder().build()
35
input = await Input.create(driver)
36
script = await Script.create(driver)
37
})
38
39
afterEach(function () {
40
return driver.quit()
41
})
42
43
describe('performActions keyboard', function () {
44
it('can type into a text field using key actions', async function () {
45
const contextId = await driver.getWindowHandle()
46
await driver.get(Pages.formPage)
47
48
// Find a text input (id="working" is <input type="text"> on formPage)
49
const inputEl = await driver.findElement({ id: 'working' })
50
await inputEl.click()
51
52
// Perform key actions using the generated API
53
await input.performActions({
54
context: contextId,
55
actions: [
56
{
57
type: 'key',
58
id: 'keyboard',
59
actions: [
60
{ type: 'keyDown', value: 'H' },
61
{ type: 'keyUp', value: 'H' },
62
{ type: 'keyDown', value: 'i' },
63
{ type: 'keyUp', value: 'i' },
64
],
65
},
66
],
67
})
68
69
const value = await inputEl.getAttribute('value')
70
assert.strictEqual(value, 'Hi')
71
})
72
73
it('can perform select all and delete', async function () {
74
const contextId = await driver.getWindowHandle()
75
await driver.get(Pages.formPage)
76
77
// id="working" is <input type="text"> on formPage
78
const inputEl = await driver.findElement({ id: 'working' })
79
await inputEl.clear()
80
await inputEl.sendKeys('hello')
81
82
// Select all and delete using BiDi key actions
83
const modifier = process.platform === 'darwin' ? '' : '' // Meta / Control
84
await input.performActions({
85
context: contextId,
86
actions: [
87
{
88
type: 'key',
89
id: 'keyboard',
90
actions: [
91
{ type: 'keyDown', value: modifier },
92
{ type: 'keyDown', value: 'a' },
93
{ type: 'keyUp', value: 'a' },
94
{ type: 'keyUp', value: modifier },
95
{ type: 'keyDown', value: '' }, // Backspace
96
{ type: 'keyUp', value: '' },
97
],
98
},
99
],
100
})
101
102
const value = await inputEl.getAttribute('value')
103
assert.strictEqual(value, '')
104
})
105
})
106
107
describe('performActions pointer mouse', function () {
108
it('can click using mouse pointer actions', async function () {
109
const contextId = await driver.getWindowHandle()
110
await driver.get(Pages.clicksPage)
111
112
const button = await driver.findElement({ id: 'normal' })
113
const rect = await button.getRect()
114
115
await input.performActions({
116
context: contextId,
117
actions: [
118
{
119
type: 'pointer',
120
id: 'mouse',
121
parameters: { pointerType: 'mouse' },
122
actions: [
123
{
124
type: 'pointerMove',
125
x: Math.round(rect.x + rect.width / 2),
126
y: Math.round(rect.y + rect.height / 2),
127
origin: 'viewport',
128
},
129
{ type: 'pointerDown', button: 0, width: 1, height: 1, pressure: 0 },
130
{ type: 'pointerUp', button: 0 },
131
],
132
},
133
],
134
})
135
136
// If click went through, page should show a result
137
const url = await driver.getCurrentUrl()
138
assert.ok(url)
139
})
140
141
it('can perform mouse move', async function () {
142
const contextId = await driver.getWindowHandle()
143
await driver.get(Pages.emptyPage)
144
145
await input.performActions({
146
context: contextId,
147
actions: [
148
{
149
type: 'pointer',
150
id: 'mouse',
151
parameters: { pointerType: 'mouse' },
152
actions: [{ type: 'pointerMove', x: 100, y: 100, origin: 'viewport' }],
153
},
154
],
155
})
156
})
157
})
158
159
ignore(env.browsers(Browser.CHROME)).describe('releaseActions', function () {
160
it('can release actions', async function () {
161
const contextId = await driver.getWindowHandle()
162
await driver.get(Pages.emptyPage)
163
164
await input.performActions({
165
context: contextId,
166
actions: [
167
{
168
type: 'key',
169
id: 'keyboard',
170
actions: [{ type: 'keyDown', value: 'a' }],
171
},
172
],
173
})
174
175
await input.releaseActions({ context: contextId })
176
})
177
})
178
179
ignore(env.browsers(Browser.FIREFOX)).describe('onFileDialogOpened', function () {
180
it('receives event when a file input is clicked', async function () {
181
let dialogEvent = null
182
await input.onFileDialogOpened((params) => {
183
dialogEvent = params
184
assert.ok(params.context, 'event should have a context')
185
})
186
187
const contextId = await driver.getWindowHandle()
188
await driver.get(Pages.uploadPage)
189
190
// userActivation: true is required — Chrome blocks file dialog without a user gesture
191
await script.evaluate({
192
expression: "document.getElementById('upload').click()",
193
target: { context: contextId },
194
awaitPromise: false,
195
userActivation: true,
196
})
197
// if code reaches here means the event did not happen so fail the test
198
await driver.wait(() => dialogEvent !== null, 5000, 'fileDialogOpened event should have fired')
199
})
200
})
201
},
202
{ browsers: [Browser.CHROME, Browser.FIREFOX] },
203
)
204
205