"Fossies" - the Fresh Open Source Software Archive 
Member "selenium-selenium-4.8.1/rb/spec/integration/selenium/webdriver/element_spec.rb" (17 Feb 2023, 20602 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the last
Fossies "Diffs" side-by-side code changes report for "element_spec.rb":
4.7.0_vs_4.8.0.
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_relative 'spec_helper'
21
22 module Selenium
23 module WebDriver
24 describe Element do
25 it 'clicks' do
26 driver.navigate.to url_for('formPage.html')
27 expect { driver.find_element(id: 'imageButton').click }.not_to raise_error
28 reset_driver!(time: 1) if %i[safari safari_preview].include? GlobalTestEnv.browser
29 end
30
31 # Safari returns "click intercepted" error instead of "element click intercepted"
32 it 'raises if different element receives click', except: {browser: %i[safari safari_preview]} do
33 driver.navigate.to url_for('click_tests/overlapping_elements.html')
34 expect { driver.find_element(id: 'contents').click }.to raise_error(Error::ElementClickInterceptedError)
35 end
36
37 # Safari returns "click intercepted" error instead of "element click intercepted"
38 it 'raises if element is partially covered', except: {browser: %i[safari safari_preview]} do
39 driver.navigate.to url_for('click_tests/overlapping_elements.html')
40 expect { driver.find_element(id: 'other_contents').click }.to raise_error(Error::ElementClickInterceptedError)
41 end
42
43 describe '#submit' do
44 it 'valid submit button' do
45 driver.navigate.to url_for('formPage.html')
46 driver.find_element(id: 'submitButton').submit
47
48 sleep 0.5
49 expect(driver.title).to eq('We Arrive Here')
50 end
51
52 it 'any input element in form' do
53 driver.navigate.to url_for('formPage.html')
54 driver.find_element(id: 'checky').submit
55
56 sleep 0.5
57 expect(driver.title).to eq('We Arrive Here')
58 end
59
60 it 'any element in form' do
61 driver.navigate.to url_for('formPage.html')
62 driver.find_element(css: 'form > p').submit
63
64 sleep 0.5
65 expect(driver.title).to eq('We Arrive Here')
66 end
67
68 it 'button with id submit' do
69 driver.navigate.to url_for('formPage.html')
70 driver.find_element(id: 'submit').submit
71
72 sleep 0.5
73 expect(driver.title).to eq('We Arrive Here')
74 end
75
76 it 'button with name submit' do
77 driver.navigate.to url_for('formPage.html')
78 driver.find_element(name: 'submit').submit
79
80 sleep 0.5
81 expect(driver.title).to eq('We Arrive Here')
82 end
83
84 it 'errors with button outside form' do
85 driver.navigate.to url_for('formPage.html')
86 expect { driver.find_element(name: 'SearchableText').submit }.to raise_error(Error::UnsupportedOperationError)
87 end
88 end
89
90 it 'sends empty keys' do
91 driver.navigate.to url_for('formPage.html')
92 element = wait_for_element(id: 'working')
93 element.send_keys
94 expect(element.text).to be_empty
95 end
96
97 it 'sends string keys' do
98 driver.navigate.to url_for('formPage.html')
99 wait_for_element(id: 'working')
100 expect { driver.find_element(id: 'working').send_keys('foo', 'bar') }.not_to raise_error
101 end
102
103 it 'sends key presses' do
104 driver.navigate.to url_for('javascriptPage.html')
105 key_reporter = driver.find_element(id: 'keyReporter')
106
107 key_reporter.send_keys('Tet', :arrow_left, 's')
108 expect(key_reporter.attribute('value')).to eq('Test')
109 end
110
111 # https://github.com/mozilla/geckodriver/issues/245
112 it 'sends key presses chords', except: {browser: %i[firefox safari safari_preview]} do
113 driver.navigate.to url_for('javascriptPage.html')
114 key_reporter = driver.find_element(id: 'keyReporter')
115
116 key_reporter.send_keys([:shift, 'h'], 'ello')
117 expect(key_reporter.attribute('value')).to eq('Hello')
118 end
119
120 it 'handles file uploads' do
121 driver.navigate.to url_for('formPage.html')
122
123 element = driver.find_element(id: 'upload')
124 expect(element.attribute('value')).to be_empty
125
126 path = WebDriver::Platform.windows? ? WebDriver::Platform.windows_path(__FILE__) : __FILE__
127
128 element.send_keys path
129
130 expect(element.attribute('value')).to include(File.basename(path))
131 end
132
133 describe 'properties and attributes' do
134 before { driver.navigate.to url_for('formPage.html') }
135
136 context 'when string type' do
137 let(:element) { driver.find_element(id: 'checky') }
138 let(:prop_or_attr) { 'type' }
139
140 it '#dom_attribute returns attribute value' do
141 expect(element.dom_attribute(prop_or_attr)).to eq 'checkbox'
142 end
143
144 it '#property returns property value' do
145 expect(element.property(prop_or_attr)).to eq 'checkbox'
146 end
147
148 it '#attribute returns value' do
149 expect(element.attribute(prop_or_attr)).to eq 'checkbox'
150 end
151 end
152
153 context 'when numeric type' do
154 let(:element) { driver.find_element(id: 'withText') }
155 let(:prop_or_attr) { 'rows' }
156
157 it '#dom_attribute String' do
158 expect(element.dom_attribute(prop_or_attr)).to eq '5'
159 end
160
161 it '#property returns Number' do
162 expect(element.property(prop_or_attr)).to eq 5
163 end
164
165 it '#attribute returns String' do
166 expect(element.attribute(prop_or_attr)).to eq '5'
167 end
168 end
169
170 context 'with boolean type of true' do
171 let(:element) { driver.find_element(id: 'checkedchecky') }
172 let(:prop_or_attr) { 'checked' }
173
174 it '#dom_attribute returns String', except: {browser: :safari} do
175 expect(element.dom_attribute(prop_or_attr)).to eq 'true'
176 end
177
178 it '#property returns true' do
179 expect(element.property(prop_or_attr)).to be true
180 end
181
182 it '#attribute returns String' do
183 expect(element.attribute(prop_or_attr)).to eq 'true'
184 end
185
186 it '#dom_attribute does not update after click', except: {browser: :safari} do
187 element.click
188 expect(element.dom_attribute(prop_or_attr)).to eq 'true'
189 end
190
191 it '#property updates to false after click' do
192 element.click
193 expect(element.property(prop_or_attr)).to be false
194 end
195
196 it '#attribute updates to nil after click' do
197 element.click
198 expect(element.attribute(prop_or_attr)).to be_nil
199 end
200 end
201
202 context 'with boolean type of false' do
203 let(:element) { driver.find_element(id: 'checky') }
204 let(:prop_or_attr) { 'checked' }
205
206 it '#dom_attribute returns nil' do
207 expect(element.dom_attribute(prop_or_attr)).to be_nil
208 end
209
210 it '#property returns false' do
211 expect(element.property(prop_or_attr)).to be false
212 end
213
214 it '#attribute returns nil' do
215 expect(element.attribute(prop_or_attr)).to be_nil
216 end
217
218 it '#dom_attribute does not update after click' do
219 element.click
220 expect(element.dom_attribute(prop_or_attr)).to be_nil
221 end
222
223 it '#property updates to true after click' do
224 element.click
225 expect(element.property(prop_or_attr)).to be true
226 end
227
228 it '#attribute updates to String after click' do
229 element.click
230 expect(element.attribute(prop_or_attr)).to eq 'true'
231 end
232 end
233
234 context 'when property exists but attribute does not' do
235 let(:element) { driver.find_element(id: 'withText') }
236 let(:prop_or_attr) { 'value' }
237
238 it '#dom_attribute returns nil' do
239 expect(element.dom_attribute(prop_or_attr)).to be_nil
240 end
241
242 it '#property returns default property' do
243 expect(element.property(prop_or_attr)).to eq 'Example text'
244 end
245
246 it '#attribute returns default property' do
247 expect(element.attribute(prop_or_attr)).to eq 'Example text'
248 end
249
250 it '#property returns updated property' do
251 element.clear
252 expect(element.property(prop_or_attr)).to be_empty
253 end
254
255 it '#attribute returns updated property' do
256 element.clear
257 expect(element.attribute(prop_or_attr)).to be_empty
258 end
259 end
260
261 context 'when attribute exists but property does not' do
262 let(:element) { driver.find_element(id: 'vsearchGadget') }
263 let(:prop_or_attr) { 'accesskey' }
264
265 it '#dom_attribute returns attribute' do
266 expect(element.dom_attribute(prop_or_attr)).to eq '4'
267 end
268
269 it '#property returns nil' do
270 expect(element.property(prop_or_attr)).to be_nil
271 end
272
273 it '#attribute returns attribute' do
274 expect(element.attribute(prop_or_attr)).to eq '4'
275 end
276 end
277
278 context 'when neither attribute nor property exists' do
279 let(:element) { driver.find_element(id: 'checky') }
280 let(:prop_or_attr) { 'nonexistent' }
281
282 it '#dom_attribute returns nil' do
283 expect(element.dom_attribute(prop_or_attr)).to be_nil
284 end
285
286 it '#property returns nil' do
287 expect(element.property(prop_or_attr)).to be_nil
288 end
289
290 it '#attribute returns nil' do
291 expect(element.attribute(prop_or_attr)).to be_nil
292 end
293 end
294
295 describe 'style' do
296 before { driver.navigate.to url_for('clickEventPage.html') }
297
298 let(:element) { driver.find_element(id: 'result') }
299 let(:prop_or_attr) { 'style' }
300
301 it '#dom_attribute attribute with no formatting' do
302 expect(element.dom_attribute(prop_or_attr)).to eq 'width:300;height:60'
303 end
304
305 # TODO: This might not be correct behavior
306 it '#property returns object',
307 except: [{browser: :firefox,
308 reason: 'https://github.com/mozilla/geckodriver/issues/1846'},
309 {browser: :safari}] do
310 expect(element.property(prop_or_attr)).to eq %w[width height]
311 end
312
313 it '#attribute returns attribute with formatting' do
314 expect(element.attribute(prop_or_attr)).to eq 'width: 300px; height: 60px;'
315 end
316 end
317
318 describe 'incorrect casing' do
319 let(:element) { driver.find_element(id: 'checky') }
320 let(:prop_or_attr) { 'nAme' }
321
322 it '#dom_attribute returns correctly cased attribute' do
323 expect(element.dom_attribute(prop_or_attr)).to eq 'checky'
324 end
325
326 it '#property returns nil' do
327 expect(element.property(prop_or_attr)).to be_nil
328 end
329
330 it '#attribute returns correctly cased attribute' do
331 expect(element.attribute(prop_or_attr)).to eq 'checky'
332 end
333 end
334
335 describe 'property attribute case difference with attribute casing' do
336 let(:element) { driver.find_element(name: 'readonly') }
337 let(:prop_or_attr) { 'readonly' }
338
339 it '#dom_attribute returns a String', except: {browser: :safari} do
340 expect(element.dom_attribute(prop_or_attr)).to eq 'true'
341 end
342
343 it '#property returns nil' do
344 expect(element.property(prop_or_attr)).to be_nil
345 end
346
347 it '#attribute returns a String' do
348 expect(element.attribute(prop_or_attr)).to eq 'true'
349 end
350 end
351
352 describe 'property attribute case difference with property casing' do
353 let(:element) { driver.find_element(name: 'readonly') }
354 let(:prop_or_attr) { 'readOnly' }
355
356 it '#dom_attribute returns a String',
357 except: [{browser: :firefox,
358 reason: 'https://github.com/mozilla/geckodriver/issues/1850'},
359 {browser: :safari}] do
360 expect(element.dom_attribute(prop_or_attr)).to eq 'true'
361 end
362
363 it '#property returns property as true' do
364 expect(element.property(prop_or_attr)).to be true
365 end
366
367 it '#attribute returns property as String' do
368 expect(element.attribute(prop_or_attr)).to eq 'true'
369 end
370 end
371
372 describe 'property attribute name difference with attribute naming' do
373 let(:element) { driver.find_element(id: 'wallace') }
374 let(:prop_or_attr) { 'class' }
375
376 it '#dom_attribute returns attribute value' do
377 expect(element.dom_attribute(prop_or_attr)).to eq 'gromit'
378 end
379
380 it '#property returns nil' do
381 expect(element.property(prop_or_attr)).to be_nil
382 end
383
384 it '#attribute returns attribute value' do
385 expect(element.attribute(prop_or_attr)).to eq 'gromit'
386 end
387 end
388
389 describe 'property attribute name difference with property naming' do
390 let(:element) { driver.find_element(id: 'wallace') }
391 let(:prop_or_attr) { 'className' }
392
393 it '#dom_attribute returns nil' do
394 expect(element.dom_attribute(prop_or_attr)).to be_nil
395 end
396
397 it '#property returns property value' do
398 expect(element.property(prop_or_attr)).to eq 'gromit'
399 end
400
401 it '#attribute returns property value' do
402 expect(element.attribute(prop_or_attr)).to eq 'gromit'
403 end
404 end
405
406 describe 'property attribute value difference' do
407 let(:element) { driver.find_element(tag_name: 'form') }
408 let(:prop_or_attr) { 'action' }
409
410 it '#dom_attribute returns attribute value' do
411 expect(element.dom_attribute(prop_or_attr)).to eq 'resultPage.html'
412 end
413
414 it '#property returns property value' do
415 expect(element.property(prop_or_attr)).to match(%r{http://(.+)/resultPage\.html})
416 end
417
418 it '#attribute returns property value' do
419 expect(element.attribute(prop_or_attr)).to match(%r{http://(.+)/resultPage\.html})
420 end
421 end
422 end
423
424 it 'returns ARIA role', only: {browser: %i[chrome edge]} do
425 driver.navigate.to 'data:text/html,' \
426 "<div role='heading' aria-level='1'>Level 1 Header</div>" \
427 '<h1>Level 1 Header</h1>' \
428 "<h2 role='alert'>Level 2 Header</h2>"
429 expect(driver.find_element(tag_name: 'div').aria_role).to eq('heading')
430 expect(driver.find_element(tag_name: 'h1').aria_role).to eq('heading')
431 expect(driver.find_element(tag_name: 'h2').aria_role).to eq('alert')
432 end
433
434 it 'returns accessible name', only: {browser: %i[chrome edge]} do
435 driver.navigate.to 'data:text/html,<h1>Level 1 Header</h1>'
436 expect(driver.find_element(tag_name: 'h1').accessible_name).to eq('Level 1 Header')
437 end
438
439 it 'clears' do
440 driver.navigate.to url_for('formPage.html')
441 expect { driver.find_element(id: 'withText').clear }.not_to raise_error
442 end
443
444 it 'gets and set selected' do
445 driver.navigate.to url_for('formPage.html')
446
447 cheese = driver.find_element(id: 'cheese')
448 peas = driver.find_element(id: 'peas')
449
450 cheese.click
451
452 expect(cheese).to be_selected
453 expect(peas).not_to be_selected
454
455 peas.click
456
457 expect(peas).to be_selected
458 expect(cheese).not_to be_selected
459 end
460
461 it 'gets enabled' do
462 driver.navigate.to url_for('formPage.html')
463 expect(driver.find_element(id: 'notWorking')).not_to be_enabled
464 end
465
466 it 'gets text' do
467 driver.navigate.to url_for('xhtmlTest.html')
468 expect(driver.find_element(class: 'header').text).to eq('XHTML Might Be The Future')
469 end
470
471 it 'gets displayed' do
472 driver.navigate.to url_for('xhtmlTest.html')
473 expect(driver.find_element(class: 'header')).to be_displayed
474 end
475
476 describe 'size and location' do
477 it 'gets current location' do
478 driver.navigate.to url_for('xhtmlTest.html')
479 loc = driver.find_element(class: 'header').location
480
481 expect(loc.x).to be >= 1
482 expect(loc.y).to be >= 1
483 end
484
485 it 'gets location once scrolled into view' do
486 driver.navigate.to url_for('javascriptPage.html')
487 loc = driver.find_element(id: 'keyUp').location_once_scrolled_into_view
488
489 expect(loc.x).to be >= 1
490 expect(loc.y).to be >= 0 # can be 0 if scrolled to the top
491 end
492
493 it 'gets size' do
494 driver.navigate.to url_for('xhtmlTest.html')
495 size = driver.find_element(class: 'header').size
496
497 expect(size.width).to be_positive
498 expect(size.height).to be_positive
499 end
500
501 it 'gets rect' do
502 driver.navigate.to url_for('xhtmlTest.html')
503 rect = driver.find_element(class: 'header').rect
504
505 expect(rect.x).to be_positive
506 expect(rect.y).to be_positive
507 expect(rect.width).to be_positive
508 expect(rect.height).to be_positive
509 end
510 end
511
512 # IE - https://github.com/SeleniumHQ/selenium/pull/4043
513 it 'drags and drop', except: {browser: :ie} do
514 driver.navigate.to url_for('dragAndDropTest.html')
515
516 img1 = driver.find_element(id: 'test1')
517 img2 = driver.find_element(id: 'test2')
518
519 driver.action.drag_and_drop_by(img1, 100, 100)
520 .drag_and_drop(img2, img1)
521 .perform
522
523 expect(img1.location).to eq(img2.location)
524 end
525
526 it 'gets css property' do
527 driver.navigate.to url_for('javascriptPage.html')
528 element = driver.find_element(id: 'green-parent')
529
530 style1 = element.css_value('background-color')
531 style2 = element.style('background-color') # backwards compatibility
532
533 acceptable = ['rgb(0, 128, 0)', '#008000', 'rgba(0,128,0,1)', 'rgba(0, 128, 0, 1)']
534 expect(acceptable).to include(style1, style2)
535 end
536
537 it 'knows when two elements are equal' do
538 driver.navigate.to url_for('simpleTest.html')
539
540 body = driver.find_element(tag_name: 'body')
541 xbody = driver.find_element(xpath: '//body')
542 jsbody = driver.execute_script('return document.getElementsByTagName("body")[0]')
543
544 expect(body).to eq(xbody)
545 expect(body).to eq(jsbody)
546 expect(body).to eql(xbody)
547 expect(body).to eql(jsbody)
548 end
549
550 it 'knows when element arrays are equal' do
551 driver.navigate.to url_for('simpleTest.html')
552
553 tags = driver.find_elements(tag_name: 'div')
554 jstags = driver.execute_script('return document.getElementsByTagName("div")')
555
556 expect(tags).to eq(jstags)
557 end
558
559 it 'knows when two elements are not equal' do
560 driver.navigate.to url_for('simpleTest.html')
561
562 elements = driver.find_elements(tag_name: 'p')
563 p1 = elements.fetch(0)
564 p2 = elements.fetch(1)
565
566 expect(p1).not_to eq(p2)
567 expect(p1).not_to eql(p2)
568 end
569
570 it 'returns the same #hash for equal elements when found by Driver#find_element' do
571 driver.navigate.to url_for('simpleTest.html')
572
573 body = driver.find_element(tag_name: 'body')
574 xbody = driver.find_element(xpath: '//body')
575
576 expect(body.hash).to eq(xbody.hash)
577 end
578
579 it 'returns the same #hash for equal elements when found by Driver#find_elements' do
580 driver.navigate.to url_for('simpleTest.html')
581
582 body = driver.find_elements(tag_name: 'body').fetch(0)
583 xbody = driver.find_elements(xpath: '//body').fetch(0)
584
585 expect(body.hash).to eq(xbody.hash)
586 end
587 end
588 end # WebDriver
589 end # Selenium