|
22 | 22 | module Selenium |
23 | 23 | module WebDriver |
24 | 24 | describe ActionBuilder do |
25 | | - let(:keyboard) do |
26 | | - instance_double(Interactions::KeyInput, |
27 | | - actions: [1, 2, 3], |
28 | | - name: 'keyboard', |
29 | | - type: Interactions::KEY) |
30 | | - end |
31 | | - let(:mouse) do |
32 | | - instance_double(Interactions::PointerInput, |
33 | | - actions: [1, 2, 3], |
34 | | - name: 'mouse', |
35 | | - type: Interactions::POINTER) |
36 | | - end |
| 25 | + let(:keyboard) { Interactions.key('key') } |
| 26 | + let(:mouse) { Interactions.pointer(:mouse, name: 'mouse') } |
37 | 27 | let(:bridge) { instance_double('Bridge').as_null_object } |
38 | 28 | let(:builder) { ActionBuilder.new(bridge, mouse, keyboard) } |
39 | 29 | let(:async_builder) { ActionBuilder.new(bridge, mouse, keyboard, true) } |
40 | 30 |
|
41 | | - context 'when adding an input device' do |
42 | | - let(:pointer_input) { Interactions.pointer(:touch, name: 'touch') } |
| 31 | + describe '#devices' do |
| 32 | + it 'returns Array of devices' do |
| 33 | + expect(builder.devices).to include(a_kind_of(Interactions::KeyInput), |
| 34 | + a_kind_of(Interactions::PointerInput)) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + describe '#add_pointer_input' do |
| 39 | + let(:device) { instance_double Interactions::PointerInput, actions: [] } |
| 40 | + |
| 41 | + it 'creates pointer and adds to devices' do |
| 42 | + builder |
| 43 | + allow(Interactions).to receive(:pointer).and_return(device) |
| 44 | + builder.add_pointer_input(:touch, 'name') |
43 | 45 |
|
44 | | - it 'should be able to add an input device' do |
45 | | - expect { async_builder.send('add_input', pointer_input) }.to change { async_builder.devices.length }.by(1) |
| 46 | + expect(builder.devices).to include(device) |
| 47 | + expect(Interactions).to have_received(:pointer).with(:touch, name: 'name') |
46 | 48 | end |
47 | 49 |
|
48 | | - it 'should add pauses to match the device with the most actions when synchronous' do |
49 | | - expect(builder).to receive(:pauses).with(pointer_input, 3) |
| 50 | + it 'adds pauses to match other devices when sync' do |
| 51 | + builder.key_down('d', device: 'key') |
| 52 | + builder.key_up('d', device: 'key') |
50 | 53 |
|
51 | | - builder.send('add_input', pointer_input) |
| 54 | + allow(Interactions).to receive(:pointer).and_return(device) |
| 55 | + allow(device).to receive(:name) |
| 56 | + allow(device).to receive(:create_pause) |
| 57 | + |
| 58 | + builder.add_pointer_input(:touch, 'name') |
| 59 | + expect(device).to have_received(:create_pause).twice |
52 | 60 | end |
53 | | - end # when adding an input device |
54 | 61 |
|
55 | | - context 'when adding a pointer input' do |
56 | | - it 'should add a PointerInput' do |
57 | | - allow(Interactions::PointerInput).to receive(:new).with(:touch, name: 'touch').and_return(:device) |
58 | | - expect(builder).to receive(:add_input).with(:device) |
| 62 | + it 'does not add pauses to match other devices when async' do |
| 63 | + async_builder.key_down('d', device: 'key') |
| 64 | + async_builder.key_up('d', device: 'key') |
| 65 | + |
| 66 | + allow(Interactions).to receive(:pointer).and_return(device) |
| 67 | + allow(device).to receive(:name) |
| 68 | + allow(device).to receive(:create_pause) |
59 | 69 |
|
60 | | - expect(builder.add_pointer_input(:touch, 'touch')).to eq(:device) |
61 | | - expect(Interactions::PointerInput).to have_received(:new).with(:touch, name: 'touch') |
| 70 | + async_builder.add_pointer_input(:touch, 'name') |
| 71 | + expect(device).not_to have_received(:create_pause) |
62 | 72 | end |
| 73 | + end |
63 | 74 |
|
64 | | - it 'should not assign the pointer input as primary if not primary' do |
65 | | - expect(builder).to receive(:add_input) |
66 | | - expect(builder).not_to receive(:set_primary_pointer) |
| 75 | + describe '#add_key_input' do |
| 76 | + let(:device) { instance_double Interactions::KeyInput, actions: [] } |
67 | 77 |
|
68 | | - builder.add_pointer_input(:touch, 'touch') |
| 78 | + it 'creates keyboard and adds to devices' do |
| 79 | + builder |
| 80 | + allow(Interactions).to receive(:key).and_return(device) |
| 81 | + |
| 82 | + builder.add_key_input('name') |
| 83 | + |
| 84 | + expect(builder.devices).to include(device) |
| 85 | + expect(Interactions).to have_received(:key).with('name') |
69 | 86 | end |
70 | | - end # when adding a pointer input |
71 | 87 |
|
72 | | - it 'should add a key input' do |
73 | | - allow(Interactions::KeyInput).to receive(:new).with('keyboard').and_return(:device) |
74 | | - expect(builder).to receive(:add_input).with(:device) |
| 88 | + it 'adds pauses to match other devices' do |
| 89 | + builder.key_down('d', device: 'key') |
| 90 | + builder.key_up('d', device: 'key') |
75 | 91 |
|
76 | | - expect(builder.add_key_input('keyboard')).to eq(:device) |
77 | | - expect(Interactions::KeyInput).to have_received(:new).with('keyboard') |
| 92 | + allow(Interactions).to receive(:key).and_return(device) |
| 93 | + allow(device).to receive(:create_pause) |
| 94 | + |
| 95 | + builder.add_key_input('name') |
| 96 | + expect(device).to have_received(:create_pause).twice |
| 97 | + end |
78 | 98 | end |
79 | 99 |
|
80 | | - it 'should get a device by name' do |
81 | | - expect(builder.get_device('mouse')).to eq(mouse) |
| 100 | + describe '#get_device' do |
| 101 | + it 'gets device by name' do |
| 102 | + expect(builder.get_device('mouse')).to eq(mouse) |
| 103 | + expect(builder.get_device('key')).to eq(keyboard) |
| 104 | + end |
82 | 105 | end |
83 | 106 |
|
84 | | - it 'should return only pointer inputs' do |
85 | | - expect(builder.pointer_inputs).to eq([mouse]) |
| 107 | + describe '#pointer_inputs' do |
| 108 | + it 'returns only pointer inputs' do |
| 109 | + touch_input = builder.add_pointer_input(:touch, 'touch') |
| 110 | + pen_input = builder.add_pointer_input(:pen, 'pen') |
| 111 | + builder.add_key_input('key2') |
| 112 | + |
| 113 | + expect(builder.pointer_inputs).to eq([mouse, touch_input, pen_input]) |
| 114 | + end |
86 | 115 | end |
87 | 116 |
|
88 | | - it 'should return the key inputs' do |
89 | | - expect(builder.key_inputs).to eq([keyboard]) |
| 117 | + describe '#key_inputs' do |
| 118 | + it 'returns only key inputs' do |
| 119 | + builder.add_pointer_input(:touch, 'touch') |
| 120 | + builder.add_pointer_input(:pen, 'pen') |
| 121 | + key_input = builder.add_key_input('key2') |
| 122 | + |
| 123 | + expect(builder.key_inputs).to eq([keyboard, key_input]) |
| 124 | + end |
90 | 125 | end |
91 | 126 |
|
92 | | - it 'should create a pause for the given device' do |
93 | | - duration = 5 |
94 | | - expect(mouse).to receive(:create_pause).with(duration) |
| 127 | + describe '#pause' do |
| 128 | + it 'creates pause with default duration' do |
| 129 | + allow(mouse).to receive :create_pause |
| 130 | + |
| 131 | + builder.pause(mouse) |
| 132 | + |
| 133 | + expect(mouse).to have_received(:create_pause).with(nil) |
| 134 | + end |
| 135 | + |
| 136 | + it 'creates pause with provided duration' do |
| 137 | + allow(mouse).to receive :create_pause |
| 138 | + |
| 139 | + builder.pause(mouse, 5) |
95 | 140 |
|
96 | | - builder.pause(mouse, 5) |
| 141 | + expect(mouse).to have_received(:create_pause).with(5) |
| 142 | + end |
97 | 143 | end |
98 | 144 |
|
99 | | - it 'should create multiple pauses for the given device' do |
100 | | - duration = 5 |
101 | | - number = 3 |
102 | | - expect(mouse).to receive(:create_pause).with(duration).exactly(number).times |
| 145 | + describe '#pauses' do |
| 146 | + it 'adds multiple pause commands' do |
| 147 | + allow(mouse).to receive :create_pause |
103 | 148 |
|
104 | | - builder.pauses(mouse, number, duration) |
| 149 | + builder.pauses(mouse, 3) |
| 150 | + |
| 151 | + expect(mouse).to have_received(:create_pause).with(nil).exactly(3).times |
| 152 | + end |
105 | 153 | end |
106 | 154 |
|
107 | | - context 'when performing actions' do |
108 | | - it 'should encode each device' do |
109 | | - expect(mouse).to receive(:encode) |
110 | | - expect(keyboard).to receive(:encode) |
111 | | - allow(builder).to receive(:clear_all_actions) |
| 155 | + describe '#perform' do |
| 156 | + it 'encodes each device' do |
| 157 | + allow(mouse).to receive(:encode) |
| 158 | + allow(keyboard).to receive(:encode) |
112 | 159 |
|
113 | 160 | builder.perform |
| 161 | + |
| 162 | + expect(keyboard).to have_received(:encode) |
| 163 | + expect(mouse).to have_received(:encode) |
114 | 164 | end |
115 | 165 |
|
116 | | - it 'should call bridge#send_actions with encoded and compacted devices' do |
117 | | - allow(mouse).to receive(:encode).and_return(nil) |
118 | | - allow(keyboard).to receive(:encode).and_return('not_nil') |
119 | | - expect(bridge).to receive(:send_actions).with(['not_nil']) |
| 166 | + it 'clears all actions' do |
120 | 167 | allow(builder).to receive(:clear_all_actions) |
121 | 168 |
|
122 | 169 | builder.perform |
| 170 | + |
| 171 | + expect(builder).to have_received(:clear_all_actions) |
123 | 172 | end |
124 | 173 |
|
125 | | - it 'should clear all actions' do |
126 | | - allow(mouse).to receive(:encode) |
127 | | - allow(keyboard).to receive(:encode) |
128 | | - expect(builder).to receive(:clear_all_actions) |
| 174 | + it 'sends non-nil encoded actions to bridge' do |
| 175 | + allow(mouse).to receive(:encode).and_return(nil) |
| 176 | + allow(keyboard).to receive(:encode).and_return(keyboard: 'encoded') |
| 177 | + allow(bridge).to receive(:send_actions) |
129 | 178 |
|
130 | 179 | builder.perform |
| 180 | + expect(bridge).to have_received(:send_actions).with([{keyboard: 'encoded'}]) |
131 | 181 | end |
132 | | - end # when performing actions |
| 182 | + end |
133 | 183 |
|
134 | | - it 'should clear all actions from devices' do |
135 | | - expect(mouse).to receive(:clear_actions) |
136 | | - expect(keyboard).to receive(:clear_actions) |
| 184 | + describe '#clear_all_actions' do |
| 185 | + it 'sends clear_actions to each devices' do |
| 186 | + allow(mouse).to receive(:clear_actions) |
| 187 | + allow(keyboard).to receive(:clear_actions) |
137 | 188 |
|
138 | | - builder.clear_all_actions |
| 189 | + builder.clear_all_actions |
| 190 | + |
| 191 | + expect(mouse).to have_received(:clear_actions) |
| 192 | + expect(keyboard).to have_received(:clear_actions) |
| 193 | + end |
139 | 194 | end |
140 | 195 |
|
141 | | - it 'should release actions' do |
142 | | - expect(bridge).to receive(:release_actions) |
| 196 | + describe '#release_actions' do |
| 197 | + it 'sends release actions command to bridge' do |
| 198 | + allow(bridge).to receive(:release_actions) |
| 199 | + |
| 200 | + builder.release_actions |
143 | 201 |
|
144 | | - builder.release_actions |
| 202 | + expect(bridge).to have_received(:release_actions) |
| 203 | + end |
145 | 204 | end |
146 | 205 |
|
147 | | - context 'when adding a tick' do |
148 | | - it 'should not create pauses for any devices when asynchronous' do |
149 | | - expect(mouse).not_to receive(:create_pause) |
150 | | - expect(keyboard).not_to receive(:create_pause) |
| 206 | + describe 'tick' do |
| 207 | + it 'adds pauses to non-active devices when synchronous' do |
| 208 | + touch = builder.add_pointer_input(:touch, 'touch') |
| 209 | + allow(mouse).to receive(:create_pause) |
| 210 | + allow(touch).to receive(:create_pause) |
| 211 | + allow(keyboard).to receive(:create_pause) |
| 212 | + |
| 213 | + builder.pointer_down(:left, device: 'mouse') |
151 | 214 |
|
152 | | - async_builder.send('tick', mouse) |
| 215 | + expect(mouse).not_to have_received(:create_pause) |
| 216 | + expect(touch).to have_received(:create_pause) |
| 217 | + expect(keyboard).to have_received(:create_pause) |
153 | 218 | end |
154 | 219 |
|
155 | | - it 'should create pauses for devices not passed when synchronous' do |
| 220 | + it 'does not create pauses for any devices when asynchronous' do |
156 | 221 | touch = builder.add_pointer_input(:touch, 'touch') |
157 | | - expect(touch).to receive(:create_pause) |
158 | | - expect(keyboard).not_to receive(:create_pause) |
159 | | - expect(mouse).not_to receive(:create_pause) |
| 222 | + allow(mouse).to receive(:create_pause) |
| 223 | + allow(touch).to receive(:create_pause) |
| 224 | + allow(keyboard).to receive(:create_pause) |
160 | 225 |
|
161 | | - builder.send('tick', mouse, keyboard) |
| 226 | + async_builder.pointer_down(:left, device: 'mouse') |
| 227 | + |
| 228 | + expect(mouse).not_to have_received(:create_pause) |
| 229 | + expect(touch).not_to have_received(:create_pause) |
| 230 | + expect(keyboard).not_to have_received(:create_pause) |
162 | 231 | end |
163 | | - end # when adding a tick |
| 232 | + end |
164 | 233 | end # ActionBuilder |
165 | 234 | end # WebDriver |
166 | 235 | end # Selenium |
0 commit comments