Skip to content

Commit 4915e2b

Browse files
committed
[rb] only create input devices if needed
1 parent 7c78f9e commit 4915e2b

15 files changed

Lines changed: 82 additions & 105 deletions

rb/lib/selenium/webdriver/common/action_builder.rb

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ def initialize(bridge, deprecated_mouse = nil, deprecated_keyboard = nil, deprec
6161
id: :action_devices
6262
add_input(deprecated_mouse)
6363
add_input(deprecated_keyboard)
64-
elsif devices.empty?
65-
add_pointer_input(:mouse, 'mouse')
66-
add_key_input('keyboard')
6764
else
6865
devices.each { |device| add_input(device) }
6966
end
@@ -84,9 +81,7 @@ def initialize(bridge, deprecated_mouse = nil, deprecated_keyboard = nil, deprec
8481
#
8582

8683
def add_pointer_input(kind, name)
87-
new_input = Interactions.pointer(kind, name: name)
88-
add_input(new_input)
89-
new_input
84+
add_input(Interactions.pointer(kind, name: name))
9085
end
9186

9287
#
@@ -102,9 +97,7 @@ def add_pointer_input(kind, name)
10297
#
10398

10499
def add_key_input(name)
105-
new_input = Interactions.key(name)
106-
add_input(new_input)
107-
new_input
100+
add_input(Interactions.key(name))
108101
end
109102

110103
#
@@ -115,7 +108,26 @@ def add_key_input(name)
115108
#
116109

117110
def get_device(name)
118-
@devices.find { |device| device.name == name.to_s } || raise(ArgumentError, "Can not find device: #{name}")
111+
WebDriver.logger.deprecate('#get_device with name parameter',
112+
'#device with :name or :type keyword',
113+
id: :get_device)
114+
device(name: name)
115+
end
116+
117+
#
118+
# Retrieves the input device for the given name or type
119+
#
120+
# @param [String] name name of the input device
121+
# @param [String] type name of the input device
122+
# @return [Selenium::WebDriver::Interactions::InputDevice] input device with given name or type
123+
#
124+
125+
def device(name: nil, type: nil)
126+
input = @devices.find { |device| (device.name == name.to_s || name.nil?) && (device.type == type || type.nil?) }
127+
128+
raise(ArgumentError, "Can not find device: #{name}") if name && input.nil?
129+
130+
input
119131
end
120132

121133
#
@@ -232,9 +244,8 @@ def add_input(device)
232244
pauses(device, max_device.actions.length) if max_device
233245
end
234246
@devices << device
247+
device
235248
end
236-
end
237-
238-
# ActionBuilder
249+
end # ActionBuilder
239250
end # WebDriver
240251
end # Selenium

rb/lib/selenium/webdriver/common/interactions/input_device.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Interactions
3030
#
3131

3232
class InputDevice
33-
attr_reader :name, :actions
33+
attr_reader :name, :actions, :type
3434

3535
def initialize(name = nil)
3636
@name = name || SecureRandom.uuid
@@ -51,13 +51,8 @@ def create_pause(duration = 0)
5151
add_action(Pause.new(self, duration))
5252
end
5353

54-
def no_actions? # Determine if only pauses are present
55-
actions = @actions.reject { |action| action.type == :pause }
56-
actions.empty?
57-
end
58-
5954
def encode
60-
{type: type, id: name, actions: @actions.map(&:encode)} unless no_actions?
55+
{type: type, id: name, actions: @actions.map(&:encode)} unless @actions.empty?
6156
end
6257
end # InputDevice
6358
end # Interactions

rb/lib/selenium/webdriver/common/interactions/key_actions.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ def key_action(*args, action: nil, device: nil)
141141
self
142142
end
143143

144-
def key_input(device = nil)
145-
device ? get_device(device) : key_inputs.first
144+
def key_input(name = nil)
145+
device(name: name, type: Interactions::KEY) || add_key_input('keyboard')
146146
end
147-
148147
end # KeyActions
149148
end # WebDriver
150149
end # Selenium

rb/lib/selenium/webdriver/common/interactions/key_input.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ module Interactions
2929
class KeyInput < InputDevice
3030
SUBTYPES = {down: :keyDown, up: :keyUp, pause: :pause}.freeze
3131

32-
def type
33-
Interactions::KEY
32+
def initialize(name = nil)
33+
super
34+
@type = Interactions::KEY
3435
end
3536

3637
def create_key_down(key)

rb/lib/selenium/webdriver/common/interactions/none_input.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ module Interactions
2828
#
2929

3030
class NoneInput < InputDevice
31-
def type
32-
Interactions::NONE
31+
def initialize(name = nil)
32+
super
33+
@type = Interactions::NONE
3334
end
3435
end # NoneInput
3536
end # Interactions

rb/lib/selenium/webdriver/common/interactions/pointer_actions.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ def button_action(button, action: nil, device: nil)
355355
self
356356
end
357357

358-
def pointer_input(device = nil)
359-
device ? get_device(device) : pointer_inputs.first
358+
def pointer_input(name = nil)
359+
device(name: name, type: Interactions::POINTER) || add_pointer_input(:mouse, 'mouse')
360360
end
361361
end # PointerActions
362362
end # WebDriver

rb/lib/selenium/webdriver/common/interactions/pointer_input.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ class PointerInput < InputDevice
3434
def initialize(kind, name: nil)
3535
super(name)
3636
@kind = assert_kind(kind)
37-
end
38-
39-
def type
40-
Interactions::POINTER
37+
@type = Interactions::POINTER
4138
end
4239

4340
def encode

rb/spec/unit/selenium/webdriver/common/action_builder_spec.rb

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ module WebDriver
2929
let(:async_builder) { ActionBuilder.new(bridge, devices: [mouse, keyboard], async: true) }
3030

3131
describe '#initialize' do
32-
it 'creates default mouse and keyboard when none are provided' do
32+
it 'does not create input devices when not provided' do
3333
action_builder = ActionBuilder.new(bridge)
34-
expect(action_builder.devices).to include(a_kind_of(Interactions::KeyInput),
35-
a_kind_of(Interactions::PointerInput))
34+
expect(action_builder.devices).to be_empty
3635
end
3736

3837
it 'deprecates using mouse and keyboard directly' do
@@ -44,9 +43,7 @@ module WebDriver
4443

4544
it 'deprecates using async parameter' do
4645
expect {
47-
action_builder = ActionBuilder.new(bridge, nil, nil, true)
48-
expect(action_builder.devices).to include(a_kind_of(Interactions::PointerInput))
49-
expect(action_builder.devices).to include(a_kind_of(Interactions::KeyInput))
46+
ActionBuilder.new(bridge, nil, nil, true)
5047
}.to have_deprecated(:action_async)
5148
end
5249

@@ -90,12 +87,9 @@ module WebDriver
9087
let(:device) { Interactions.pointer :mouse }
9188

9289
it 'creates pointer and adds to devices' do
93-
builder
94-
allow(Interactions).to receive(:pointer).and_return(device)
95-
builder.add_pointer_input(:touch, 'name')
90+
device = builder.add_pointer_input(:touch, 'name')
9691

9792
expect(builder.devices).to include(device)
98-
expect(Interactions).to have_received(:pointer).with(:touch, name: 'name')
9993
end
10094

10195
it 'adds pauses to match other devices when sync' do
@@ -127,13 +121,9 @@ module WebDriver
127121
let(:device) { Interactions.key }
128122

129123
it 'creates keyboard and adds to devices' do
130-
builder
131-
allow(Interactions).to receive(:key).and_return(device)
132-
133-
builder.add_key_input('name')
124+
device = builder.add_key_input('name')
134125

135126
expect(builder.devices).to include(device)
136-
expect(Interactions).to have_received(:key).with('name')
137127
end
138128

139129
it 'adds pauses to match other devices' do
@@ -148,10 +138,29 @@ module WebDriver
148138
end
149139
end
150140

151-
describe '#get_device' do
141+
describe '#device' do
152142
it 'gets device by name' do
153-
expect(builder.get_device('mouse')).to eq(mouse)
154-
expect(builder.get_device('key')).to eq(keyboard)
143+
expect(builder.device(name: 'mouse')).to eq(mouse)
144+
end
145+
146+
it 'raises ArgumentError when name not found' do
147+
expect { builder.device(name: 'none', type: Interactions::NONE) }.to raise_error(ArgumentError)
148+
end
149+
150+
it 'gets device by type' do
151+
expect(builder.device(type: Interactions::POINTER)).to eq(mouse)
152+
end
153+
154+
it 'returns nil when type not found' do
155+
expect(builder.device(type: Interactions::NONE)).to be_nil
156+
end
157+
end
158+
159+
describe '#get_device' do
160+
it 'gets device by name with deprecation' do
161+
expect {
162+
expect(builder.get_device('mouse')).to eq(mouse)
163+
}.to have_deprecated(:get_device)
155164
end
156165
end
157166

rb/spec/unit/selenium/webdriver/common/interactions/input_device_spec.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,6 @@ def type
8181
expect(device).to have_received(:add_action).with(action)
8282
end
8383
end
84-
85-
describe '#no_actions?' do
86-
let(:typing) { instance_double(TypingInteraction, type: :not_pause) }
87-
88-
it 'returns true when all actions are pauses' do
89-
allow(device).to receive(:type).and_return(:none)
90-
2.times { device.create_pause }
91-
expect(device.no_actions?).to be true
92-
end
93-
94-
it 'returns false when not all actions are pauses' do
95-
allow(device).to receive(:type).and_return(:none)
96-
allow(typing).to receive(:class).and_return(TypingInteraction)
97-
device.create_pause
98-
device.add_action(typing)
99-
expect(device.no_actions?).to be false
100-
end
101-
end
10284
end
10385
end # Interactions
10486
end # WebDriver

rb/spec/unit/selenium/webdriver/common/interactions/interaction_spec.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ module Selenium
2323
module WebDriver
2424
module Interactions
2525
class NewDevice < InputDevice
26-
def type
27-
:special
26+
def initialize(name = nil)
27+
super
28+
@type = :special
2829
end
2930

3031
def create_special(val)
@@ -54,22 +55,18 @@ def special_action(special, device: nil)
5455
self
5556
end
5657

57-
def special_inputs
58-
@devices.select { |device| device.is_a? NewDevice }
59-
end
60-
6158
private
6259

63-
def special_input(device = nil)
64-
device ? get_device(device) : special_inputs.first
60+
def special_input(name = nil)
61+
device(name: name, type: :newType) || add_input(NewDevice.new('new'))
6562
end
6663
end
6764

6865
describe Interaction do
6966
it 'can create subclass' do
7067
bridge = instance_double(Remote::Bridge)
7168
allow(bridge).to receive(:send_actions)
72-
sub_action_builder = SubActionBuilder.new(bridge, devices: [NewDevice.new('new')])
69+
sub_action_builder = SubActionBuilder.new(bridge)
7370
sub_action_builder.special_action('special').perform
7471

7572
expect(bridge).to have_received(:send_actions).with([{type: :special,

0 commit comments

Comments
 (0)