Skip to content

Commit

Permalink
fix(webError): fix WebError when using sync API (#2721)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Jan 22, 2025
1 parent 4ae12bd commit dffa098
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
5 changes: 4 additions & 1 deletion playwright/_impl/_browser_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,10 @@ def _on_dialog(self, dialog: Dialog) -> None:
asyncio.create_task(dialog.dismiss())

def _on_page_error(self, error: Error, page: Optional[Page]) -> None:
self.emit(BrowserContext.Events.WebError, WebError(self._loop, page, error))
self.emit(
BrowserContext.Events.WebError,
WebError(self._loop, self._dispatcher_fiber, page, error),
)
if page:
page.emit(Page.Events.PageError, error)

Expand Down
9 changes: 7 additions & 2 deletions playwright/_impl/_web_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@
# limitations under the License.

from asyncio import AbstractEventLoop
from typing import Optional
from typing import Any, Optional

from playwright._impl._helper import Error
from playwright._impl._page import Page


class WebError:
def __init__(
self, loop: AbstractEventLoop, page: Optional[Page], error: Error
self,
loop: AbstractEventLoop,
dispatcher_fiber: Any,
page: Optional[Page],
error: Error,
) -> None:
self._loop = loop
self._dispatcher_fiber = dispatcher_fiber
self._page = page
self._error = error

Expand Down
2 changes: 2 additions & 0 deletions playwright/async_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
Selectors,
Touchscreen,
Video,
WebError,
WebSocket,
WebSocketRoute,
Worker,
Expand Down Expand Up @@ -190,6 +191,7 @@ def __call__(
"Touchscreen",
"Video",
"ViewportSize",
"WebError",
"WebSocket",
"WebSocketRoute",
"Worker",
Expand Down
2 changes: 2 additions & 0 deletions playwright/sync_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
Selectors,
Touchscreen,
Video,
WebError,
WebSocket,
WebSocketRoute,
Worker,
Expand Down Expand Up @@ -190,6 +191,7 @@ def __call__(
"Touchscreen",
"Video",
"ViewportSize",
"WebError",
"WebSocket",
"WebSocketRoute",
"Worker",
Expand Down
10 changes: 9 additions & 1 deletion tests/async/test_browsercontext_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import pytest

from playwright.async_api import Page
from playwright.async_api import BrowserContext, Page
from tests.utils import must

from ..server import Server, TestServerRequest
Expand Down Expand Up @@ -198,3 +198,11 @@ async def test_page_error_event_should_work(page: Page) -> None:
page_error = await page_error_info.value
assert page_error.page == page
assert "boom" in page_error.error.stack


async def test_weberror_event_should_work(context: BrowserContext, page: Page) -> None:
async with context.expect_event("weberror") as error_info:
await page.goto('data:text/html,<script>throw new Error("Test")</script>')
error = await error_info.value
assert error.page == page
assert error.error.message == "Test"
10 changes: 9 additions & 1 deletion tests/sync/test_browsercontext_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import pytest

from playwright.sync_api import Dialog, Page
from playwright.sync_api import BrowserContext, Dialog, Page

from ..server import Server, TestServerRequest

Expand Down Expand Up @@ -198,3 +198,11 @@ def test_console_event_should_work_with_context_manager(page: Page) -> None:
message = cm_info.value
assert message.text == "hello"
assert message.page == page


def test_weberror_event_should_work(context: BrowserContext, page: Page) -> None:
with context.expect_event("weberror") as error_info:
page.goto('data:text/html,<script>throw new Error("Test")</script>')
error = error_info.value
assert error.page == page
assert error.error.message == "Test"

0 comments on commit dffa098

Please sign in to comment.