@@ -30,7 +30,12 @@ def test_get_by_test_id_escape_id(page: Page) -> None:
30
30
31
31
32
32
def test_get_by_text (page : Page ) -> None :
33
- page .set_content ("<div>yo</div><div>ya</div><div>\n ye </div>" )
33
+ page .set_content ("<div><div>yo</div><div>ya</div><div>\n ye </div></div>" )
34
+
35
+ expect (page .get_by_text ("yo" )).to_have_count (1 )
36
+ expect (page .main_frame .get_by_text ("yo" )).to_have_count (1 )
37
+ expect (page .locator ("div" ).get_by_text ("yo" )).to_have_count (1 )
38
+
34
39
assert ">\n ye </div>" in page .get_by_text ("ye" ).evaluate ("e => e.outerHTML" )
35
40
assert ">\n ye </div>" in page .get_by_text (r"ye" ).evaluate ("e => e.outerHTML" )
36
41
@@ -50,6 +55,11 @@ def test_get_by_label(page: Page) -> None:
50
55
page .set_content (
51
56
"<div><label for=target>Name</label><input id=target type=text></div>"
52
57
)
58
+
59
+ expect (page .get_by_label ("Name" )).to_have_count (1 )
60
+ expect (page .main_frame .get_by_label ("Name" )).to_have_count (1 )
61
+ expect (page .locator ("div" ).get_by_label ("Name" )).to_have_count (1 )
62
+
53
63
assert page .get_by_text ("Name" ).evaluate ("e => e.nodeName" ) == "LABEL"
54
64
assert page .get_by_label ("Name" ).evaluate ("e => e.nodeName" ) == "INPUT"
55
65
assert page .main_frame .get_by_label ("Name" ).evaluate ("e => e.nodeName" ) == "INPUT"
@@ -58,13 +68,38 @@ def test_get_by_label(page: Page) -> None:
58
68
)
59
69
60
70
71
+ def test_get_by_label_with_nested_elements (page : Page ) -> None :
72
+ page .set_content (
73
+ "<label for=target>Last <span>Name</span></label><input id=target type=text>"
74
+ )
75
+
76
+ expect (page .get_by_label ("last name" )).to_have_attribute ("id" , "target" )
77
+ expect (page .get_by_label ("st na" )).to_have_attribute ("id" , "target" )
78
+ expect (page .get_by_label ("Name" )).to_have_attribute ("id" , "target" )
79
+ expect (page .get_by_label ("Last Name" , exact = True )).to_have_attribute ("id" , "target" )
80
+ expect (
81
+ page .get_by_label (re .compile (r"Last\s+name" , re .IGNORECASE ))
82
+ ).to_have_attribute ("id" , "target" )
83
+
84
+ expect (page .get_by_label ("Last" , exact = True )).to_have_count (0 )
85
+ expect (page .get_by_label ("last name" , exact = True )).to_have_count (0 )
86
+ expect (page .get_by_label ("Name" , exact = True )).to_have_count (0 )
87
+ expect (page .get_by_label ("what?" )).to_have_count (0 )
88
+ expect (page .get_by_label (re .compile (r"last name" ))).to_have_count (0 )
89
+
90
+
61
91
def test_get_by_placeholder (page : Page ) -> None :
62
92
page .set_content (
63
93
"""<div>
64
94
<input placeholder="Hello">
65
95
<input placeholder="Hello World">
66
96
</div>"""
67
97
)
98
+
99
+ expect (page .get_by_placeholder ("hello" )).to_have_count (2 )
100
+ expect (page .main_frame .get_by_placeholder ("hello" )).to_have_count (2 )
101
+ expect (page .locator ("div" ).get_by_placeholder ("hello" )).to_have_count (2 )
102
+
68
103
expect (page .get_by_placeholder ("hello" )).to_have_count (2 )
69
104
expect (page .get_by_placeholder ("Hello" , exact = True )).to_have_count (1 )
70
105
expect (page .get_by_placeholder (re .compile (r"wor" , re .IGNORECASE ))).to_have_count (1 )
@@ -81,6 +116,11 @@ def test_get_by_alt_text(page: Page) -> None:
81
116
<input alt="Hello World">
82
117
</div>"""
83
118
)
119
+
120
+ expect (page .get_by_alt_text ("hello" )).to_have_count (2 )
121
+ expect (page .main_frame .get_by_alt_text ("hello" )).to_have_count (2 )
122
+ expect (page .locator ("div" ).get_by_alt_text ("hello" )).to_have_count (2 )
123
+
84
124
expect (page .get_by_alt_text ("hello" )).to_have_count (2 )
85
125
expect (page .get_by_alt_text ("Hello" , exact = True )).to_have_count (1 )
86
126
expect (page .get_by_alt_text (re .compile (r"wor" , re .IGNORECASE ))).to_have_count (1 )
@@ -97,10 +137,51 @@ def test_get_by_title(page: Page) -> None:
97
137
<input title="Hello World">
98
138
</div>"""
99
139
)
140
+
141
+ expect (page .get_by_title ("hello" )).to_have_count (2 )
142
+ expect (page .main_frame .get_by_title ("hello" )).to_have_count (2 )
143
+ expect (page .locator ("div" ).get_by_title ("hello" )).to_have_count (2 )
144
+
100
145
expect (page .get_by_title ("hello" )).to_have_count (2 )
101
146
expect (page .get_by_title ("Hello" , exact = True )).to_have_count (1 )
102
147
expect (page .get_by_title (re .compile (r"wor" , re .IGNORECASE ))).to_have_count (1 )
103
148
104
149
# Coverage
105
150
expect (page .main_frame .get_by_title ("hello" )).to_have_count (2 )
106
151
expect (page .locator ("div" ).get_by_title ("hello" )).to_have_count (2 )
152
+
153
+
154
+ def test_get_by_escaping (page : Page ) -> None :
155
+ page .set_content (
156
+ """<label id=label for=control>Hello
157
+ wo"rld</label><input id=control />"""
158
+ )
159
+ page .locator ("input" ).evaluate (
160
+ """input => {
161
+ input.setAttribute('placeholder', 'hello\\ nwo"rld');
162
+ input.setAttribute('title', 'hello\\ nwo"rld');
163
+ input.setAttribute('alt', 'hello\\ nwo"rld');
164
+ }"""
165
+ )
166
+ expect (page .get_by_text ('hello\n wo"rld' )).to_have_attribute ("id" , "label" )
167
+ expect (page .get_by_label ('hello\n wo"rld' )).to_have_attribute ("id" , "control" )
168
+ expect (page .get_by_placeholder ('hello\n wo"rld' )).to_have_attribute ("id" , "control" )
169
+ expect (page .get_by_alt_text ('hello\n wo"rld' )).to_have_attribute ("id" , "control" )
170
+ expect (page .get_by_title ('hello\n wo"rld' )).to_have_attribute ("id" , "control" )
171
+
172
+ page .set_content (
173
+ """<label id=label for=control>Hello
174
+ world</label><input id=control />"""
175
+ )
176
+ page .locator ("input" ).evaluate (
177
+ """input => {
178
+ input.setAttribute('placeholder', 'hello\\ nworld');
179
+ input.setAttribute('title', 'hello\\ nworld');
180
+ input.setAttribute('alt', 'hello\\ nworld');
181
+ }"""
182
+ )
183
+ expect (page .get_by_text ("hello\n world" )).to_have_attribute ("id" , "label" )
184
+ expect (page .get_by_label ("hello\n world" )).to_have_attribute ("id" , "control" )
185
+ expect (page .get_by_placeholder ("hello\n world" )).to_have_attribute ("id" , "control" )
186
+ expect (page .get_by_alt_text ("hello\n world" )).to_have_attribute ("id" , "control" )
187
+ expect (page .get_by_title ("hello\n world" )).to_have_attribute ("id" , "control" )
0 commit comments