/
driver_spec.rb
368 lines (297 loc) · 14.3 KB
/
driver_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# frozen_string_literal: true
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
require_relative 'spec_helper'
module Selenium
module WebDriver
describe Driver do
it_behaves_like 'driver that can be started concurrently', exclude: {browser: %i[safari safari_preview]}
it 'creates default capabilities', exclude: {browser: %i[safari safari_preview]} do
reset_driver! do |driver|
caps = driver.capabilities
expect(caps.proxy).to be_nil
expect(caps.browser_version).to match(/^\d\d\d?\./)
expect(caps.platform_name).not_to be_nil
expect(caps.accept_insecure_certs).to eq(caps.browser_name == 'firefox')
expect(caps.page_load_strategy).to eq 'normal'
expect(caps.implicit_timeout).to be_zero
expect(caps.page_load_timeout).to eq 300000
expect(caps.script_timeout).to eq 30000
end
end
it 'gets driver status' do
status = driver.status
expect(status).to include('ready', 'message')
end
it 'gets the page title' do
driver.navigate.to url_for('xhtmlTest.html')
expect(driver.title).to eq('XHTML Test Page')
end
it 'gets the page source' do
driver.navigate.to url_for('xhtmlTest.html')
expect(driver.page_source).to match(%r{<title>XHTML Test Page</title>}i)
end
it 'refreshes the page' do
driver.navigate.to url_for('javascriptPage.html')
sleep 1 # javascript takes too long to load
driver.find_element(id: 'updatediv').click
expect(driver.find_element(id: 'dynamo').text).to eq('Fish and chips!')
driver.navigate.refresh
wait_for_element(id: 'dynamo')
expect(driver.find_element(id: 'dynamo').text).to eq("What's for dinner?")
end
describe 'one element' do
it 'finds by id' do
driver.navigate.to url_for('xhtmlTest.html')
element = driver.find_element(id: 'id1')
expect(element).to be_a(WebDriver::Element)
expect(element.text).to eq('Foo')
end
it 'finds by field name' do
driver.navigate.to url_for('formPage.html')
expect(driver.find_element(name: 'x').attribute('value')).to eq('name')
end
it 'finds by class name' do # rubocop:disable RSpec/RepeatedExample
driver.navigate.to url_for('xhtmlTest.html')
expect(driver.find_element(class: 'header').text).to eq('XHTML Might Be The Future')
end
# TODO: Rewrite this test so it's not a duplicate of above or remove
it 'finds elements with a hash selector' do # rubocop:disable RSpec/RepeatedExample
driver.navigate.to url_for('xhtmlTest.html')
expect(driver.find_element(class: 'header').text).to eq('XHTML Might Be The Future')
end
it 'finds by link text' do
driver.navigate.to url_for('xhtmlTest.html')
expect(driver.find_element(link: 'Foo').text).to eq('Foo')
end
it 'finds by xpath' do
driver.navigate.to url_for('xhtmlTest.html')
expect(driver.find_element(xpath: '//h1').text).to eq('XHTML Might Be The Future')
end
it 'finds by css selector' do
driver.navigate.to url_for('xhtmlTest.html')
expect(driver.find_element(css: 'div.content').attribute('class')).to eq('content')
end
it 'finds by tag name' do
driver.navigate.to url_for('xhtmlTest.html')
expect(driver.find_element(tag_name: 'div').attribute('class')).to eq('navigation')
end
it 'finds above another' do
driver.navigate.to url_for('relative_locators.html')
above = driver.find_element(relative: {tag_name: 'td', above: {id: 'center'}})
expect(above.attribute('id')).to eq('second')
end
it 'finds child element' do
driver.navigate.to url_for('nestedElements.html')
element = driver.find_element(name: 'form2')
child = element.find_element(name: 'selectomatic')
expect(child.attribute('id')).to eq('2')
end
it 'finds child element by tag name' do
driver.navigate.to url_for('nestedElements.html')
element = driver.find_element(name: 'form2')
child = element.find_element(tag_name: 'select')
expect(child.attribute('id')).to eq('2')
end
it 'finds elements with the shortcut syntax' do
driver.navigate.to url_for('xhtmlTest.html')
expect(driver[:id1]).to be_a(WebDriver::Element)
expect(driver[xpath: '//h1']).to be_a(WebDriver::Element)
end
it 'raises if element not found' do
driver.navigate.to url_for('xhtmlTest.html')
expect {
driver.find_element(id: 'not-there')
}.to raise_error(Error::NoSuchElementError, /errors#no-such-element-exception/)
end
it 'raises if invalid locator',
except: {browser: %i[chrome edge],
reason: 'https://bugs.chromium.org/p/chromedriver/issues/detail?id=4743'},
exclude: {browser: %i[safari safari_preview], reason: 'Safari TimeoutError'} do
driver.navigate.to url_for('xhtmlTest.html')
expect {
driver.find_element(xpath: '*?//-')
}.to raise_error(Error::InvalidSelectorError, /errors#invalid-selector-exception/)
end
end
describe 'many elements' do
it 'finds by class name' do
driver.navigate.to url_for('xhtmlTest.html')
expect(driver.find_elements(class: 'nameC').size).to eq(2)
end
it 'finds by css selector' do
driver.navigate.to url_for('xhtmlTest.html')
expect(driver.find_elements(css: 'p').size).to be_positive
end
it 'finds above element' do
driver.navigate.to url_for('relative_locators.html')
lowest = driver.find_element(id: 'below')
above = driver.find_elements(relative: {tag_name: 'p', above: lowest})
expect(above.map { |e| e.attribute('id') }).to eq(%w[mid above])
end
it 'finds above another' do
driver.navigate.to url_for('relative_locators.html')
above = driver.find_elements(relative: {css: 'td', above: {id: 'center'}})
expect(above.map { |e| e.attribute('id') }).to eq(%w[second first third])
end
it 'finds below element' do
driver.navigate.to url_for('relative_locators.html')
midpoint = driver.find_element(id: 'mid')
above = driver.find_elements(relative: {id: 'below', below: midpoint})
expect(above.map { |e| e.attribute('id') }).to eq(['below'])
end
it 'finds near another within default distance' do
driver.navigate.to url_for('relative_locators.html')
near = driver.find_elements(relative: {tag_name: 'td', near: {id: 'sixth'}})
expect(near.map { |e| e.attribute('id') }).to eq(%w[third ninth center second eighth])
end
it 'finds near another within custom distance', except: {browser: %i[safari safari_preview]} do
driver.navigate.to url_for('relative_locators.html')
near = driver.find_elements(relative: {tag_name: 'td', near: {id: 'sixth', distance: 100}})
expect(near.map { |e| e.attribute('id') }).to eq(%w[third ninth center second eighth])
end
it 'finds to the left of another' do
driver.navigate.to url_for('relative_locators.html')
left = driver.find_elements(relative: {tag_name: 'td', left: {id: 'center'}})
expect(left.map { |e| e.attribute('id') }).to eq(%w[fourth first seventh])
end
it 'finds to the right of another' do
driver.navigate.to url_for('relative_locators.html')
right = driver.find_elements(relative: {tag_name: 'td', right: {id: 'center'}})
expect(right.map { |e| e.attribute('id') }).to eq(%w[sixth third ninth])
end
it 'finds by combined relative locators' do
driver.navigate.to url_for('relative_locators.html')
found = driver.find_elements(relative: {tag_name: 'td', right: {id: 'second'}, above: {id: 'center'}})
expect(found.map { |e| e.attribute('id') }).to eq(['third'])
end
it 'finds all by empty relative locator' do
driver.navigate.to url_for('relative_locators.html')
expected = driver.find_elements(tag_name: 'p')
actual = driver.find_elements(relative: {tag_name: 'p'})
expect(actual).to eq(expected)
end
it 'finds children by field name' do
driver.navigate.to url_for('nestedElements.html')
element = driver.find_element(name: 'form2')
children = element.find_elements(name: 'selectomatic')
expect(children.size).to eq(2)
end
end
describe 'execute script' do
it 'returns strings' do
driver.navigate.to url_for('xhtmlTest.html')
expect(driver.execute_script('return document.title;')).to eq('XHTML Test Page')
end
it 'returns numbers' do
driver.navigate.to url_for('xhtmlTest.html')
expect(driver.execute_script('return document.title.length;')).to eq(15)
end
it 'returns elements' do
driver.navigate.to url_for('xhtmlTest.html')
element = driver.execute_script("return document.getElementById('id1');")
expect(element).to be_a(WebDriver::Element)
expect(element.text).to eq('Foo')
end
it 'unwraps elements in deep objects' do
driver.navigate.to url_for('xhtmlTest.html')
result = driver.execute_script(<<~SCRIPT)
var e1 = document.getElementById('id1');
var body = document.body;
return {
elements: {'body' : body, other: [e1] }
};
SCRIPT
expect(result).to be_a(Hash)
expect(result['elements']['body']).to be_a(WebDriver::Element)
expect(result['elements']['other'].first).to be_a(WebDriver::Element)
end
it 'returns booleans' do
driver.navigate.to url_for('xhtmlTest.html')
expect(driver.execute_script('return true;')).to be(true)
end
it 'raises if the script is bad' do
driver.navigate.to url_for('xhtmlTest.html')
expect {
driver.execute_script('return squiggle();')
}.to raise_error(Selenium::WebDriver::Error::JavascriptError)
end
it 'returns arrays' do
driver.navigate.to url_for('xhtmlTest.html')
expect(driver.execute_script('return ["zero", "one", "two"];')).to eq(%w[zero one two])
end
it 'is able to call functions on the page' do
driver.navigate.to url_for('javascriptPage.html')
driver.execute_script("displayMessage('I like cheese');")
expect(driver.find_element(id: 'result').text.strip).to eq('I like cheese')
end
it 'is able to pass string arguments' do
driver.navigate.to url_for('javascriptPage.html')
expect(driver.execute_script("return arguments[0] == 'fish' ? 'fish' : 'not fish';", 'fish')).to eq('fish')
end
it 'is able to pass boolean arguments' do
driver.navigate.to url_for('javascriptPage.html')
expect(driver.execute_script('return arguments[0] == true;', true)).to be(true)
end
it 'is able to pass numeric arguments' do
driver.navigate.to url_for('javascriptPage.html')
expect(driver.execute_script('return arguments[0] == 1 ? 1 : 0;', 1)).to eq(1)
end
it 'is able to pass null arguments' do
driver.navigate.to url_for('javascriptPage.html')
expect(driver.execute_script('return arguments[0];', nil)).to be_nil
end
it 'is able to pass array arguments' do
driver.navigate.to url_for('javascriptPage.html')
expect(driver.execute_script('return arguments[0];', [1, '2', 3])).to eq([1, '2', 3])
end
it 'is able to pass element arguments' do
driver.navigate.to url_for('javascriptPage.html')
button = driver.find_element(id: 'plainButton')
js = "arguments[0]['flibble'] = arguments[0].getAttribute('id'); return arguments[0]['flibble'];"
expect(driver.execute_script(js, button))
.to eq('plainButton')
end
it 'is able to pass in multiple arguments' do
driver.navigate.to url_for('javascriptPage.html')
expect(driver.execute_script('return arguments[0] + arguments[1];', 'one', 'two')).to eq('onetwo')
end
end
describe 'execute async script' do
before do
driver.manage.timeouts.script = 1
driver.navigate.to url_for('ajaxy_page.html')
end
it 'is able to return arrays of primitives from async scripts' do
result = driver.execute_async_script "arguments[arguments.length - 1]([null, 123, 'abc', true, false]);"
expect(result).to eq([nil, 123, 'abc', true, false])
end
it 'is able to pass multiple arguments to async scripts' do
result = driver.execute_async_script 'arguments[arguments.length - 1](arguments[0] + arguments[1]);', 1, 2
expect(result).to eq(3)
end
# Safari raises TimeoutError instead
it 'times out if the callback is not invoked', except: {browser: %i[safari safari_preview]} do
expect {
# Script is expected to be async and explicitly callback, so this should timeout.
driver.execute_async_script 'return 1 + 2;'
}.to raise_error(Selenium::WebDriver::Error::ScriptTimeoutError)
end
end
end
end # WebDriver
end # Selenium