forked from robotframework/SeleniumLibrary
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_elementfinder.py
794 lines (631 loc) · 30 KB
/
test_elementfinder.py
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
from pathlib import Path
import pytest
from approvaltests import verify_all
from approvaltests.reporters import GenericDiffReporterFactory
from mockito import any, mock, verify, when, unstub
from selenium.webdriver.common.by import By
from SeleniumLibrary.errors import ElementNotFound
from SeleniumLibrary.locators.elementfinder import ElementFinder
@pytest.fixture(scope="function")
def finder():
ctx = mock()
ctx.driver = mock()
return ElementFinder(ctx)
@pytest.fixture
def reporter():
cur_dir = Path(__file__).parent.absolute()
reporter_json = cur_dir / ".." / "approvals_reporters.json"
factory = GenericDiffReporterFactory()
factory.load(reporter_json.absolute())
return factory.get_first_working()
def teardown_function():
unstub()
def test_implicit_xpath():
_verify_parse_locator("//foo", "xpath", "//foo")
_verify_parse_locator("(//foo)", "xpath", "(//foo)")
_verify_parse_locator("((//foo))", "xpath", "((//foo))")
_verify_parse_locator("((((//foo))", "xpath", "((((//foo))")
_verify_parse_locator("//id=bar", "xpath", "//id=bar")
def test_no_separator():
_verify_parse_locator("foo", "default", "foo")
_verify_parse_locator("", "default", "")
def test_equal_sign_as_separator():
_verify_parse_locator("class=foo", "class", "foo")
_verify_parse_locator("id=foo=bar", "id", "foo=bar")
def test_colon_as_separator():
_verify_parse_locator("class:foo", "class", "foo")
_verify_parse_locator("id:foo:bar", "id", "foo:bar")
def test_use_first_separator_when_both_are_used():
_verify_parse_locator("id:foo=bar", "id", "foo=bar")
_verify_parse_locator("id=foo:bar", "id", "foo:bar")
def test_preserve_trailing_whitespace():
_verify_parse_locator("//foo/bar ", "xpath", "//foo/bar ")
_verify_parse_locator("class=foo ", "class", "foo ")
def test_strategy_case_is_not_changed():
_verify_parse_locator("XPATH://foo/bar ", "XPATH", "//foo/bar ")
_verify_parse_locator("XPATH=//foo/bar ", "XPATH", "//foo/bar ")
def test_data_locator_parsing():
_verify_parse_locator("data:id:my_id", "data", "id:my_id")
_verify_parse_locator(
"data:automation:my_automation_id", "data", "automation:my_automation_id"
)
def test_remove_whitespace_around_prefix_and_separator():
_verify_parse_locator("class = foo", "class", "foo")
_verify_parse_locator("class : foo", "class", "foo")
_verify_parse_locator(" id = foo = bar ", "id", "foo = bar ")
_verify_parse_locator(" id : foo : bar ", "id", "foo : bar ")
def test_separator_without_matching_prefix_is_ignored():
_verify_parse_locator("no=match", "default", "no=match")
_verify_parse_locator("no:match", "default", "no:match")
def test_registered_strategy_can_be_used_as_prefix():
_verify_parse_locator("registered=no", "default", "registered=no")
_verify_parse_locator("registered:no", "default", "registered:no")
finder = ElementFinder(None)
finder.register("registered", lambda *args: None, persist=True)
_verify_parse_locator("registered=yes!!", "registered", "yes!!", finder)
_verify_parse_locator("registered:yes!!", "registered", "yes!!", finder)
def _verify_parse_locator(locator, prefix, criteria, finder=None):
if not finder:
finder = ElementFinder(None)
get_prefix, get_criteria = finder._parse_locator(locator)
assert get_prefix == prefix
assert get_criteria == criteria
def test_parent_is_not_webelement(finder):
with pytest.raises(ValueError):
finder.find("//div", parent="//button")
def test_find_by_xpath_parent_is_webelement(finder):
webelement = mock()
when(finder)._is_webelement(webelement).thenReturn(True)
when(finder)._is_webelement("//div").thenReturn(False)
when(webelement).find_elements(By.XPATH, "//div").thenReturn([mock()])
finder.find("//div", parent=webelement)
verify(webelement).find_elements(By.XPATH, "//div")
def test_find_by_identifier_parent_is_webelement(finder):
webelement = mock()
when(finder)._is_webelement(webelement).thenReturn(True)
when(finder)._is_webelement("identifier=value").thenReturn(False)
when(webelement).find_elements(By.ID, "value").thenReturn([mock()])
when(webelement).find_elements(By.NAME, "value").thenReturn([mock()])
finder.find("identifier=value", parent=webelement)
verify(webelement).find_elements(By.NAME, "value")
verify(webelement).find_elements(By.ID, "value")
def test_find_by_id_parent_is_webelement(finder):
webelement = mock()
when(finder)._is_webelement(webelement).thenReturn(True)
when(finder)._is_webelement("id=value").thenReturn(False)
# when(webelement).find_elements(By.ID, 'value').thenReturn([mock()])
when(webelement).find_elements(By.ID, "value").thenReturn([mock()])
finder.find("id=value", parent=webelement)
verify(webelement).find_elements(By.ID, "value")
def test_find_by_name_parent_is_webelement(finder):
webelement = mock()
when(finder)._is_webelement(webelement).thenReturn(True)
when(finder)._is_webelement("name=value").thenReturn(False)
when(webelement).find_elements(By.NAME, "value").thenReturn([mock()])
finder.find("name=value", parent=webelement)
verify(webelement).find_elements(By.NAME, "value")
def test_find_by_dom__parent_is_webelement(finder):
webelement = mock()
when(finder)._is_webelement(webelement).thenReturn(True)
when(finder)._is_webelement("dom=value").thenReturn(False)
when(finder)._disallow_webelement_parent(webelement).thenRaise(
ValueError("This method does not allow webelement as parent")
)
with pytest.raises(ValueError) as error:
finder.find("dom=value", parent=webelement)
assert "not allow webelement as parent" in str(error.value)
def test_find_by_sizzle_parent_is_webelement(finder):
webelement = mock()
when(finder)._is_webelement(webelement).thenReturn(True)
when(finder)._is_webelement("sizzle=div.class").thenReturn(False)
when(finder)._disallow_webelement_parent(webelement).thenRaise(
ValueError("This method does not allow webelement as parent")
)
with pytest.raises(ValueError) as error:
finder.find("sizzle=div.class", parent=webelement)
assert "not allow webelement as parent" in str(error.value)
def test_find_by_link_text_parent_is_webelement(finder):
webelement = mock()
when(finder)._is_webelement(webelement).thenReturn(True)
when(finder)._is_webelement("link=My Link").thenReturn(False)
when(webelement).find_elements(By.LINK_TEXT, "My Link").thenReturn([mock()])
finder.find("link=My Link", parent=webelement)
verify(webelement).find_elements(By.LINK_TEXT, "My Link")
def test_find_by_partial_link_text_parent_is_webelement(finder):
webelement = mock()
when(finder)._is_webelement(webelement).thenReturn(True)
when(finder)._is_webelement("partial link=My L").thenReturn(False)
when(webelement).find_elements(By.PARTIAL_LINK_TEXT, "My L").thenReturn([mock()])
finder.find("partial link=My L", parent=webelement)
verify(webelement).find_elements(By.PARTIAL_LINK_TEXT, "My L")
def test_find_by_css_parent_is_webelement(finder):
webelement = mock()
when(finder)._is_webelement(webelement).thenReturn(True)
when(finder)._is_webelement("css=div").thenReturn(False)
when(webelement).find_elements(By.CSS_SELECTOR, "div").thenReturn([mock()])
finder.find("css=div", parent=webelement)
verify(webelement).find_elements(By.CSS_SELECTOR, "div")
def test_find_by_class_parent_is_webelement(finder):
webelement = mock()
when(finder)._is_webelement(webelement).thenReturn(True)
when(finder)._is_webelement("class=name").thenReturn(False)
when(webelement).find_elements(By.CLASS_NAME, "name").thenReturn([mock()])
finder.find("class=name", parent=webelement)
verify(webelement).find_elements(By.CLASS_NAME, "name")
def test_find_by_tag_name_parent_is_webelement(finder):
webelement = mock()
when(finder)._is_webelement(webelement).thenReturn(True)
when(finder)._is_webelement("tag=name").thenReturn(False)
when(webelement).find_elements(By.TAG_NAME, "name").thenReturn([mock()])
finder.find("tag=name", parent=webelement)
verify(webelement).find_elements(By.TAG_NAME, "name")
def test_find_sc_locator_parent_is_webelement(finder):
webelement = mock()
when(finder)._is_webelement(webelement).thenReturn(True)
when(finder)._is_webelement("scLocator=div").thenReturn(False)
when(finder)._disallow_webelement_parent(webelement).thenRaise(
ValueError("This method does not allow webelement as parent")
)
with pytest.raises(ValueError) as error:
finder.find("scLocator=div", parent=webelement)
assert "not allow webelement as parent" in str(error.value)
def test_find_by_default_parent_is_webelement(finder):
xpath = "//*[(@id='name' or @name='name')]"
webelement = mock()
when(finder)._is_webelement(webelement).thenReturn(True)
when(finder)._is_webelement("default=name").thenReturn(False)
when(webelement).find_elements(By.XPATH, xpath).thenReturn([mock()])
finder.find("default=name", parent=webelement)
verify(webelement).find_elements(By.XPATH, xpath)
def test_non_existing_prefix(finder):
with pytest.raises(ElementNotFound):
finder.find("something=test1")
with pytest.raises(ElementNotFound):
finder.find("foo:bar")
def test_find_with_no_tag(finder):
driver = _get_driver(finder)
finder.find("test1", required=False)
verify(driver).find_elements(By.XPATH, "//*[(@id='test1' or " "@name='test1')]")
def test_find_with_explicit_default_strategy(finder):
driver = _get_driver(finder)
finder.find("default=test1", required=False)
verify(driver).find_elements(By.XPATH, "//*[(@id='test1' or " "@name='test1')]")
def test_find_with_explicit_default_strategy_and_equals(finder):
driver = _get_driver(finder)
driver.current_url = "http://localhost/mypage.html"
finder.find("default=page.do?foo=bar", tag="a", required=False)
verify(driver).find_elements(
By.XPATH,
"//a[(@id='page.do?foo=bar' or @name='page.do?foo=bar' or "
"@href='page.do?foo=bar' or "
"normalize-space(descendant-or-self::text())='page.do?foo=bar' or "
"@href='http://localhost/page.do?foo=bar')]",
)
def test_find_with_tag(finder):
driver = _get_driver(finder)
finder.find("test1", tag="div", required=False)
verify(driver).find_elements(By.XPATH, "//div[(@id='test1' or @name='test1')]")
def test_find_with_data(finder):
driver = _get_driver(finder)
finder.find("data:id:my_id", tag="div", required=False)
verify(driver).find_elements(By.XPATH, '//*[@data-id="my_id"]')
def test_find_with_invalid_data(finder):
with pytest.raises(
ValueError,
match=r"^Provided selector \(id:\) is malformed\. Correct format: name:value\.",
):
finder.find("data:id:", tag="div", required=False)
with pytest.raises(
ValueError,
match=r"^Provided selector \(\) is malformed\. Correct format: name:value\.",
):
finder.find("data:", tag="div", required=False)
def test_find_with_locator_with_apos(finder):
driver = _get_driver(finder)
finder.find("test '1'", required=False)
verify(driver).find_elements(
By.XPATH, "//*[(@id=\"test '1'\" or @name=\"test '1'\")]"
)
def test_find_with_locator_with_quote(finder):
driver = _get_driver(finder)
finder.find('test "1"', required=False)
verify(driver).find_elements(
By.XPATH, "//*[(@id='test \"1\"' or @name='test \"1\"')]"
)
def test_find_with_locator_with_quote_and_apos(finder):
driver = _get_driver(finder)
finder.find("test \"1\" and '2'", required=False)
verify(driver).find_elements(
By.XPATH,
"//*[(@id=concat('test \"1\" and ', \"'\", '2', \"'\", '') "
"or @name=concat('test \"1\" and ', \"'\", '2', \"'\", ''))]",
)
def test_find_with_a(finder):
driver = _get_driver(finder)
driver.current_url = "http://localhost/mypage.html"
finder.find("test1", tag="a", required=False)
verify(driver).find_elements(
By.XPATH,
"//a[(@id='test1' or @name='test1' or @href='test1' or "
"normalize-space(descendant-or-self::text())='test1' or "
"@href='http://localhost/test1')]",
)
def test_find_with_link_synonym(finder):
driver = _get_driver(finder)
driver.current_url = "http://localhost/mypage.html"
finder.find("test1", tag="link", required=False)
verify(driver).find_elements(
By.XPATH,
"//a[(@id='test1' or @name='test1' or @href='test1' or "
"normalize-space(descendant-or-self::text())='test1' or "
"@href='http://localhost/test1')]",
)
def test_find_with_img(finder):
driver = _get_driver(finder)
driver.current_url = "http://localhost/mypage.html"
finder.find("test1", tag="img", required=False)
verify(driver).find_elements(
By.XPATH,
"//img[(@id='test1' or @name='test1' or @src='test1' or "
"@alt='test1' or @src='http://localhost/test1')]",
)
def test_find_with_image_synonym(finder):
driver = _get_driver(finder)
driver.current_url = "http://localhost/mypage.html"
finder.find("test1", tag="image", required=False)
verify(driver).find_elements(
By.XPATH,
"//img[(@id='test1' or @name='test1' or @src='test1' or "
"@alt='test1' or @src='http://localhost/test1')]",
)
def test_find_with_input(finder):
driver = _get_driver(finder)
driver.current_url = "http://localhost/mypage.html"
finder.find("test1", tag="input", required=False)
verify(driver).find_elements(
By.XPATH,
"//input[(@id='test1' or @name='test1' or @value='test1' or "
"@src='test1' or @src='http://localhost/test1')]",
)
def test_find_with_radio_button_synonym(finder):
driver = _get_driver(finder)
driver.current_url = "http://localhost/mypage.html"
finder.find("test1", tag="radio button", required=False)
verify(driver).find_elements(
By.XPATH,
"//input[@type='radio' and (@id='test1' or @name='test1' or "
"@value='test1' or @src='test1' or "
"@src='http://localhost/test1')]",
)
def test_find_with_checkbox_synonym(finder):
driver = _get_driver(finder)
driver.current_url = "http://localhost/mypage.html"
finder.find("test1", tag="checkbox", required=False)
verify(driver).find_elements(
By.XPATH,
"//input[@type='checkbox' and (@id='test1' or @name='test1' or "
"@value='test1' or @src='test1' or "
"@src='http://localhost/test1')]",
)
def test_find_with_file_upload_synonym(finder):
driver = _get_driver(finder)
driver.current_url = "http://localhost/mypage.html"
finder.find("test1", tag="file upload", required=False)
verify(driver).find_elements(
By.XPATH,
"//input[@type='file' and (@id='test1' or @name='test1' or "
"@value='test1' or @src='test1' or "
"@src='http://localhost/test1')]",
)
def test_find_with_text_field_synonym(finder):
driver = _get_driver(finder)
driver.current_url = "http://localhost/mypage.html"
finder.find("test1", tag="text field", required=False)
verify(driver).find_elements(
By.XPATH,
"//input[@type[. = 'date' or . = 'datetime-local' or . = 'email' or "
". = 'month' or . = 'number' or . = 'password' or . = 'search' or "
". = 'tel' or . = 'text' or . = 'time' or . = 'url' or . = 'week' or . = 'file'] and "
"(@id='test1' or @name='test1' or @value='test1' or @src='test1' or "
"@src='http://localhost/test1')]",
)
def test_find_with_button(finder):
driver = _get_driver(finder)
finder.find("test1", tag="button", required=False)
verify(driver).find_elements(
By.XPATH,
"//button[(@id='test1' or @name='test1' or @value='test1' or "
"normalize-space(descendant-or-self::text())='test1')]",
)
def test_find_with_select(finder):
driver = _get_driver(finder)
finder.find("test1", tag="select", required=False)
verify(driver).find_elements(By.XPATH, "//select[(@id='test1' or @name='test1')]")
def test_find_with_list_synonym(finder):
driver = _get_driver(finder)
finder.find("test1", tag="list", required=False)
verify(driver).find_elements(By.XPATH, "//select[(@id='test1' or @name='test1')]")
def test_find_with_implicit_xpath(finder):
driver = _get_driver(finder)
elements = _make_mock_elements("div", "a", "span", "a")
when(driver).find_elements(By.XPATH, "//*[(@test='1')]").thenReturn(elements)
result = finder.find("//*[(@test='1')]", first_only=False)
assert result == elements
result = finder.find("//*[(@test='1')]", tag="a", first_only=False)
assert result == [elements[1], elements[3]]
def test_find_by_identifier(finder):
driver = _get_driver(finder)
id_elements = _make_mock_elements("div", "a")
name_elements = _make_mock_elements("span", "a")
when(driver).find_elements(By.ID, "test1").thenReturn(list(id_elements)).thenReturn(
list(id_elements)
)
when(driver).find_elements(By.NAME, "test1").thenReturn(
list(name_elements)
).thenReturn(list(name_elements))
all_elements = list(id_elements)
all_elements.extend(name_elements)
result = finder.find("identifier=test1", first_only=False)
assert result == all_elements
result = finder.find("identifier=test1", tag="a", first_only=False)
assert result == [id_elements[1], name_elements[1]]
def test_find_by_id(finder):
driver = _get_driver(finder)
elements = _make_mock_elements("div", "a", "span", "a")
when(driver).find_elements(By.ID, "test1").thenReturn(elements)
result = finder.find("id=test1", first_only=False)
assert result == elements
result = finder.find("id=test1", tag="a", first_only=False)
assert result == [elements[1], elements[3]]
def test_find_by_name(finder):
driver = _get_driver(finder)
elements = _make_mock_elements("div", "a", "span", "a")
when(driver).find_elements(By.NAME, "test1").thenReturn(elements)
result = finder.find("name=test1", first_only=False)
assert result == elements
result = finder.find("name=test1", tag="a", first_only=False)
assert result == [elements[1], elements[3]]
def test_find_by_xpath(finder):
driver = _get_driver(finder)
elements = _make_mock_elements("div", "a", "span", "a")
when(driver).find_elements(By.XPATH, "//*[(@test='1')]").thenReturn(elements)
result = finder.find("xpath=//*[(@test='1')]", first_only=False)
assert result == elements
result = finder.find("xpath=//*[(@test='1')]", tag="a", first_only=False)
assert result == [elements[1], elements[3]]
def test_find_by_dom(finder):
driver = _get_driver(finder)
elements = _make_mock_elements("div", "a", "span", "a")
elems = [elements[1], elements[3]]
when(driver).execute_script(
"return document.getElementsByTagName('a');"
).thenReturn(elems)
result = finder.find("dom=document.getElementsByTagName('a')", first_only=False)
assert result == [elements[1], elements[3]]
def test_find_by_link_text(finder):
driver = _get_driver(finder)
elements = _make_mock_elements("div", "a", "span", "a")
when(driver).find_elements(By.LINK_TEXT, "my link").thenReturn(elements)
result = finder.find("link=my link", first_only=False)
assert result == elements
result = finder.find("link=my link", tag="a", first_only=False)
assert result == [elements[1], elements[3]]
def test_find_by_partial_link_text(finder):
driver = _get_driver(finder)
elements = _make_mock_elements("div", "a", "span", "a")
when(driver).find_elements(By.PARTIAL_LINK_TEXT, "my link").thenReturn(elements)
result = finder.find("partial link=my link", first_only=False)
assert result == elements
result = finder.find("partial link=my link", tag="a", first_only=False)
assert result == [elements[1], elements[3]]
def test_find_by_css_selector(finder):
driver = _get_driver(finder)
elements = _make_mock_elements("div", "a", "span", "a")
when(driver).find_elements(By.CSS_SELECTOR, "#test1").thenReturn(elements)
result = finder.find("css=#test1", first_only=False)
assert result == elements
result = finder.find("css=#test1", tag="a", first_only=False)
assert result == [elements[1], elements[3]]
def test_find_by_class_names(finder):
driver = _get_driver(finder)
elements = _make_mock_elements("div", "a", "span", "a")
when(driver).find_elements(By.CLASS_NAME, "test1").thenReturn(elements)
result = finder.find("class=test1", first_only=False)
assert result == elements
result = finder.find("class=test1", tag="a", first_only=False)
assert result == [elements[1], elements[3]]
def test_find_by_tag_name(finder):
driver = _get_driver(finder)
elements = _make_mock_elements("div", "a", "span", "a")
when(driver).find_elements(By.TAG_NAME, "div").thenReturn(elements)
result = finder.find("tag=div", first_only=False)
assert result == elements
result = finder.find("tag=div", tag="a", first_only=False)
assert result == [elements[1], elements[3]]
def test_find_with_sloppy_prefix(finder):
driver = _get_driver(finder)
elements = _make_mock_elements("div", "a", "span", "a")
when(driver).find_elements(By.ID, "test1").thenReturn(elements)
when(driver).find_elements(By.PARTIAL_LINK_TEXT, "test1").thenReturn(elements)
result = finder.find("ID=test1", first_only=False)
assert result == elements
result = finder.find("iD=test1", first_only=False)
assert result == elements
result = finder.find("id=test1", first_only=False)
assert result == elements
result = finder.find(" id =test1", first_only=False)
assert result == elements
result = finder.find(" partiallink =test1", first_only=False)
assert result == elements
result = finder.find(" p art iallin k =test1", first_only=False)
assert result == elements
def test_find_with_sloppy_criteria(finder):
driver = _get_driver(finder)
elements = _make_mock_elements("div", "a", "span", "a")
when(driver).find_elements(By.ID, "test1 ").thenReturn(elements)
result = finder.find("id= test1 ", first_only=False)
assert result == elements
def test_find_by_id_with_synonym_and_constraints(finder):
driver = _get_driver(finder)
elements = _make_mock_elements(
"div", "input", "span", "input", "a", "input", "div", "input", "input"
)
elements[1].set_attribute("type", "radio")
elements[3].set_attribute("type", "checkbox")
elements[5].set_attribute("type", "text")
elements[7].set_attribute("type", "file")
elements[8].set_attribute("type", "email")
when(driver).find_elements(By.ID, "test1").thenReturn(elements)
result = finder.find("id=test1", first_only=False)
assert result == elements
result = finder.find("id=test1", tag="input", first_only=False)
assert result == [elements[1], elements[3], elements[5], elements[7], elements[8]]
result = finder.find("id=test1", tag="radio button", first_only=False)
assert result == [elements[1]]
result = finder.find("id=test1", tag="checkbox", first_only=False)
assert result == [elements[3]]
result = finder.find("id=test1", tag="text field", first_only=False)
assert result == [elements[5], elements[7], elements[8]]
result = finder.find("id=test1", tag="file upload", first_only=False)
assert result == [elements[7]]
def test_find_returns_bad_values(finder):
# selenium.webdriver.ie.webdriver.WebDriver sometimes returns these
# and ChromeDriver has also returned None:
# https://github.com/SeleniumHQ/selenium/issues/4555
driver = _get_driver(finder)
func_name = "find_elements"
locator_strategies = (
By.ID,
By.NAME,
By.XPATH,
By.LINK_TEXT,
By.CSS_SELECTOR,
By.TAG_NAME,
)
for bad_value in (None, {"": None}):
for locator_strategy in locator_strategies:
when_find_func = getattr(when(driver), func_name)
when_find_func(locator_strategy, any()).thenReturn(bad_value)
for locator in (
"identifier=it",
"id=it",
"name=it",
"xpath=//div",
"link=it",
"css=div.it",
"tag=div",
"default",
):
result = finder.find(locator, required=False, first_only=False)
assert result == []
result = finder.find(locator, tag="div", required=False, first_only=False)
assert result == []
def test_usage_of_multiple_locators_using_double_arrow_as_separator(finder):
driver = _get_driver(finder)
div_elements = [_make_mock_element("div")]
a_elements = [_make_mock_element("a")]
img_elements = [_make_mock_element("img")]
when(driver).find_elements(By.CSS_SELECTOR, "#test1").thenReturn(div_elements)
when(div_elements[0]).find_elements(By.XPATH, "//a").thenReturn(a_elements)
when(a_elements[0]).find_elements(By.TAG_NAME, "img").thenReturn(img_elements)
when(finder)._is_webelement("css=#test1 >> xpath=//a >> tag=img").thenReturn(False)
when(finder)._is_webelement(["xpath=//a", "tag=img"]).thenReturn(False)
when(finder)._is_webelement(["tag=img"]).thenReturn(False)
when(finder)._is_webelement("tag=img").thenReturn(False)
when(finder)._is_webelement(div_elements[0]).thenReturn(True)
when(finder)._is_webelement("css=#test1").thenReturn(False)
when(finder)._is_webelement(a_elements[0]).thenReturn(True)
when(finder)._is_webelement("xpath=//a").thenReturn(False)
result = finder.find("css=#test1 >> xpath=//a >> tag=img", first_only=False)
assert result == img_elements
def test_usage_of_multiple_locators_using_list(finder):
driver = _get_driver(finder)
div_elements = [_make_mock_element("div")]
a_elements = [_make_mock_element("a")]
img_elements = [_make_mock_element("img")]
list_of_locators = ["css:#test1", "xpath://a", "tag:img"]
when(driver).find_elements(By.CSS_SELECTOR, "#test1").thenReturn(div_elements)
when(div_elements[0]).find_elements(By.XPATH, "//a").thenReturn(a_elements)
when(a_elements[0]).find_elements(By.TAG_NAME, "img").thenReturn(img_elements)
when(finder)._is_webelement(list_of_locators).thenReturn(False)
when(finder)._is_webelement(["xpath://a", "tag:img"]).thenReturn(False)
when(finder)._is_webelement(["tag:img"]).thenReturn(False)
when(finder)._is_webelement("tag:img").thenReturn(False)
when(finder)._is_webelement(div_elements[0]).thenReturn(True)
when(finder)._is_webelement("css:#test1").thenReturn(False)
when(finder)._is_webelement(a_elements[0]).thenReturn(True)
when(finder)._is_webelement("xpath://a").thenReturn(False)
result = finder.find(list_of_locators, first_only=False)
assert result == img_elements
def test_locator_split(finder: ElementFinder, reporter: GenericDiffReporterFactory):
results = [
finder._split_locator("//div"),
finder._split_locator("xpath://div"),
finder._split_locator("xpath=//div"),
finder._split_locator('//*[text(), " >> "]'),
finder._split_locator('//*[text(), " >> "] >> css:foobar'),
finder._split_locator('//*[text(), " >> "] >> //div'),
finder._split_locator('//*[text(), " >> "] >> css:foobar >> id:tidii'),
finder._split_locator(
"identifier:id >> id=name >> name=id >> xpath://a >> dom=name >> link=id >> partial link=something >> "
'css=#name >> class:name >> jquery=dom.find("foobar") >> sizzle:query.find("tidii") >> '
"tag:name >> scLocator:tidii"
),
finder._split_locator(['//*[text(), " >> "]', "css:foobar", "tidii"]),
finder._split_locator("xpath://* >> xpath://div"),
finder._split_locator("xpAtH://* >> xPAth://div"),
finder._split_locator("xpath : //a >> xpath : //div"),
finder._split_locator('//*[text(), " >> css:"]'),
finder._split_locator(
[
'//*[text(), " >> css:"]',
]
),
]
verify_all("Split multi locator", results, reporter=reporter)
def test_relative_locator_split(finder: ElementFinder, reporter: GenericDiffReporterFactory):
results = [
finder._split_locator('//*[text(), " >> "] >> css:foobar >> id:tidii'),
finder._split_locator('//*[text(), " >> "] >leftof> css:foobar >rightof> id:tidii'),
finder._split_locator(
"identifier:id >> id=name >> name=id >> xpath://a >> dom=name >> link=id >> partial link=something >> "
'css=#name >> class:name >> jquery=dom.find("foobar") >> sizzle:query.find("tidii") >> '
"tag:name >> scLocator:tidii"
),
finder._split_locator(['//*[text(), " >> "]', "css:foobar", "tidii"]),
finder._split_locator("xpath://* >> xpath://div"),
finder._split_locator("xpath://* >above> xpath://div"),
finder._split_locator("xpath://* >below> xpath://div"),
finder._split_locator("xpath://* >leftof> xpath://div"),
finder._split_locator("xpath://* >rightof> xpath://div"),
finder._split_locator("xpath://* >near> xpath://div"),
finder._split_locator("xpath://* >lEftOf> xpath://div"),
finder._split_locator("xpath://* >lefto> xpath://div"),
finder._split_locator("xpath://* >unknown> xpath://div"),
finder._split_locator("xpath://* > > xpath://div"),
]
verify_all("Split multi relative locator", results, reporter=reporter)
def test_locator_split_with_non_strings(finder: ElementFinder):
assert finder._split_locator([]) == []
assert finder._split_locator(None) == [None]
locator = object
assert finder._split_locator(locator) == [locator]
def _make_mock_elements(*tags):
elements = []
for tag in tags:
element = _make_mock_element(tag)
elements.append(element)
return elements
def _make_mock_element(tag):
element = mock()
element.tag_name = tag
element.attributes = {}
def set_attribute(name, value):
element.attributes[name] = value
element.set_attribute = set_attribute
def get_attribute(name):
return element.attributes[name]
element.get_attribute = get_attribute
return element
def _get_driver(finder):
return finder.ctx.driver