Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/actions_test.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
'use strict'
19
20
const assert = require('node:assert')
21
const fileServer = require('../lib/test/fileserver')
22
const { ignore, Pages, suite } = require('../lib/test')
23
const { Key, Origin } = require('selenium-webdriver/lib/input')
24
const { Browser, By, until } = require('selenium-webdriver')
25
26
suite(function (env) {
27
describe('WebDriver.actions()', function () {
28
let driver
29
30
beforeEach(async function () {
31
driver = await env.builder().build()
32
})
33
34
afterEach(function () {
35
return driver.quit()
36
})
37
38
it('click(element)', async function () {
39
await driver.get(fileServer.whereIs('/data/actions/click.html'))
40
41
let box = await driver.findElement(By.id('box'))
42
assert.strictEqual(await box.getAttribute('class'), '')
43
44
await driver.actions().click(box).perform()
45
await driver.wait(async () => {
46
assert.strictEqual(await box.getAttribute('class'), 'green')
47
return true
48
}, 10000)
49
})
50
51
it('click(element) clicks in center of element', async function () {
52
await driver.get(fileServer.whereIs('/data/actions/record_click.html'))
53
54
const div = await driver.findElement(By.css('div'))
55
const rect = await div.getRect()
56
assert.deepStrictEqual(rect, { width: 500, height: 500, x: 0, y: 0 })
57
58
await driver.actions().click(div).perform()
59
60
await driver.wait(
61
async () => {
62
const clicks = await driver.executeScript('return clicks')
63
return clicks.length > 0
64
},
65
10000,
66
'No clicks returned',
67
)
68
const clicks = await driver.executeScript('return clicks')
69
assert.deepStrictEqual(clicks, [[250, 250]])
70
})
71
72
it('can move relative to element center', async function () {
73
await driver.get(fileServer.whereIs('/data/actions/record_click.html'))
74
75
const div = await driver.findElement(By.css('div'))
76
const rect = await div.getRect()
77
assert.deepStrictEqual(rect, { width: 500, height: 500, x: 0, y: 0 })
78
79
await driver.actions().move({ x: 10, y: 10, origin: div }).click().perform()
80
81
await driver.wait(
82
async () => {
83
const clicks = await driver.executeScript('return clicks')
84
return clicks.length > 0
85
},
86
10000,
87
'No clicks returned',
88
)
89
const clicks = await driver.executeScript('return clicks')
90
assert.deepStrictEqual(clicks, [[260, 260]])
91
})
92
93
ignore(env.browsers(Browser.SAFARI)).it('doubleClick(element)', async function () {
94
await driver.get(fileServer.whereIs('/data/actions/click.html'))
95
96
let box = await driver.findElement(By.id('box'))
97
assert.strictEqual(await box.getAttribute('class'), '')
98
99
await driver.actions().doubleClick(box).perform()
100
await driver.wait(async () => (await box.getAttribute('class')) === 'blue', 10000)
101
assert.strictEqual(await box.getAttribute('class'), 'blue')
102
})
103
104
it('dragAndDrop()', async function () {
105
await driver.get(fileServer.whereIs('/data/actions/drag.html'))
106
107
let slide = await driver.findElement(By.id('slide'))
108
assert.strictEqual(await slide.getCssValue('left'), '0px')
109
assert.strictEqual(await slide.getCssValue('top'), '0px')
110
111
let br = await driver.findElement(By.id('BR'))
112
await driver.actions().dragAndDrop(slide, br).perform()
113
assert.strictEqual(await slide.getCssValue('left'), '206px')
114
assert.strictEqual(await slide.getCssValue('top'), '206px')
115
116
let tr = await driver.findElement(By.id('TR'))
117
await driver.actions().dragAndDrop(slide, tr).perform()
118
assert.strictEqual(await slide.getCssValue('left'), '206px')
119
assert.strictEqual(await slide.getCssValue('top'), '1px')
120
})
121
122
it('move()', async function () {
123
await driver.get(fileServer.whereIs('/data/actions/drag.html'))
124
125
let slide = await driver.findElement(By.id('slide'))
126
assert.strictEqual(await slide.getCssValue('left'), '0px')
127
assert.strictEqual(await slide.getCssValue('top'), '0px')
128
129
await driver
130
.actions()
131
.move({ origin: slide })
132
.press()
133
.move({ x: 100, y: 100, origin: Origin.POINTER })
134
.release()
135
.perform()
136
137
await driver.wait(async () => (await slide.getCssValue('left')) === '101px', 10000)
138
assert.strictEqual(await slide.getCssValue('left'), '101px')
139
assert.strictEqual(await slide.getCssValue('left'), '101px')
140
})
141
142
it('can move to and click element in an iframe', async function () {
143
await driver.get(fileServer.whereIs('click_tests/click_in_iframe.html'))
144
145
await driver.wait(until.elementLocated(By.id('ifr')), 5000).then((frame) => driver.switchTo().frame(frame))
146
147
let link = await driver.findElement(By.id('link'))
148
149
await driver.actions().click(link).perform()
150
await driver.switchTo().defaultContent()
151
return driver.wait(until.titleIs('Submitted Successfully!'), 10000)
152
})
153
154
it('can send keys to focused element', async function () {
155
await driver.get(Pages.formPage)
156
157
let el = await driver.findElement(By.id('email'))
158
assert.strictEqual(await el.getAttribute('value'), '')
159
160
await driver.executeScript('arguments[0].focus()', el)
161
162
await driver.actions().sendKeys('foobar').perform()
163
164
await driver.wait(async () => (await el.getAttribute('value')) === 'foobar', 10000)
165
assert.strictEqual(await el.getAttribute('value'), 'foobar')
166
})
167
168
it('can get the property of element', async function () {
169
await driver.get(Pages.formPage)
170
171
let el = await driver.findElement(By.id('email'))
172
assert.strictEqual(await el.getProperty('value'), '')
173
174
await driver.executeScript('arguments[0].focus()', el)
175
176
await driver.actions().sendKeys('foobar').perform()
177
178
await driver.wait(async () => (await el.getProperty('value')) === 'foobar', 10000)
179
assert.strictEqual(await el.getProperty('value'), 'foobar')
180
})
181
182
it('can send keys to focused element (with modifiers)', async function () {
183
await driver.get(Pages.formPage)
184
185
let el = await driver.findElement(By.id('email'))
186
assert.strictEqual(await el.getAttribute('value'), '')
187
188
await driver.executeScript('arguments[0].focus()', el)
189
190
await driver.actions().sendKeys('fo').keyDown(Key.SHIFT).sendKeys('OB').keyUp(Key.SHIFT).sendKeys('ar').perform()
191
192
await driver.wait(async () => (await el.getAttribute('value')) === 'foOBar', 10000)
193
assert.strictEqual(await el.getAttribute('value'), 'foOBar')
194
})
195
196
it('can interact with simple form elements', async function () {
197
await driver.get(Pages.formPage)
198
199
let el = await driver.findElement(By.id('email'))
200
assert.strictEqual(await el.getAttribute('value'), '')
201
202
await driver.actions().click(el).sendKeys('foobar').perform()
203
204
await driver.wait(async () => (await el.getAttribute('value')) === 'foobar', 10000)
205
assert.strictEqual(await el.getAttribute('value'), 'foobar')
206
})
207
208
it('can send keys to designated element', async function () {
209
await driver.get(Pages.formPage)
210
211
let el = await driver.findElement(By.id('email'))
212
assert.strictEqual(await el.getAttribute('value'), '')
213
214
await driver.actions().sendKeys(el, 'foobar').perform()
215
216
await driver.wait(async () => (await el.getAttribute('value')) === 'foobar', 10000)
217
assert.strictEqual(await el.getAttribute('value'), 'foobar')
218
})
219
220
ignore(env.browsers(Browser.FIREFOX, Browser.SAFARI)).it('can scroll with the wheel input', async function () {
221
await driver.get(Pages.scrollingPage)
222
let scrollable = await driver.findElement(By.id('scrollable'))
223
224
await driver.actions().scroll(0, 0, 5, 10, scrollable).perform()
225
let events = await _getEvents(driver)
226
assert.strictEqual(events[0].type, 'wheel')
227
assert.ok(events[0].deltaX >= 5)
228
assert.ok(events[0].deltaY >= 10)
229
assert.strictEqual(events[0].deltaZ, 0)
230
assert.strictEqual(events[0].target, 'scrollContent')
231
})
232
233
async function _getEvents(driver) {
234
await driver.wait(async () => {
235
const events = await driver.executeScript('return allEvents.events;')
236
return events.length > 0
237
}, 5000)
238
return (await driver.executeScript('return allEvents.events;')) || []
239
}
240
})
241
})
242
243