From e48dc1ac0886b122c93e660e2470548d07683380 Mon Sep 17 00:00:00 2001 From: "Ross A. Wollman" Date: Sun, 14 Aug 2022 20:29:33 -0700 Subject: [PATCH 1/3] chore: roll Playwright to 1.25.0 --- playwright/async_api/_generated.py | 8 ++++++++ playwright/sync_api/_generated.py | 8 ++++++++ setup.py | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/playwright/async_api/_generated.py b/playwright/async_api/_generated.py index 1b364aca9..c2bc7ace7 100644 --- a/playwright/async_api/_generated.py +++ b/playwright/async_api/_generated.py @@ -2791,6 +2791,10 @@ async def snapshot( ) -> typing.Optional[typing.Dict]: """Accessibility.snapshot + **DEPRECATED** This method is deprecated. Please use other libraries such as [Axe](https://www.deque.com/axe/) if you + need to test page accessibility. See our Node.js [guide](https://playwright.dev/docs/accessibility-testing) for + integration with Axe. + Captures the current state of the accessibility tree. The returned object represents the root accessible node of the page. @@ -6233,6 +6237,10 @@ def once( def accessibility(self) -> "Accessibility": """Page.accessibility + **DEPRECATED** This property is deprecated. Please use other libraries such as [Axe](https://www.deque.com/axe/) if you + need to test page accessibility. See our Node.js [guide](https://playwright.dev/docs/accessibility-testing) for + integration with Axe. + Returns ------- Accessibility diff --git a/playwright/sync_api/_generated.py b/playwright/sync_api/_generated.py index b949c7d77..3d0601fff 100644 --- a/playwright/sync_api/_generated.py +++ b/playwright/sync_api/_generated.py @@ -2827,6 +2827,10 @@ def snapshot( ) -> typing.Optional[typing.Dict]: """Accessibility.snapshot + **DEPRECATED** This method is deprecated. Please use other libraries such as [Axe](https://www.deque.com/axe/) if you + need to test page accessibility. See our Node.js [guide](https://playwright.dev/docs/accessibility-testing) for + integration with Axe. + Captures the current state of the accessibility tree. The returned object represents the root accessible node of the page. @@ -6231,6 +6235,10 @@ def once(self, event: str, f: typing.Callable[..., None]) -> None: def accessibility(self) -> "Accessibility": """Page.accessibility + **DEPRECATED** This property is deprecated. Please use other libraries such as [Axe](https://www.deque.com/axe/) if you + need to test page accessibility. See our Node.js [guide](https://playwright.dev/docs/accessibility-testing) for + integration with Axe. + Returns ------- Accessibility diff --git a/setup.py b/setup.py index 90c0c7e78..3487a6abf 100644 --- a/setup.py +++ b/setup.py @@ -30,7 +30,7 @@ InWheel = None from wheel.bdist_wheel import bdist_wheel as BDistWheelCommand -driver_version = "1.25.0-alpha-1660067092000" +driver_version = "1.25.0" def extractall(zip: zipfile.ZipFile, path: str) -> None: From 1425c3a6886e1de6c8a22c0f7cf6c8e631fc3ae7 Mon Sep 17 00:00:00 2001 From: "Ross A. Wollman" Date: Sun, 14 Aug 2022 21:14:44 -0700 Subject: [PATCH 2/3] port print response body in failed to_be_ok --- playwright/_impl/_assertions.py | 9 +++++ playwright/_impl/_helper.py | 9 +++++ tests/async/test_assertions.py | 58 +++++++++++++++++++++++++++++++++ tests/sync/test_assertions.py | 58 +++++++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+) diff --git a/playwright/_impl/_assertions.py b/playwright/_impl/_assertions.py index cfe4dd4d7..75b7f91f1 100644 --- a/playwright/_impl/_assertions.py +++ b/playwright/_impl/_assertions.py @@ -17,6 +17,7 @@ from playwright._impl._api_structures import ExpectedTextValue, FrameExpectOptions from playwright._impl._fetch import APIResponse +from playwright._impl._helper import is_textual_mime_type from playwright._impl._locator import Locator from playwright._impl._page import Page from playwright._impl._str_utils import escape_regex_flags @@ -594,6 +595,14 @@ async def to_be_ok( log = "\n".join(log_list).strip() if log: message += f"\n Call log:\n{log}" + + content_type = self._actual.headers.get("content-type") + print(f">>> content-type {content_type}") + is_text_encoding = content_type and is_textual_mime_type(content_type) + text = await self._actual.text() if is_text_encoding else None + if text is not None: + message += f"\n Response Text:\n{text[:1000]}" + raise AssertionError(message) async def not_to_be_ok(self) -> None: diff --git a/playwright/_impl/_helper.py b/playwright/_impl/_helper.py index 050a12304..da1211e6f 100644 --- a/playwright/_impl/_helper.py +++ b/playwright/_impl/_helper.py @@ -362,3 +362,12 @@ def is_file_payload(value: Optional[Any]) -> bool: and "mimeType" in value and "buffer" in value ) + + +TEXTUAL_MIME_TYPE = re.compile( + r"^(text\/.*?|application\/(json|(x-)?javascript|xml.*?|ecmascript|graphql|x-www-form-urlencoded)|image\/svg(\+xml)?|application\/.*?(\+json|\+xml))(;\s*charset=.*)?$" +) + + +def is_textual_mime_type(mime_type: str) -> bool: + return bool(TEXTUAL_MIME_TYPE.match(mime_type)) diff --git a/tests/async/test_assertions.py b/tests/async/test_assertions.py index 367354aa9..996f02390 100644 --- a/tests/async/test_assertions.py +++ b/tests/async/test_assertions.py @@ -609,3 +609,61 @@ async def test_assertions_response_is_ok_fail(page: Page, server: Server) -> Non error_message = str(excinfo.value) assert ("→ GET " + server.PREFIX + "/unknown") in error_message assert "← 404 Not Found" in error_message + + +async def test_should_print_response_with_text_content_type_if_to_be_ok_fails( + page: Page, server: Server +) -> None: + server.set_route( + "/text-content-type", + lambda r: ( + r.setResponseCode(404), + r.setHeader("content-type", "text/plain"), + r.write(b"Text error"), + r.finish(), + ), + ) + server.set_route( + "/no-content-type", + lambda r: ( + r.setResponseCode(404), + r.write(b"No content type error"), + r.finish(), + ), + ) + server.set_route( + "/binary-content-type", + lambda r: ( + r.setResponseCode(404), + r.setHeader("content-type", "image/bmp"), + r.write(b"Image content type error"), + r.finish(), + ), + ) + + response = await page.request.get(server.PREFIX + "/text-content-type") + with pytest.raises(AssertionError) as excinfo: + await expect(response).to_be_ok() + error_message = str(excinfo.value) + assert ("→ GET " + server.PREFIX + "/text-content-type") in error_message + assert "← 404 Not Found" in error_message + assert "Response Text:" in error_message + assert "Text error" in error_message + + response = await page.request.get(server.PREFIX + "/no-content-type") + with pytest.raises(AssertionError) as excinfo: + await expect(response).to_be_ok() + error_message = str(excinfo.value) + assert ("→ GET " + server.PREFIX + "/no-content-type") in error_message + assert "← 404 Not Found" in error_message + assert "Response Text:" not in error_message + assert "No content type error" not in error_message + + response = await page.request.get(server.PREFIX + "/binary-content-type") + with pytest.raises(AssertionError) as excinfo: + await expect(response).to_be_ok() + error_message = str(excinfo.value) + assert ("→ GET " + server.PREFIX + "/binary-content-type") in error_message + assert "← 404 Not Found" in error_message + assert "Response Text:" not in error_message + assert "Image content type error" not in error_message diff --git a/tests/sync/test_assertions.py b/tests/sync/test_assertions.py index e509f0e77..f99923cd4 100644 --- a/tests/sync/test_assertions.py +++ b/tests/sync/test_assertions.py @@ -596,3 +596,61 @@ def test_assertions_response_is_ok_fail(page: Page, server: Server) -> None: error_message = str(excinfo.value) assert ("→ GET " + server.PREFIX + "/unknown") in error_message assert "← 404 Not Found" in error_message + + +def test_should_print_response_with_text_content_type_if_to_be_ok_fails( + page: Page, server: Server +) -> None: + server.set_route( + "/text-content-type", + lambda r: ( + r.setResponseCode(404), + r.setHeader("content-type", "text/plain"), + r.write(b"Text error"), + r.finish(), + ), + ) + server.set_route( + "/no-content-type", + lambda r: ( + r.setResponseCode(404), + r.write(b"No content type error"), + r.finish(), + ), + ) + server.set_route( + "/binary-content-type", + lambda r: ( + r.setResponseCode(404), + r.setHeader("content-type", "image/bmp"), + r.write(b"Image content type error"), + r.finish(), + ), + ) + + response = page.request.get(server.PREFIX + "/text-content-type") + with pytest.raises(AssertionError) as excinfo: + expect(response).to_be_ok() + error_message = str(excinfo.value) + assert ("→ GET " + server.PREFIX + "/text-content-type") in error_message + assert "← 404 Not Found" in error_message + assert "Response Text:" in error_message + assert "Text error" in error_message + + response = page.request.get(server.PREFIX + "/no-content-type") + with pytest.raises(AssertionError) as excinfo: + expect(response).to_be_ok() + error_message = str(excinfo.value) + assert ("→ GET " + server.PREFIX + "/no-content-type") in error_message + assert "← 404 Not Found" in error_message + assert "Response Text:" not in error_message + assert "No content type error" not in error_message + + response = page.request.get(server.PREFIX + "/binary-content-type") + with pytest.raises(AssertionError) as excinfo: + expect(response).to_be_ok() + error_message = str(excinfo.value) + assert ("→ GET " + server.PREFIX + "/binary-content-type") in error_message + assert "← 404 Not Found" in error_message + assert "Response Text:" not in error_message + assert "Image content type error" not in error_message From 664fa0c8a59c1dddb6eade194adb8fffb02abcf1 Mon Sep 17 00:00:00 2001 From: Ross Wollman Date: Mon, 15 Aug 2022 09:00:18 -0700 Subject: [PATCH 3/3] Update playwright/_impl/_assertions.py Co-authored-by: Max Schmitt --- playwright/_impl/_assertions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/playwright/_impl/_assertions.py b/playwright/_impl/_assertions.py index 75b7f91f1..3bb79ab2d 100644 --- a/playwright/_impl/_assertions.py +++ b/playwright/_impl/_assertions.py @@ -597,7 +597,6 @@ async def to_be_ok( message += f"\n Call log:\n{log}" content_type = self._actual.headers.get("content-type") - print(f">>> content-type {content_type}") is_text_encoding = content_type and is_textual_mime_type(content_type) text = await self._actual.text() if is_text_encoding else None if text is not None: