Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/sync/test_page_aria_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('<a href="/auth?r=/">Log in</a>')
expect(page.locator("body")).to_match_aria_snapshot(
"""
- link "Log in":
- /url: /auth?r=/
""",
)
36 changes: 36 additions & 0 deletions tests/sync/test_page_event_console.py
Original file line number Diff line number Diff line change
@@ -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"
36 changes: 36 additions & 0 deletions tests/sync/test_page_event_pageerror.py
Original file line number Diff line number Diff line change
@@ -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"
54 changes: 54 additions & 0 deletions tests/sync/test_page_event_request.py
Original file line number Diff line number Diff line change
@@ -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
Loading