Skip to content

Commit 11f6b1c

Browse files
committed
Testing new implementations
1 parent b7f89a1 commit 11f6b1c

File tree

7 files changed

+83
-4
lines changed

7 files changed

+83
-4
lines changed

playwright/_impl/_assertions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,15 @@ async def to_contain_class(
309309
timeout: float = None,
310310
) -> None:
311311
__tracebackhide__ = True
312+
if isinstance(expected, collections.abc.Sequence) and not isinstance(
313+
expected, str
314+
):
315+
expected_text = to_expected_text_values(expected)
316+
else:
317+
expected_text = to_expected_text_values([expected])
312318
await self._expect_impl(
313319
"to.contain.class",
314-
FrameExpectOptions(expectedValue=expected, timeout=timeout),
320+
FrameExpectOptions(expectedText=expected_text, timeout=timeout),
315321
expected,
316322
"Locator expected to contain class",
317323
)

tests/async/test_assertions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ async def test_assertions_locator_to_have_class(page: Page, server: Server) -> N
145145
await expect(page.locator("div.foobar")).to_have_class("oh-no", timeout=100)
146146

147147

148+
async def test_assertions_locator_to_contain_class(page: Page, server: Server) -> None:
149+
await page.goto(server.EMPTY_PAGE)
150+
await page.set_content("<div class='foo bar another foobar'>kek</div>")
151+
await expect(page.locator("div.foobar")).to_contain_class("foobar")
152+
await expect(page.locator("div.foobar")).to_contain_class(["another foobar"])
153+
await expect(page.locator("div.foobar")).not_to_contain_class(
154+
"kekstar", timeout=100
155+
)
156+
with pytest.raises(AssertionError):
157+
await expect(page.locator("div.foobar")).to_contain_class("oh-no", timeout=100)
158+
159+
148160
async def test_assertions_locator_to_have_count(page: Page, server: Server) -> None:
149161
await page.goto(server.EMPTY_PAGE)
150162
await page.set_content("<div class=foobar>kek</div><div class=foobar>kek</div>")

tests/async/test_fetch_global.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,23 @@ async def test_should_not_throw_when_fail_on_status_code_is_false(
524524
response = await request.fetch(server.EMPTY_PAGE)
525525
assert response.status == 404
526526
await request.dispose()
527+
528+
529+
async def test_should_follow_max_redirects(
530+
playwright: Playwright, server: Server
531+
) -> None:
532+
redirect_count = 0
533+
534+
def _handle_request(req: TestServerRequest) -> None:
535+
nonlocal redirect_count
536+
redirect_count += 1
537+
req.setResponseCode(301),
538+
req.setHeader("Location", server.EMPTY_PAGE),
539+
req.finish(),
540+
541+
server.set_route("/empty.html", _handle_request)
542+
request = await playwright.request.new_context(max_redirects=1)
543+
with pytest.raises(Error, match="Max redirect count exceeded"):
544+
await request.fetch(server.EMPTY_PAGE)
545+
assert redirect_count == 2
546+
await request.dispose()

tests/async/test_page_aria_snapshot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ async def test_should_snapshot_complex(page: Page) -> None:
8888
"""
8989
- list:
9090
- listitem:
91-
- link "link"
91+
- link "link":
92+
- /url: about:blank
9293
""",
9394
)
9495

tests/sync/test_assertions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ def test_assertions_locator_to_have_class(page: Page, server: Server) -> None:
124124
expect(page.locator("div.foobar")).to_have_class("oh-no", timeout=100)
125125

126126

127+
def test_assertions_locator_to_contain_class(page: Page, server: Server) -> None:
128+
page.goto(server.EMPTY_PAGE)
129+
page.set_content("<div class='foo bar another foobar'>kek</div>")
130+
expect(page.locator("div.foobar")).to_contain_class("foobar")
131+
expect(page.locator("div.foobar")).to_contain_class(["another foobar"])
132+
expect(page.locator("div.foobar")).not_to_contain_class("kekstar", timeout=100)
133+
with pytest.raises(AssertionError):
134+
expect(page.locator("div.foobar")).to_contain_class("oh-no", timeout=100)
135+
136+
127137
def test_assertions_locator_to_have_count(page: Page, server: Server) -> None:
128138
page.goto(server.EMPTY_PAGE)
129139
page.set_content("<div class=foobar>kek</div><div class=foobar>kek</div>")

tests/sync/test_fetch_global.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import pytest
2020

2121
from playwright.sync_api import APIResponse, Error, Playwright, StorageState
22-
from tests.server import Server
22+
from tests.server import Server, TestServerRequest
2323

2424

2525
@pytest.mark.parametrize(
@@ -361,3 +361,21 @@ def test_should_not_throw_when_fail_on_status_code_is_false(
361361
response = request.fetch(server.EMPTY_PAGE)
362362
assert response.status == 404
363363
request.dispose()
364+
365+
366+
def test_should_follow_max_redirects(playwright: Playwright, server: Server) -> None:
367+
redirect_count = 0
368+
369+
def _handle_request(req: TestServerRequest) -> None:
370+
nonlocal redirect_count
371+
redirect_count += 1
372+
req.setResponseCode(301),
373+
req.setHeader("Location", server.EMPTY_PAGE),
374+
req.finish(),
375+
376+
server.set_route("/empty.html", _handle_request)
377+
request = playwright.request.new_context(max_redirects=1)
378+
with pytest.raises(Error, match="Max redirect count exceeded"):
379+
request.fetch(server.EMPTY_PAGE)
380+
assert redirect_count == 2
381+
request.dispose()

tests/sync/test_page_aria_snapshot.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ def test_should_snapshot_complex(page: Page) -> None:
8888
"""
8989
- list:
9090
- listitem:
91-
- link "link"
91+
- link "link":
92+
- /url: about:blank
9293
""",
9394
)
95+
96+
97+
def test_should_snapshot_with_ref(page: Page) -> None:
98+
page.set_content('<ul><li><a href="about:blank">link</a></li></ul>')
99+
expected = """
100+
- list [ref=s1e3]:
101+
- listitem [ref=s1e4]:
102+
- link "link" [ref=s1e5]:
103+
- /url: about:blank
104+
"""
105+
assert page.locator("body").aria_snapshot(ref=True) == _unshift(expected)

0 commit comments

Comments
 (0)