Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/webdriver/common/action_builder_spec.rb
1865 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
require File.expand_path('../spec_helper', __dir__)
21
22
module Selenium
23
module WebDriver
24
describe ActionBuilder do
25
let(:keyboard) { Interactions.key('key') }
26
let(:mouse) { Interactions.pointer(:mouse, name: 'mouse') }
27
let(:bridge) { instance_double(Remote::Bridge).as_null_object }
28
let(:builder) { described_class.new(bridge, devices: [mouse, keyboard]) }
29
let(:async_builder) { described_class.new(bridge, devices: [mouse, keyboard], async: true) }
30
31
describe '#initialize' do
32
it 'does not create input devices when not provided' do
33
action_builder = described_class.new(bridge)
34
expect(action_builder.devices).to be_empty
35
end
36
37
it 'accepts mouse and keyboard with devices keyword' do
38
action_builder = described_class.new(bridge, devices: [mouse, keyboard])
39
expect(action_builder.devices).to eq([mouse, keyboard])
40
end
41
42
it 'accepts multiple devices with devices keyword' do
43
none = Interactions.none('none')
44
touch = Interactions.pointer(:touch, name: 'touch')
45
action_builder = described_class.new(bridge, devices: [mouse, keyboard, none, touch])
46
47
expect(action_builder.devices).to eq([mouse, keyboard, none, touch])
48
end
49
50
it 'accepts duration' do
51
action_builder = described_class.new(bridge, duration: 2200)
52
expect(action_builder.default_move_duration).to eq(2.2)
53
end
54
55
it 'raises a TypeError if a non InputDevice is passed into devices' do
56
expect {
57
described_class.new(bridge, devices: [mouse, keyboard, 'banana'])
58
}.to raise_error(TypeError)
59
end
60
end
61
62
describe '#devices' do
63
it 'returns Array of devices' do
64
expect(builder.devices).to include(a_kind_of(Interactions::KeyInput),
65
a_kind_of(Interactions::PointerInput))
66
end
67
end
68
69
describe '#add_pointer_input' do
70
let(:device) { Interactions.pointer :mouse }
71
72
it 'creates pointer and adds to devices' do
73
device = builder.add_pointer_input(:touch, 'name')
74
75
expect(builder.devices).to include(device)
76
end
77
78
it 'adds pauses to match other devices when sync' do
79
builder.key_down('d', device: 'key')
80
builder.key_up('d', device: 'key')
81
82
allow(Interactions).to receive(:pointer).and_return(device)
83
allow(device).to receive(:name)
84
allow(device).to receive(:create_pause)
85
86
builder.add_pointer_input(:touch, 'name')
87
expect(device).to have_received(:create_pause).twice
88
end
89
90
it 'does not add pauses to match other devices when async' do
91
async_builder.key_down('d', device: 'key')
92
async_builder.key_up('d', device: 'key')
93
94
allow(Interactions).to receive(:pointer).and_return(device)
95
allow(device).to receive(:name)
96
allow(device).to receive(:create_pause)
97
98
async_builder.add_pointer_input(:touch, 'name')
99
expect(device).not_to have_received(:create_pause)
100
end
101
end
102
103
describe '#add_key_input' do
104
let(:device) { Interactions.key }
105
106
it 'creates keyboard and adds to devices' do
107
device = builder.add_key_input('name')
108
109
expect(builder.devices).to include(device)
110
end
111
112
it 'adds pauses to match other devices' do
113
builder.key_down('d', device: 'key')
114
builder.key_up('d', device: 'key')
115
116
allow(Interactions).to receive(:key).and_return(device)
117
allow(device).to receive(:create_pause)
118
119
builder.add_key_input('name')
120
expect(device).to have_received(:create_pause).twice
121
end
122
end
123
124
describe '#device' do
125
it 'gets device by name' do
126
expect(builder.device(name: 'mouse')).to eq(mouse)
127
end
128
129
it 'raises ArgumentError when name not found' do
130
expect { builder.device(name: 'none', type: Interactions::NONE) }.to raise_error(ArgumentError)
131
end
132
133
it 'gets device by type' do
134
expect(builder.device(type: Interactions::POINTER)).to eq(mouse)
135
end
136
137
it 'returns nil when type not found' do
138
expect(builder.device(type: Interactions::NONE)).to be_nil
139
end
140
end
141
142
describe '#pointer_inputs' do
143
it 'returns only pointer inputs' do
144
touch_input = builder.add_pointer_input(:touch, 'touch')
145
pen_input = builder.add_pointer_input(:pen, 'pen')
146
builder.add_key_input('key2')
147
148
expect(builder.pointer_inputs).to eq([mouse, touch_input, pen_input])
149
end
150
end
151
152
describe '#key_inputs' do
153
it 'returns only key inputs' do
154
builder.add_pointer_input(:touch, 'touch')
155
builder.add_pointer_input(:pen, 'pen')
156
key_input = builder.add_key_input('key2')
157
158
expect(builder.key_inputs).to eq([keyboard, key_input])
159
end
160
end
161
162
describe '#pause' do
163
it 'creates pause with default duration' do
164
allow(mouse).to receive :create_pause
165
166
builder.pause(device: mouse)
167
168
expect(mouse).to have_received(:create_pause).with(0)
169
end
170
171
it 'creates pause with provided duration' do
172
allow(mouse).to receive :create_pause
173
174
builder.pause(device: mouse, duration: 5)
175
176
expect(mouse).to have_received(:create_pause).with(5)
177
end
178
end
179
180
describe '#pauses' do
181
it 'adds 2 pauses to a pointer device by default' do
182
allow(mouse).to receive :create_pause
183
184
builder.pauses
185
186
expect(mouse).to have_received(:create_pause).with(0).exactly(2).times
187
end
188
189
it 'adds multiple pause commands' do
190
allow(mouse).to receive :create_pause
191
192
builder.pauses(device: mouse, number: 3)
193
194
expect(mouse).to have_received(:create_pause).with(0).exactly(3).times
195
end
196
end
197
198
describe '#perform' do
199
it 'encodes each device' do
200
allow(mouse).to receive(:encode)
201
allow(keyboard).to receive(:encode)
202
203
builder.perform
204
205
expect(keyboard).to have_received(:encode)
206
expect(mouse).to have_received(:encode)
207
end
208
209
it 'clears all actions' do
210
allow(builder).to receive(:clear_all_actions)
211
212
builder.perform
213
214
expect(builder).to have_received(:clear_all_actions)
215
end
216
217
it 'sends non-nil encoded actions to bridge' do
218
allow(mouse).to receive(:encode).and_return(nil)
219
allow(keyboard).to receive(:encode).and_return(keyboard: 'encoded')
220
allow(bridge).to receive(:send_actions)
221
222
builder.perform
223
expect(bridge).to have_received(:send_actions).with([{keyboard: 'encoded'}])
224
end
225
end
226
227
describe '#clear_all_actions' do
228
it 'sends clear_actions to each devices' do
229
allow(mouse).to receive(:clear_actions)
230
allow(keyboard).to receive(:clear_actions)
231
232
builder.clear_all_actions
233
234
expect(mouse).to have_received(:clear_actions)
235
expect(keyboard).to have_received(:clear_actions)
236
end
237
end
238
239
describe '#release_actions' do
240
it 'sends release actions command to bridge' do
241
allow(bridge).to receive(:release_actions)
242
243
builder.release_actions
244
245
expect(bridge).to have_received(:release_actions)
246
end
247
end
248
249
describe 'tick' do
250
it 'adds pauses to non-active devices when synchronous' do
251
touch = builder.add_pointer_input(:touch, 'touch')
252
allow(mouse).to receive(:create_pause)
253
allow(touch).to receive(:create_pause)
254
allow(keyboard).to receive(:create_pause)
255
256
builder.pointer_down(:left, device: 'mouse')
257
258
expect(mouse).not_to have_received(:create_pause)
259
expect(touch).to have_received(:create_pause)
260
expect(keyboard).to have_received(:create_pause)
261
end
262
263
it 'does not create pauses for any devices when asynchronous' do
264
touch = builder.add_pointer_input(:touch, 'touch')
265
allow(mouse).to receive(:create_pause)
266
allow(touch).to receive(:create_pause)
267
allow(keyboard).to receive(:create_pause)
268
269
async_builder.pointer_down(:left, device: 'mouse')
270
271
expect(mouse).not_to have_received(:create_pause)
272
expect(touch).not_to have_received(:create_pause)
273
expect(keyboard).not_to have_received(:create_pause)
274
end
275
end
276
end # ActionBuilder
277
end # WebDriver
278
end # Selenium
279
280