forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_locator_get_by.py
213 lines (173 loc) · 8.05 KB
/
test_locator_get_by.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
# 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 re
from playwright.sync_api import Page, expect
def test_get_by_test_id(page: Page) -> None:
page.set_content("<div><div data-testid='Hello'>Hello world</div></div>")
expect(page.get_by_test_id("Hello")).to_have_text("Hello world")
expect(page.main_frame.get_by_test_id("Hello")).to_have_text("Hello world")
expect(page.locator("div").get_by_test_id("Hello")).to_have_text("Hello world")
def test_get_by_test_id_escape_id(page: Page) -> None:
page.set_content("<div><div data-testid='He\"llo'>Hello world</div></div>")
expect(page.get_by_test_id('He"llo')).to_have_text("Hello world")
def test_get_by_text(page: Page) -> None:
page.set_content("<div><div>yo</div><div>ya</div><div>\nye </div></div>")
expect(page.get_by_text("yo")).to_have_count(1)
expect(page.main_frame.get_by_text("yo")).to_have_count(1)
expect(page.locator("div").get_by_text("yo")).to_have_count(1)
assert ">\nye </div>" in page.get_by_text("ye").evaluate("e => e.outerHTML")
assert ">\nye </div>" in page.get_by_text(r"ye").evaluate("e => e.outerHTML")
page.set_content("<div> ye </div><div>ye</div>")
assert "> ye </div>" in page.get_by_text("ye", exact=True).first.evaluate(
"e => e.outerHTML"
)
page.set_content("<div>Hello world</div><div>Hello</div>")
assert (
page.get_by_text("Hello", exact=True).evaluate("e => e.outerHTML")
== "<div>Hello</div>"
)
def test_get_by_label(page: Page) -> None:
page.set_content(
"<div><label for=target>Name</label><input id=target type=text></div>"
)
expect(page.get_by_label("Name")).to_have_count(1)
expect(page.main_frame.get_by_label("Name")).to_have_count(1)
expect(page.locator("div").get_by_label("Name")).to_have_count(1)
assert page.get_by_text("Name").evaluate("e => e.nodeName") == "LABEL"
assert page.get_by_label("Name").evaluate("e => e.nodeName") == "INPUT"
assert page.main_frame.get_by_label("Name").evaluate("e => e.nodeName") == "INPUT"
assert (
page.locator("div").get_by_label("Name").evaluate("e => e.nodeName") == "INPUT"
)
def test_get_by_label_with_nested_elements(page: Page) -> None:
page.set_content(
"<label for=target>Last <span>Name</span></label><input id=target type=text>"
)
expect(page.get_by_label("last name")).to_have_attribute("id", "target")
expect(page.get_by_label("st na")).to_have_attribute("id", "target")
expect(page.get_by_label("Name")).to_have_attribute("id", "target")
expect(page.get_by_label("Last Name", exact=True)).to_have_attribute("id", "target")
expect(
page.get_by_label(re.compile(r"Last\s+name", re.IGNORECASE))
).to_have_attribute("id", "target")
expect(page.get_by_label("Last", exact=True)).to_have_count(0)
expect(page.get_by_label("last name", exact=True)).to_have_count(0)
expect(page.get_by_label("Name", exact=True)).to_have_count(0)
expect(page.get_by_label("what?")).to_have_count(0)
expect(page.get_by_label(re.compile(r"last name"))).to_have_count(0)
def test_get_by_placeholder(page: Page) -> None:
page.set_content(
"""<div>
<input placeholder="Hello">
<input placeholder="Hello World">
</div>"""
)
expect(page.get_by_placeholder("hello")).to_have_count(2)
expect(page.main_frame.get_by_placeholder("hello")).to_have_count(2)
expect(page.locator("div").get_by_placeholder("hello")).to_have_count(2)
expect(page.get_by_placeholder("hello")).to_have_count(2)
expect(page.get_by_placeholder("Hello", exact=True)).to_have_count(1)
expect(page.get_by_placeholder(re.compile(r"wor", re.IGNORECASE))).to_have_count(1)
# Coverage
expect(page.main_frame.get_by_placeholder("hello")).to_have_count(2)
expect(page.locator("div").get_by_placeholder("hello")).to_have_count(2)
def test_get_by_alt_text(page: Page) -> None:
page.set_content(
"""<div>
<input alt="Hello">
<input alt="Hello World">
</div>"""
)
expect(page.get_by_alt_text("hello")).to_have_count(2)
expect(page.main_frame.get_by_alt_text("hello")).to_have_count(2)
expect(page.locator("div").get_by_alt_text("hello")).to_have_count(2)
expect(page.get_by_alt_text("hello")).to_have_count(2)
expect(page.get_by_alt_text("Hello", exact=True)).to_have_count(1)
expect(page.get_by_alt_text(re.compile(r"wor", re.IGNORECASE))).to_have_count(1)
# Coverage
expect(page.main_frame.get_by_alt_text("hello")).to_have_count(2)
expect(page.locator("div").get_by_alt_text("hello")).to_have_count(2)
def test_get_by_title(page: Page) -> None:
page.set_content(
"""<div>
<input title="Hello">
<input title="Hello World">
</div>"""
)
expect(page.get_by_title("hello")).to_have_count(2)
expect(page.main_frame.get_by_title("hello")).to_have_count(2)
expect(page.locator("div").get_by_title("hello")).to_have_count(2)
expect(page.get_by_title("hello")).to_have_count(2)
expect(page.get_by_title("Hello", exact=True)).to_have_count(1)
expect(page.get_by_title(re.compile(r"wor", re.IGNORECASE))).to_have_count(1)
# Coverage
expect(page.main_frame.get_by_title("hello")).to_have_count(2)
expect(page.locator("div").get_by_title("hello")).to_have_count(2)
def test_get_by_escaping(page: Page) -> None:
page.set_content(
"""<label id=label for=control>Hello my
wo"rld</label><input id=control />"""
)
page.locator("input").evaluate(
"""input => {
input.setAttribute('placeholder', 'hello my\\nwo"rld');
input.setAttribute('title', 'hello my\\nwo"rld');
input.setAttribute('alt', 'hello my\\nwo"rld');
}"""
)
expect(page.get_by_text('hello my\nwo"rld')).to_have_attribute("id", "label")
expect(page.get_by_text('hello my wo"rld')).to_have_attribute(
"id", "label"
)
expect(page.get_by_label('hello my\nwo"rld')).to_have_attribute("id", "control")
expect(page.get_by_placeholder('hello my\nwo"rld')).to_have_attribute(
"id", "control"
)
expect(page.get_by_alt_text('hello my\nwo"rld')).to_have_attribute("id", "control")
expect(page.get_by_title('hello my\nwo"rld')).to_have_attribute("id", "control")
page.set_content(
"""<label id=label for=control>Hello my
world</label><input id=control />"""
)
page.locator("input").evaluate(
"""input => {
input.setAttribute('placeholder', 'hello my\\nworld');
input.setAttribute('title', 'hello my\\nworld');
input.setAttribute('alt', 'hello my\\nworld');
}"""
)
expect(page.get_by_text("hello my\nworld")).to_have_attribute("id", "label")
expect(page.get_by_text("hello my world")).to_have_attribute(
"id", "label"
)
expect(page.get_by_label("hello my\nworld")).to_have_attribute("id", "control")
expect(page.get_by_placeholder("hello my\nworld")).to_have_attribute(
"id", "control"
)
expect(page.get_by_alt_text("hello my\nworld")).to_have_attribute("id", "control")
expect(page.get_by_title("hello my\nworld")).to_have_attribute("id", "control")
def test_get_by_role(page: Page) -> None:
page.set_content(
"""
<button>Hello</button>
<button>Hel"lo</button>
<div role="dialog">I am a dialog</div>
"""
)
expect(page.get_by_role("button", name="hello")).to_have_count(1)
expect(page.get_by_role("button", name='Hel"lo')).to_have_count(1)
expect(
page.get_by_role("button", name=re.compile(r"he", re.IGNORECASE))
).to_have_count(2)
expect(page.get_by_role("dialog")).to_have_count(1)