forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_locators.py
920 lines (748 loc) · 30.3 KB
/
test_locators.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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
# Copyright (c) Microsoft Corporation.
#
# Licensed 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.
import os
import re
from typing import Callable
from urllib.parse import urlparse
import pytest
from playwright._impl._path_utils import get_file_dirname
from playwright.sync_api import Error, Page, expect
from tests.server import Server
_dirname = get_file_dirname()
FILE_TO_UPLOAD = _dirname / ".." / "assets/file-to-upload.txt"
def test_locators_click_should_work(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/input/button.html")
button = page.locator("button")
button.click()
assert page.evaluate("window['result']") == "Clicked"
def test_locators_click_should_work_with_node_removed(
page: Page, server: Server
) -> None:
page.goto(server.PREFIX + "/input/button.html")
page.evaluate("delete window['Node']")
button = page.locator("button")
button.click()
assert page.evaluate("window['result']") == "Clicked"
def test_locators_click_should_work_for_text_nodes(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/input/button.html")
page.evaluate(
"""() => {
window['double'] = false;
const button = document.querySelector('button');
button.addEventListener('dblclick', event => {
window['double'] = true;
});
}"""
)
button = page.locator("button")
button.dblclick()
assert page.evaluate("double") is True
assert page.evaluate("result") == "Clicked"
def test_locators_should_have_repr(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/input/button.html")
button = page.locator("button")
button.click()
assert (
str(button)
== f"<Locator frame=<Frame name= url='{server.PREFIX}/input/button.html'> selector='button'>"
)
def test_locators_get_attribute_should_work(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/dom.html")
button = page.locator("#outer")
assert button.get_attribute("name") == "value"
assert button.get_attribute("foo") is None
def test_locators_input_value_should_work(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/dom.html")
page.fill("#textarea", "input value")
text_area = page.locator("#textarea")
assert text_area.input_value() == "input value"
def test_locators_inner_html_should_work(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/dom.html")
locator = page.locator("#outer")
assert locator.inner_html() == '<div id="inner">Text,\nmore text</div>'
def test_locators_inner_text_should_work(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/dom.html")
locator = page.locator("#inner")
assert locator.inner_text() == "Text, more text"
def test_locators_text_content_should_work(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/dom.html")
locator = page.locator("#inner")
assert locator.text_content() == "Text,\nmore text"
def test_locators_is_hidden_and_is_visible_should_work(page: Page) -> None:
page.set_content("<div>Hi</div><span></span>")
div = page.locator("div")
assert div.is_visible() is True
assert div.is_hidden() is False
span = page.locator("span")
assert span.is_visible() is False
assert span.is_hidden() is True
def test_locators_is_enabled_and_is_disabled_should_work(page: Page) -> None:
page.set_content(
"""
<button disabled>button1</button>
<button>button2</button>
<div>div</div>
"""
)
div = page.locator("div")
assert div.is_enabled() is True
assert div.is_disabled() is False
button1 = page.locator(':text("button1")')
assert button1.is_enabled() is False
assert button1.is_disabled() is True
button1 = page.locator(':text("button2")')
assert button1.is_enabled() is True
assert button1.is_disabled() is False
def test_locators_is_editable_should_work(page: Page) -> None:
page.set_content(
"""
<input id=input1 disabled><textarea></textarea><input id=input2>
"""
)
input1 = page.locator("#input1")
assert input1.is_editable() is False
input2 = page.locator("#input2")
assert input2.is_editable() is True
def test_locators_is_checked_should_work(page: Page) -> None:
page.set_content(
"""
<input type='checkbox' checked><div>Not a checkbox</div>
"""
)
element = page.locator("input")
assert element.is_checked() is True
element.evaluate("e => e.checked = false")
assert element.is_checked() is False
def test_locators_all_text_contents_should_work(page: Page) -> None:
page.set_content(
"""
<div>A</div><div>B</div><div>C</div>
"""
)
element = page.locator("div")
assert element.all_text_contents() == ["A", "B", "C"]
def test_locators_all_inner_texts(page: Page) -> None:
page.set_content(
"""
<div>A</div><div>B</div><div>C</div>
"""
)
element = page.locator("div")
assert element.all_inner_texts() == ["A", "B", "C"]
def test_locators_should_query_existing_element(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/playground.html")
page.set_content(
"""<html><body><div class="second"><div class="inner">A</div></div></body></html>"""
)
html = page.locator("html")
second = html.locator(".second")
inner = second.locator(".inner")
assert page.evaluate("e => e.textContent", inner.element_handle()) == "A"
def test_locators_evaluate_handle_should_work(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/dom.html")
outer = page.locator("#outer")
inner = outer.locator("#inner")
check = inner.locator("#check")
text = inner.evaluate_handle("e => e.firstChild")
page.evaluate("1 + 1")
assert (
str(outer)
== f"<Locator frame=<Frame name= url='{server.PREFIX}/dom.html'> selector='#outer'>"
)
assert (
str(inner)
== f"<Locator frame=<Frame name= url='{server.PREFIX}/dom.html'> selector='#outer >> #inner'>"
)
assert str(text) == "JSHandle@#text=Text,↵more text"
assert (
str(check)
== f"<Locator frame=<Frame name= url='{server.PREFIX}/dom.html'> selector='#outer >> #inner >> #check'>"
)
def test_locators_should_query_existing_elements(page: Page) -> None:
page.set_content("""<html><body><div>A</div><br/><div>B</div></body></html>""")
html = page.locator("html")
elements = html.locator("div").element_handles()
assert len(elements) == 2
result = []
for element in elements:
result.append(page.evaluate("e => e.textContent", element))
assert result == ["A", "B"]
def test_locators_return_empty_array_for_non_existing_elements(page: Page) -> None:
page.set_content("""<html><body><div>A</div><br/><div>B</div></body></html>""")
html = page.locator("html")
elements = html.locator("abc").element_handles()
assert len(elements) == 0
assert elements == []
def test_locators_evaluate_all_should_work(page: Page) -> None:
page.set_content(
"""<html><body><div class="tweet"><div class="like">100</div><div class="like">10</div></div></body></html>"""
)
tweet = page.locator(".tweet .like")
content = tweet.evaluate_all("nodes => nodes.map(n => n.innerText)")
assert content == ["100", "10"]
def test_locators_evaluate_all_should_work_with_missing_selector(page: Page) -> None:
page.set_content("""<div class="a">not-a-child-div</div><div id="myId"></div""")
tweet = page.locator("#myId .a")
nodes_length = tweet.evaluate_all("nodes => nodes.length")
assert nodes_length == 0
def test_locators_hover_should_work(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/input/scrollable.html")
button = page.locator("#button-6")
button.hover()
assert page.evaluate("document.querySelector('button:hover').id") == "button-6"
def test_locators_fill_should_work(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/input/textarea.html")
button = page.locator("input")
button.fill("some value")
assert page.evaluate("result") == "some value"
def test_locators_clear_should_work(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/input/textarea.html")
button = page.locator("input")
button.fill("some value")
assert page.evaluate("result") == "some value"
button.clear()
assert page.evaluate("result") == ""
def test_locators_check_should_work(page: Page) -> None:
page.set_content("<input id='checkbox' type='checkbox'></input>")
button = page.locator("input")
button.check()
assert page.evaluate("checkbox.checked") is True
def test_locators_uncheck_should_work(page: Page) -> None:
page.set_content("<input id='checkbox' type='checkbox' checked></input>")
button = page.locator("input")
button.uncheck()
assert page.evaluate("checkbox.checked") is False
def test_locators_select_option_should_work(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/input/select.html")
select = page.locator("select")
select.select_option("blue")
assert page.evaluate("result.onInput") == ["blue"]
assert page.evaluate("result.onChange") == ["blue"]
def test_locators_focus_should_work(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/input/button.html")
button = page.locator("button")
assert button.evaluate("button => document.activeElement === button") is False
button.focus()
assert button.evaluate("button => document.activeElement === button") is True
def test_locators_dispatch_event_should_work(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/input/button.html")
button = page.locator("button")
button.dispatch_event("click")
assert page.evaluate("result") == "Clicked"
def test_locators_should_upload_a_file(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/input/fileupload.html")
input = page.locator("input[type=file]")
file_path = os.path.relpath(FILE_TO_UPLOAD, os.getcwd())
input.set_input_files(file_path)
assert (
page.evaluate("e => e.files[0].name", input.element_handle())
== "file-to-upload.txt"
)
def test_locators_should_press(page: Page) -> None:
page.set_content("<input type='text' />")
page.locator("input").press("h")
assert page.eval_on_selector("input", "input => input.value") == "h"
def test_locators_should_scroll_into_view(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/offscreenbuttons.html")
for i in range(11):
button = page.locator(f"#btn{i}")
before = button.evaluate(
"button => button.getBoundingClientRect().right - window.innerWidth"
)
assert before == 10 * i
button.scroll_into_view_if_needed()
after = button.evaluate(
"button => button.getBoundingClientRect().right - window.innerWidth"
)
assert after <= 0
page.evaluate("window.scrollTo(0, 0)")
def test_locators_should_select_textarea(
page: Page, server: Server, browser_name: str
) -> None:
page.goto(server.PREFIX + "/input/textarea.html")
textarea = page.locator("textarea")
textarea.evaluate("textarea => textarea.value = 'some value'")
textarea.select_text()
textarea.select_text(timeout=1_000)
if browser_name == "firefox" or browser_name == "webkit":
assert textarea.evaluate("el => el.selectionStart") == 0
assert textarea.evaluate("el => el.selectionEnd") == 10
else:
assert page.evaluate("window.getSelection().toString()") == "some value"
def test_locators_should_type(page: Page) -> None:
page.set_content("<input type='text' />")
page.locator("input").type("hello")
assert page.eval_on_selector("input", "input => input.value") == "hello"
def test_locators_should_screenshot(
page: Page, server: Server, assert_to_be_golden: Callable[[bytes, str], None]
) -> None:
page.set_viewport_size(
{
"width": 500,
"height": 500,
}
)
page.goto(server.PREFIX + "/grid.html")
page.evaluate("window.scrollBy(50, 100)")
element = page.locator(".box:nth-of-type(3)")
assert_to_be_golden(element.screenshot(), "screenshot-element-bounding-box.png")
assert_to_be_golden(
element.screenshot(timeout=1_000), "screenshot-element-bounding-box.png"
)
def test_locators_should_return_bounding_box(page: Page, server: Server) -> None:
page.set_viewport_size(
{
"width": 500,
"height": 500,
}
)
page.goto(server.PREFIX + "/grid.html")
element = page.locator(".box:nth-of-type(13)")
box = element.bounding_box()
assert box == {
"x": 100,
"y": 50,
"width": 50,
"height": 50,
}
def test_locators_should_respect_first_and_last(page: Page) -> None:
page.set_content(
"""
<section>
<div><p>A</p></div>
<div><p>A</p><p>A</p></div>
<div><p>A</p><p>A</p><p>A</p></div>
</section>"""
)
assert page.locator("div >> p").count() == 6
assert page.locator("div").locator("p").count() == 6
assert page.locator("div").first.locator("p").count() == 1
assert page.locator("div").last.locator("p").count() == 3
def test_locators_should_respect_nth(page: Page) -> None:
page.set_content(
"""
<section>
<div><p>A</p></div>
<div><p>A</p><p>A</p></div>
<div><p>A</p><p>A</p><p>A</p></div>
</section>"""
)
assert page.locator("div >> p").nth(0).count() == 1
assert page.locator("div").nth(1).locator("p").count() == 2
assert page.locator("div").nth(2).locator("p").count() == 3
def test_locators_should_throw_on_capture_without_nth(page: Page) -> None:
page.set_content(
"""
<section><div><p>A</p></div></section>
"""
)
with pytest.raises(Error, match="Can't query n-th element"):
page.locator("*css=div >> p").nth(1).click()
def test_locators_should_throw_due_to_strictness(page: Page) -> None:
page.set_content(
"""
<div>A</div><div>B</div>
"""
)
with pytest.raises(Error, match="strict mode violation"):
page.locator("div").is_visible()
def test_locators_should_throw_due_to_strictness_2(page: Page) -> None:
page.set_content(
"""
<select><option>One</option><option>Two</option></select>
"""
)
with pytest.raises(Error, match="strict mode violation"):
page.locator("option").evaluate("e => {}")
def test_locators_set_checked(page: Page) -> None:
page.set_content("`<input id='checkbox' type='checkbox'></input>`")
locator = page.locator("input")
locator.set_checked(True)
assert page.evaluate("checkbox.checked")
locator.set_checked(False)
assert page.evaluate("checkbox.checked") is False
def test_should_combine_visible_with_other_selectors(page: Page) -> None:
page.set_content(
"""<div>
<div class="item" style="display: none">Hidden data0</div>
<div class="item">visible data1</div>
<div class="item" style="display: none">Hidden data1</div>
<div class="item">visible data2</div>
<div class="item" style="display: none">Hidden data1</div>
<div class="item">visible data3</div>
</div>
"""
)
locator = page.locator(".item >> visible=true").nth(1)
expect(locator).to_have_text("visible data2")
expect(page.locator(".item >> visible=true >> text=data3")).to_have_text(
"visible data3"
)
def test_locator_count_should_work_with_deleted_map_in_main_world(page: Page) -> None:
page.evaluate("Map = 1")
page.locator("#searchResultTableDiv .x-grid3-row").count()
expect(page.locator("#searchResultTableDiv .x-grid3-row")).to_have_count(0)
def test_locator_locator_and_framelocator_locator_should_accept_locator(
page: Page,
) -> None:
page.set_content(
"""
<div><input value=outer></div>
<iframe srcdoc="<div><input value=inner></div>"></iframe>
"""
)
input_locator = page.locator("input")
assert input_locator.input_value() == "outer"
assert page.locator("div").locator(input_locator).input_value() == "outer"
assert page.frame_locator("iframe").locator(input_locator).input_value() == "inner"
assert (
page.frame_locator("iframe").locator("div").locator(input_locator).input_value()
== "inner"
)
div_locator = page.locator("div")
assert div_locator.locator("input").input_value() == "outer"
assert (
page.frame_locator("iframe").locator(div_locator).locator("input").input_value()
== "inner"
)
def route_iframe(page: Page) -> None:
page.route(
"**/empty.html",
lambda route: route.fulfill(
body='<iframe src="iframe.html"></iframe>', content_type="text/html"
),
)
page.route(
"**/iframe.html",
lambda route: route.fulfill(
body="""<html>
<div>
<button>Hello iframe</button>
<iframe src="iframe-2.html"></iframe>
</div>
<span>1</span>
<span>2</span>
</html>""",
content_type="text/html",
),
)
page.route(
"**/iframe-2.html",
lambda route: route.fulfill(
body="<html><button>Hello nested iframe</button></html>",
content_type="text/html",
),
)
def test_locators_frame_should_work_with_iframe(page: Page, server: Server) -> None:
route_iframe(page)
page.goto(server.EMPTY_PAGE)
button = page.frame_locator("iframe").locator("button")
button.wait_for()
assert button.inner_text() == "Hello iframe"
button.click()
assert (
repr(page.frame_locator("iframe"))
== f"<FrameLocator frame=<Frame name= url='{server.PREFIX}/empty.html'> selector='iframe'>"
)
def test_locators_frame_should_work_for_nested_iframe(
page: Page, server: Server
) -> None:
route_iframe(page)
page.goto(server.EMPTY_PAGE)
button = page.frame_locator("iframe").frame_locator("iframe").locator("button")
button.wait_for()
assert button.inner_text() == "Hello nested iframe"
button.click()
def test_locators_frame_should_work_with_locator_frame_locator(
page: Page, server: Server
) -> None:
route_iframe(page)
page.goto(server.EMPTY_PAGE)
button = page.locator("body").frame_locator("iframe").locator("button")
button.wait_for()
assert button.inner_text() == "Hello iframe"
button.click()
def route_ambiguous(page: Page) -> None:
page.route(
"**/empty.html",
lambda route: route.fulfill(
body="""
<iframe src="iframe-1.html"></iframe>
<iframe src="iframe-2.html"></iframe>
<iframe src="iframe-3.html"></iframe>
""",
content_type="text/html",
),
)
page.route(
"**/iframe-*",
lambda route: route.fulfill(
body=f"<html><button>Hello from {urlparse(route.request.url).path[1:]}</button></html>",
content_type="text/html",
),
)
def test_locator_frame_locator_should_throw_on_ambiguity(
page: Page, server: Server
) -> None:
route_ambiguous(page)
page.goto(server.EMPTY_PAGE)
button = page.locator("body").frame_locator("iframe").locator("button")
with pytest.raises(
Error,
match=r'.*strict mode violation: locator\("body"\)\.locator\("iframe"\) resolved to 3 elements.*',
):
button.wait_for()
def test_locator_frame_locator_should_not_throw_on_first_last_nth(
page: Page, server: Server
) -> None:
route_ambiguous(page)
page.goto(server.EMPTY_PAGE)
button1 = page.locator("body").frame_locator("iframe").first.locator("button")
assert button1.text_content() == "Hello from iframe-1.html"
button2 = page.locator("body").frame_locator("iframe").nth(1).locator("button")
assert button2.text_content() == "Hello from iframe-2.html"
button3 = page.locator("body").frame_locator("iframe").last.locator("button")
assert button3.text_content() == "Hello from iframe-3.html"
def test_drag_to(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/drag-n-drop.html")
page.locator("#source").drag_to(page.locator("#target"))
assert (
page.eval_on_selector(
"#target", "target => target.contains(document.querySelector('#source'))"
)
is True
)
def test_locator_query_should_filter_by_text(page: Page, server: Server) -> None:
page.set_content("<div>Foobar</div><div>Bar</div>")
expect(page.locator("div", has_text="Foo")).to_have_text("Foobar")
def test_locator_query_should_filter_by_text_2(page: Page, server: Server) -> None:
page.set_content("<div>foo <span>hello world</span> bar</div>")
expect(page.locator("div", has_text="hello world")).to_have_text(
"foo hello world bar"
)
def test_locator_query_should_filter_by_regex(page: Page, server: Server) -> None:
page.set_content("<div>Foobar</div><div>Bar</div>")
expect(page.locator("div", has_text=re.compile(r"Foo.*"))).to_have_text("Foobar")
def test_locator_query_should_filter_by_text_with_quotes(
page: Page, server: Server
) -> None:
page.set_content('<div>Hello "world"</div><div>Hello world</div>')
expect(page.locator("div", has_text='Hello "world"')).to_have_text('Hello "world"')
def test_locator_query_should_filter_by_regex_with_quotes(
page: Page, server: Server
) -> None:
page.set_content('<div>Hello "world"</div><div>Hello world</div>')
expect(page.locator("div", has_text=re.compile('Hello "world"'))).to_have_text(
'Hello "world"'
)
def test_locator_query_should_filter_by_regex_and_regexp_flags(
page: Page, server: Server
) -> None:
page.set_content('<div>Hello "world"</div><div>Hello world</div>')
expect(
page.locator("div", has_text=re.compile('hElLo "world', re.IGNORECASE))
).to_have_text('Hello "world"')
def test_locator_should_return_page(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/frames/two-frames.html")
outer = page.locator("#outer")
assert outer.page == page
inner = outer.locator("#inner")
assert inner.page == page
in_frame = page.frames[1].locator("div")
assert in_frame.page == page
def test_locator_should_support_has_locator(page: Page, server: Server) -> None:
page.set_content("<div><span>hello</span></div><div><span>world</span></div>")
expect(page.locator("div", has=page.locator("text=world"))).to_have_count(1)
assert (
page.locator("div", has=page.locator("text=world")).evaluate("e => e.outerHTML")
== "<div><span>world</span></div>"
)
expect(page.locator("div", has=page.locator('text="hello"'))).to_have_count(1)
assert (
page.locator("div", has=page.locator('text="hello"')).evaluate(
"e => e.outerHTML"
)
== "<div><span>hello</span></div>"
)
expect(page.locator("div", has=page.locator("xpath=./span"))).to_have_count(2)
expect(page.locator("div", has=page.locator("span"))).to_have_count(2)
expect(page.locator("div", has=page.locator("span", has_text="wor"))).to_have_count(
1
)
assert (
page.locator("div", has=page.locator("span", has_text="wor")).evaluate(
"e => e.outerHTML"
)
== "<div><span>world</span></div>"
)
expect(
page.locator(
"div",
has=page.locator("span"),
has_text="wor",
)
).to_have_count(1)
def test_locator_should_enforce_same_frame_for_has_locator(
page: Page, server: Server
) -> None:
page.goto(server.PREFIX + "/frames/two-frames.html")
child = page.frames[1]
with pytest.raises(Error) as exc_info:
page.locator("div", has=child.locator("span"))
assert (
'Inner "has" locator must belong to the same frame.' in exc_info.value.message
)
def test_locator_should_support_locator_or(page: Page, server: Server) -> None:
page.set_content("<div>hello</div><span>world</span>")
expect(page.locator("div").or_(page.locator("span"))).to_have_count(2)
expect(page.locator("div").or_(page.locator("span"))).to_have_text(
["hello", "world"]
)
expect(
page.locator("span").or_(page.locator("article")).or_(page.locator("div"))
).to_have_text(["hello", "world"])
expect(page.locator("article").or_(page.locator("someting"))).to_have_count(0)
expect(page.locator("article").or_(page.locator("div"))).to_have_text("hello")
expect(page.locator("article").or_(page.locator("span"))).to_have_text("world")
expect(page.locator("div").or_(page.locator("article"))).to_have_text("hello")
expect(page.locator("span").or_(page.locator("article"))).to_have_text("world")
def test_locator_highlight_should_work(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/grid.html")
page.locator(".box").nth(3).highlight()
assert page.locator("x-pw-glass").is_visible()
def test_should_support_locator_that(page: Page) -> None:
page.set_content(
"<section><div><span>hello</span></div><div><span>world</span></div></section>"
)
expect(page.locator("div").filter(has_text="hello")).to_have_count(1)
expect(
page.locator("div", has_text="hello").filter(has_text="hello")
).to_have_count(1)
expect(
page.locator("div", has_text="hello").filter(has_text="world")
).to_have_count(0)
expect(
page.locator("section", has_text="hello").filter(has_text="world")
).to_have_count(1)
expect(page.locator("div").filter(has_text="hello").locator("span")).to_have_count(
1
)
expect(
page.locator("div").filter(has=page.locator("span", has_text="world"))
).to_have_count(1)
expect(page.locator("div").filter(has=page.locator("span"))).to_have_count(2)
expect(
page.locator("div").filter(
has=page.locator("span"),
has_text="world",
)
).to_have_count(1)
def test_should_filter_by_case_insensitive_regex_in_a_child(page: Page) -> None:
page.set_content('<div class="test"><h5>Title Text</h5></div>')
expect(
page.locator("div", has_text=re.compile(r"^title text$", re.I))
).to_have_text("Title Text")
def test_should_filter_by_case_insensitive_regex_in_multiple_children(
page: Page,
) -> None:
page.set_content('<div class="test"><h5>Title</h5> <h2><i>Text</i></h2></div>')
expect(
page.locator("div", has_text=re.compile(r"^title text$", re.I))
).to_have_class("test")
def test_should_filter_by_regex_with_special_symbols(page: Page) -> None:
page.set_content(
'<div class="test"><h5>First/"and"</h5><h2><i>Second\\</i></h2></div>'
)
expect(
page.locator("div", has_text=re.compile(r'^first\/".*"second\\$', re.S | re.I))
).to_have_class("test")
def test_should_support_locator_filter(page: Page) -> None:
page.set_content(
"<section><div><span>hello</span></div><div><span>world</span></div></section>"
)
expect(page.locator("div").filter(has_text="hello")).to_have_count(1)
expect(
page.locator("div", has_text="hello").filter(has_text="hello")
).to_have_count(1)
expect(
page.locator("div", has_text="hello").filter(has_text="world")
).to_have_count(0)
expect(
page.locator("section", has_text="hello").filter(has_text="world")
).to_have_count(1)
expect(page.locator("div").filter(has_text="hello").locator("span")).to_have_count(
1
)
expect(
page.locator("div").filter(has=page.locator("span", has_text="world"))
).to_have_count(1)
expect(page.locator("div").filter(has=page.locator("span"))).to_have_count(2)
expect(
page.locator("div").filter(
has=page.locator("span"),
has_text="world",
)
).to_have_count(1)
expect(
page.locator("div").filter(has_not=page.locator("span", has_text="world"))
).to_have_count(1)
expect(page.locator("div").filter(has_not=page.locator("section"))).to_have_count(2)
expect(page.locator("div").filter(has_not=page.locator("span"))).to_have_count(0)
expect(page.locator("div").filter(has_not_text="hello")).to_have_count(1)
expect(page.locator("div").filter(has_not_text="foo")).to_have_count(2)
def test_locators_has_does_not_encode_unicode(page: Page, server: Server) -> None:
page.goto(server.EMPTY_PAGE)
locators = [
page.locator("button", has_text="Драматург"),
page.locator("button", has_text=re.compile("Драматург")),
page.locator("button", has=page.locator("text=Драматург")),
]
for locator in locators:
with pytest.raises(Error) as exc_info:
locator.click(timeout=1_000)
assert "Драматург" in exc_info.value.message
def test_locators_should_focus_and_blur_a_button(page: Page, server: Server) -> None:
page.goto(server.PREFIX + "/input/button.html")
button = page.locator("button")
assert not button.evaluate("button => document.activeElement === button")
focused = False
blurred = False
def focus_event() -> None:
nonlocal focused
focused = True
def blur_event() -> None:
nonlocal blurred
blurred = True
page.expose_function("focusEvent", focus_event)
page.expose_function("blurEvent", blur_event)
button.evaluate(
"""button => {
button.addEventListener('focus', window['focusEvent']);
button.addEventListener('blur', window['blurEvent']);
}"""
)
button.focus()
assert focused
assert not blurred
assert button.evaluate("button => document.activeElement === button")
button.blur()
assert focused
assert blurred
assert not button.evaluate("button => document.activeElement === button")
def test_locator_all_should_work(page: Page) -> None:
page.set_content("<div><p>A</p><p>B</p><p>C</p></div>")
texts = []
for p in page.locator("p").all():
texts.append(p.text_content())
assert texts == ["A", "B", "C"]