@@ -30,7 +30,12 @@ def test_get_by_test_id_escape_id(page: Page) -> None:
3030
3131
3232def 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+
3439 assert ">\n ye </div>" in page .get_by_text ("ye" ).evaluate ("e => e.outerHTML" )
3540 assert ">\n ye </div>" in page .get_by_text (r"ye" ).evaluate ("e => e.outerHTML" )
3641
@@ -50,6 +55,11 @@ def test_get_by_label(page: Page) -> None:
5055 page .set_content (
5156 "<div><label for=target>Name</label><input id=target type=text></div>"
5257 )
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+
5363 assert page .get_by_text ("Name" ).evaluate ("e => e.nodeName" ) == "LABEL"
5464 assert page .get_by_label ("Name" ).evaluate ("e => e.nodeName" ) == "INPUT"
5565 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:
5868 )
5969
6070
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+
6191def test_get_by_placeholder (page : Page ) -> None :
6292 page .set_content (
6393 """<div>
6494 <input placeholder="Hello">
6595 <input placeholder="Hello World">
6696 </div>"""
6797 )
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+
68103 expect (page .get_by_placeholder ("hello" )).to_have_count (2 )
69104 expect (page .get_by_placeholder ("Hello" , exact = True )).to_have_count (1 )
70105 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:
81116 <input alt="Hello World">
82117 </div>"""
83118 )
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+
84124 expect (page .get_by_alt_text ("hello" )).to_have_count (2 )
85125 expect (page .get_by_alt_text ("Hello" , exact = True )).to_have_count (1 )
86126 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:
97137 <input title="Hello World">
98138 </div>"""
99139 )
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+
100145 expect (page .get_by_title ("hello" )).to_have_count (2 )
101146 expect (page .get_by_title ("Hello" , exact = True )).to_have_count (1 )
102147 expect (page .get_by_title (re .compile (r"wor" , re .IGNORECASE ))).to_have_count (1 )
103148
104149 # Coverage
105150 expect (page .main_frame .get_by_title ("hello" )).to_have_count (2 )
106151 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