From 62078fb41d2b6309c6f93e0e9dcb6d68d4832392 Mon Sep 17 00:00:00 2001 From: Adam Gastineau Date: Mon, 6 Oct 2025 09:24:41 -0700 Subject: [PATCH] chore: add dupe sync tests for 1.56 roll --- tests/sync/test_page_aria_snapshot.py | 10 +++++ tests/sync/test_page_event_console.py | 36 +++++++++++++++++ tests/sync/test_page_event_pageerror.py | 36 +++++++++++++++++ tests/sync/test_page_event_request.py | 54 +++++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 tests/sync/test_page_event_console.py create mode 100644 tests/sync/test_page_event_pageerror.py create mode 100644 tests/sync/test_page_event_request.py diff --git a/tests/sync/test_page_aria_snapshot.py b/tests/sync/test_page_aria_snapshot.py index e892bb371..17d722ae8 100644 --- a/tests/sync/test_page_aria_snapshot.py +++ b/tests/sync/test_page_aria_snapshot.py @@ -204,3 +204,13 @@ def test_should_snapshot_with_restored_contain_mode_inside_deep_equal( - listitem: 1.1 """, ) + + +def test_match_values_both_against_regex_and_string(page: Page) -> None: + page.set_content('Log in') + expect(page.locator("body")).to_match_aria_snapshot( + """ + - link "Log in": + - /url: /auth?r=/ + """, + ) diff --git a/tests/sync/test_page_event_console.py b/tests/sync/test_page_event_console.py new file mode 100644 index 000000000..0ab19eca7 --- /dev/null +++ b/tests/sync/test_page_event_console.py @@ -0,0 +1,36 @@ +# 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. + + +from playwright.sync_api import Page + + +def test_console_messages_should_work(page: Page) -> None: + page.evaluate( + """() => { + for (let i = 0; i < 301; i++) + console.log('message' + i); + }""" + ) + + messages = page.console_messages() + objects = [{"text": m.text, "type": m.type, "page": m.page} for m in messages] + + expected = [] + for i in range(201, 301): + expected.append({"text": f"message{i}", "type": "log", "page": page}) + + assert len(objects) >= 100, "should be at least 100 messages" + message_count = len(messages) - len(expected) + assert objects[message_count:] == expected, "should return last messages" diff --git a/tests/sync/test_page_event_pageerror.py b/tests/sync/test_page_event_pageerror.py new file mode 100644 index 000000000..9295cb1b0 --- /dev/null +++ b/tests/sync/test_page_event_pageerror.py @@ -0,0 +1,36 @@ +# 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. + +from playwright.sync_api import Page + + +def test_page_errors_should_work(page: Page) -> None: + page.evaluate( + """async () => { + for (let i = 0; i < 301; i++) + window.setTimeout(() => { throw new Error('error' + i); }, 0); + await new Promise(f => window.setTimeout(f, 100)); + }""" + ) + + errors = page.page_errors() + messages = [e.message for e in errors] + + expected = [] + for i in range(201, 301): + expected.append(f"error{i}") + + assert len(messages) >= 100, "should be at least 100 errors" + message_count = len(messages) - len(expected) + assert messages[message_count:] == expected, "should return last errors" diff --git a/tests/sync/test_page_event_request.py b/tests/sync/test_page_event_request.py new file mode 100644 index 000000000..77515091f --- /dev/null +++ b/tests/sync/test_page_event_request.py @@ -0,0 +1,54 @@ +# 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. + +from playwright.sync_api import Page, Request, Route +from tests.server import Server + + +def test_should_return_last_requests(page: Page, server: Server) -> None: + page.goto(server.PREFIX + "/title.html") + for i in range(200): + + def _handle_route(route: Route) -> None: + route.fulfill( + status=200, + body=f"url:{route.request.url}", + ) + + page.route(f"**/fetch?{i}", _handle_route) + + # #0 is the navigation request, so start with #1. + for i in range(1, 100): + page.evaluate("url => fetch(url)", server.PREFIX + f"/fetch?{i}") + first_100_requests_with_goto = page.requests() + first_100_requests = first_100_requests_with_goto[1:] + + for i in range(100, 200): + page.evaluate("url => fetch(url)", server.PREFIX + f"/fetch?{i}") + last_100_requests = page.requests() + + all_requests = first_100_requests + last_100_requests + + def gather_response(request: Request) -> dict: + response = request.response() + assert response + return {"text": response.text(), "url": request.url} + + # All 199 requests are fully functional. + received = [gather_response(request) for request in all_requests] + expected = [] + for i in range(1, 200): + url = server.PREFIX + f"/fetch?{i}" + expected.append({"url": url, "text": f"url:{url}"}) + assert received == expected