How to use test_assertions_locator_to_have_count method in Playwright Python

Best Python code snippet using playwright-python

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

test_assertions.py

Source: test_assertions.py Github

copy
1# Copyright (c) Microsoft Corporation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License")
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import re
16from datetime import datetime
17
18import pytest
19
20from playwright.sync_api import Browser, Page, expect
21from tests.server import Server
22
23
24def test_assertions_page_to_have_title(page: Page, server: Server) -> None:
25    page.goto(server.EMPTY_PAGE)
26    page.set_content("<title>new title</title>")
27    expect(page).to_have_title("new title")
28    expect(page).to_have_title(re.compile("new title"))
29    with pytest.raises(AssertionError):
30        expect(page).to_have_title("not the current title", timeout=100)
31    with pytest.raises(AssertionError):
32        expect(page).to_have_title(re.compile("not the current title"), timeout=100)
33    with pytest.raises(AssertionError):
34        expect(page).not_to_have_title(re.compile("new title"), timeout=100)
35    with pytest.raises(AssertionError):
36        expect(page).not_to_have_title("new title", timeout=100)
37    expect(page).not_to_have_title("great title", timeout=100)
38    page.evaluate(
39        """
40        setTimeout(() => {
41            document.title = 'great title';
42        }, 2000);
43    """
44    )
45    expect(page).to_have_title("great title")
46    expect(page).to_have_title(re.compile("great title"))
47
48
49def test_assertions_page_to_have_url(page: Page, server: Server) -> None:
50    page.goto(server.EMPTY_PAGE)
51    expect(page).to_have_url(server.EMPTY_PAGE)
52    expect(page).to_have_url(re.compile(r".*/empty\.html"))
53    with pytest.raises(AssertionError):
54        expect(page).to_have_url("nooooo", timeout=100)
55    with pytest.raises(AssertionError):
56        expect(page).to_have_url(re.compile("not-the-url"), timeout=100)
57    page.evaluate(
58        """
59        setTimeout(() => {
60            window.location = window.location.origin + '/grid.html';
61        }, 2000);
62    """
63    )
64    expect(page).to_have_url(server.PREFIX + "/grid.html")
65    expect(page).not_to_have_url(server.EMPTY_PAGE, timeout=100)
66    with pytest.raises(AssertionError):
67        expect(page).not_to_have_url(re.compile(r".*/grid\.html"), timeout=100)
68    with pytest.raises(AssertionError):
69        expect(page).not_to_have_url(server.PREFIX + "/grid.html", timeout=100)
70    expect(page).to_have_url(re.compile(r".*/grid\.html"))
71    expect(page).not_to_have_url("**/empty.html", timeout=100)
72
73
74def test_assertions_page_to_have_url_with_base_url(
75    browser: Browser, server: Server
76) -> None:
77    page = browser.new_page(base_url=server.PREFIX)
78    page.goto("/empty.html")
79    expect(page).to_have_url("/empty.html")
80    expect(page).to_have_url(re.compile(r".*/empty\.html"))
81    page.close()
82
83
84def test_assertions_locator_to_contain_text(page: Page, server: Server) -> None:
85    page.goto(server.EMPTY_PAGE)
86    page.set_content("<div id=foobar>kek</div>")
87    expect(page.locator("div#foobar")).to_contain_text("kek")
88    expect(page.locator("div#foobar")).not_to_contain_text("bar", timeout=100)
89    with pytest.raises(AssertionError):
90        expect(page.locator("div#foobar")).to_contain_text("bar", timeout=100)
91
92    page.set_content("<div>Text \n1</div><div>Text2</div><div>Text3</div>")
93    expect(page.locator("div")).to_contain_text(["ext     1", re.compile("ext3")])
94
95
96def test_assertions_locator_to_have_attribute(page: Page, server: Server) -> None:
97    page.goto(server.EMPTY_PAGE)
98    page.set_content("<div id=foobar>kek</div>")
99    expect(page.locator("div#foobar")).to_have_attribute("id", "foobar")
100    expect(page.locator("div#foobar")).to_have_attribute("id", re.compile("foobar"))
101    expect(page.locator("div#foobar")).not_to_have_attribute("id", "kek", timeout=100)
102    with pytest.raises(AssertionError):
103        expect(page.locator("div#foobar")).to_have_attribute("id", "koko", timeout=100)
104
105
106def test_assertions_locator_to_have_class(page: Page, server: Server) -> None:
107    page.goto(server.EMPTY_PAGE)
108    page.set_content("<div class=foobar>kek</div>")
109    expect(page.locator("div.foobar")).to_have_class("foobar")
110    expect(page.locator("div.foobar")).to_have_class(["foobar"])
111    expect(page.locator("div.foobar")).to_have_class(re.compile("foobar"))
112    expect(page.locator("div.foobar")).to_have_class([re.compile("foobar")])
113    expect(page.locator("div.foobar")).not_to_have_class("kekstar", timeout=100)
114    with pytest.raises(AssertionError):
115        expect(page.locator("div.foobar")).to_have_class("oh-no", timeout=100)
116
117
118def test_assertions_locator_to_have_count(page: Page, server: Server) -> None:
119    page.goto(server.EMPTY_PAGE)
120    page.set_content("<div class=foobar>kek</div><div class=foobar>kek</div>")
121    expect(page.locator("div.foobar")).to_have_count(2)
122    expect(page.locator("div.foobar")).not_to_have_count(42, timeout=100)
123    with pytest.raises(AssertionError):
124        expect(page.locator("div.foobar")).to_have_count(42, timeout=100)
125
126
127def test_assertions_locator_to_have_css(page: Page, server: Server) -> None:
128    page.goto(server.EMPTY_PAGE)
129    page.set_content("<div class=foobar style='color: rgb(234, 74, 90);'>kek</div>")
130    expect(page.locator("div.foobar")).to_have_css("color", "rgb(234, 74, 90)")
131    expect(page.locator("div.foobar")).not_to_have_css(
132        "color", "rgb(42, 42, 42)", timeout=100
133    )
134    with pytest.raises(AssertionError):
135        expect(page.locator("div.foobar")).to_have_css(
136            "color", "rgb(42, 42, 42)", timeout=100
137        )
138
139
140def test_assertions_locator_to_have_id(page: Page, server: Server) -> None:
141    page.goto(server.EMPTY_PAGE)
142    page.set_content("<div class=foobar id=kek>kek</div>")
143    expect(page.locator("div.foobar")).to_have_id("kek")
144    expect(page.locator("div.foobar")).not_to_have_id("top", timeout=100)
145    with pytest.raises(AssertionError):
146        expect(page.locator("div.foobar")).to_have_id("top", timeout=100)
147
148
149def test_assertions_locator_to_have_js_property(page: Page, server: Server) -> None:
150    page.goto(server.EMPTY_PAGE)
151    page.set_content("<div></div>")
152    page.eval_on_selector(
153        "div", "e => e.foo = { a: 1, b: 'string', c: new Date(1627503992000) }"
154    )
155    expect(page.locator("div")).to_have_js_property(
156        "foo",
157        {"a": 1, "b": "string", "c": datetime.utcfromtimestamp(1627503992000 / 1000)},
158    )
159
160
161def test_assertions_locator_to_have_text(page: Page, server: Server) -> None:
162    page.goto(server.EMPTY_PAGE)
163    page.set_content("<div id=foobar>kek</div>")
164    expect(page.locator("div#foobar")).to_have_text("kek")
165    expect(page.locator("div#foobar")).not_to_have_text("top", timeout=100)
166
167    page.set_content("<div>Text    \n1</div><div>Text   2a</div>")
168    # Should only normalize whitespace in the first item.
169    expect(page.locator("div")).to_have_text(["Text  1", re.compile(r"Text   \d+a")])
170
171
172def test_assertions_locator_to_have_value(page: Page, server: Server) -> None:
173    page.goto(server.EMPTY_PAGE)
174    page.set_content("<input type=text id=foo>")
175    my_input = page.locator("#foo")
176    expect(my_input).to_have_value("")
177    expect(my_input).not_to_have_value("bar", timeout=100)
178    my_input.fill("kektus")
179    expect(my_input).to_have_value("kektus")
180
181
182def test_assertions_locator_to_be_checked(page: Page, server: Server) -> None:
183    page.goto(server.EMPTY_PAGE)
184    page.set_content("<input type=checkbox>")
185    my_checkbox = page.locator("input")
186    expect(my_checkbox).not_to_be_checked()
187    with pytest.raises(AssertionError):
188        expect(my_checkbox).to_be_checked(timeout=100)
189    expect(my_checkbox).to_be_checked(timeout=100, checked=False)
190    with pytest.raises(AssertionError):
191        expect(my_checkbox).to_be_checked(timeout=100, checked=True)
192    my_checkbox.check()
193    expect(my_checkbox).to_be_checked(timeout=100, checked=True)
194    with pytest.raises(AssertionError):
195        expect(my_checkbox).to_be_checked(timeout=100, checked=False)
196    expect(my_checkbox).to_be_checked()
197
198
199def test_assertions_locator_to_be_disabled_enabled(page: Page, server: Server) -> None:
200    page.goto(server.EMPTY_PAGE)
201    page.set_content("<input type=checkbox>")
202    my_checkbox = page.locator("input")
203    expect(my_checkbox).not_to_be_disabled()
204    expect(my_checkbox).to_be_enabled()
205    with pytest.raises(AssertionError):
206        expect(my_checkbox).to_be_disabled(timeout=100)
207    my_checkbox.evaluate("e => e.disabled = true")
208    expect(my_checkbox).to_be_disabled()
209    with pytest.raises(AssertionError):
210        expect(my_checkbox).to_be_enabled(timeout=100)
211
212
213def test_assertions_locator_to_be_editable(page: Page, server: Server) -> None:
214    page.goto(server.EMPTY_PAGE)
215    page.set_content("<input></input><button disabled>Text</button>")
216    expect(page.locator("button")).not_to_be_editable()
217    expect(page.locator("input")).to_be_editable()
218    with pytest.raises(AssertionError):
219        expect(page.locator("button")).to_be_editable(timeout=100)
220
221
222def test_assertions_locator_to_be_empty(page: Page, server: Server) -> None:
223    page.goto(server.EMPTY_PAGE)
224    page.set_content(
225        "<input value=text name=input1></input><input name=input2></input>"
226    )
227    expect(page.locator("input[name=input1]")).not_to_be_empty()
228    expect(page.locator("input[name=input2]")).to_be_empty()
229    with pytest.raises(AssertionError):
230        expect(page.locator("input[name=input1]")).to_be_empty(timeout=100)
231
232
233def test_assertions_locator_to_be_focused(page: Page, server: Server) -> None:
234    page.goto(server.EMPTY_PAGE)
235    page.set_content("<input type=checkbox>")
236    my_checkbox = page.locator("input")
237    with pytest.raises(AssertionError):
238        expect(my_checkbox).to_be_focused(timeout=100)
239    my_checkbox.focus()
240    expect(my_checkbox).to_be_focused()
241
242
243def test_assertions_locator_to_be_hidden_visible(page: Page, server: Server) -> None:
244    page.goto(server.EMPTY_PAGE)
245    page.set_content("<div style='width: 50px; height: 50px;'>Something</div>")
246    my_checkbox = page.locator("div")
247    expect(my_checkbox).to_be_visible()
248    with pytest.raises(AssertionError):
249        expect(my_checkbox).to_be_hidden(timeout=100)
250    my_checkbox.evaluate("e => e.style.display = 'none'")
251    expect(my_checkbox).to_be_hidden()
252    with pytest.raises(AssertionError):
253        expect(my_checkbox).to_be_visible(timeout=100)
254
255
256def test_assertions_should_serialize_regexp_correctly(
257    page: Page, server: Server
258) -> None:
259    page.goto(server.EMPTY_PAGE)
260    page.set_content("<div>iGnOrEcAsE</div>")
261    expect(page.locator("div")).to_have_text(re.compile(r"ignorecase", re.IGNORECASE))
262    page.set_content(
263        """<div>start
264some
265lines
266between
267end</div>"""
268    )
269    expect(page.locator("div")).to_have_text(re.compile(r"start.*end", re.DOTALL))
270    page.set_content(
271        """<div>line1
272line2
273line3</div>"""
274    )
275    expect(page.locator("div")).to_have_text(re.compile(r"^line2$", re.MULTILINE))
276
277
278def test_assertions_response_is_ok_pass(page: Page, server: Server) -> None:
279    response = page.request.get(server.EMPTY_PAGE)
280    expect(response).to_be_ok()
281
282
283def test_assertions_response_is_ok_pass_with_not(page: Page, server: Server) -> None:
284    response = page.request.get(server.PREFIX + "/unknown")
285    expect(response).not_to_be_ok()
286
287
288def test_assertions_response_is_ok_fail(page: Page, server: Server) -> None:
289    response = page.request.get(server.PREFIX + "/unknown")
290    with pytest.raises(AssertionError) as excinfo:
291        expect(response).to_be_ok()
292    error_message = str(excinfo.value)
293    assert ("→ GET " + server.PREFIX + "/unknown") in error_message
294    assert "← 404 Not Found" in error_message
295
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Python on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)