Skip to content

Commit 58f212e

Browse files
ft: allow async callbacks with Page.on calls (microsoft#353)
1 parent 4674d1b commit 58f212e

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

playwright/connection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
from typing import Any, Callable, Dict, Optional, Union
1919

2020
from greenlet import greenlet
21-
from pyee import EventEmitter
21+
from pyee import AsyncIOEventEmitter
2222

2323
from playwright.helper import ParsedMessagePayload, parse_error
2424
from playwright.sync_base import dispatcher_fiber
2525
from playwright.transport import Transport
2626

2727

28-
class Channel(EventEmitter):
28+
class Channel(AsyncIOEventEmitter):
2929
def __init__(self, connection: "Connection", guid: str) -> None:
3030
super().__init__()
3131
self._connection: Connection = connection
@@ -64,15 +64,15 @@ def send_no_reply(self, method: str, params: Dict = None) -> None:
6464
self._connection._send_message_to_server(self._guid, method, params)
6565

6666

67-
class ChannelOwner(EventEmitter):
67+
class ChannelOwner(AsyncIOEventEmitter):
6868
def __init__(
6969
self,
7070
parent: Union["ChannelOwner", "Connection"],
7171
type: str,
7272
guid: str,
7373
initializer: Dict,
7474
) -> None:
75-
super().__init__()
75+
super().__init__(loop=parent._loop)
7676
self._loop: asyncio.AbstractEventLoop = parent._loop
7777
self._type = type
7878
self._guid = guid

tests/async/test_browsercontext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ def handle_page(page):
728728
events.append("CREATED: " + page.url)
729729
page.on("close", lambda: events.append("DESTROYED: " + page.url))
730730

731-
context.on("page", lambda page: handle_page(page))
731+
context.on("page", handle_page)
732732

733733
page = await context.newPage()
734734
await page.goto(server.EMPTY_PAGE)

tests/async/test_dialog.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
async def test_should_fire(page: Page, server):
2323
result = []
2424

25-
def on_dialog(dialog: Dialog):
25+
async def on_dialog(dialog: Dialog):
2626
result.append(True)
2727
assert dialog.type == "alert"
2828
assert dialog.defaultValue == ""
2929
assert dialog.message == "yo"
30-
asyncio.create_task(dialog.accept())
30+
await dialog.accept()
3131

3232
page.on("dialog", on_dialog)
3333
await page.evaluate("alert('yo')")
@@ -37,12 +37,12 @@ def on_dialog(dialog: Dialog):
3737
async def test_should_allow_accepting_prompts(page: Page, server):
3838
result = []
3939

40-
def on_dialog(dialog: Dialog):
40+
async def on_dialog(dialog: Dialog):
4141
result.append(True)
4242
assert dialog.type == "prompt"
4343
assert dialog.defaultValue == "yes."
4444
assert dialog.message == "question?"
45-
asyncio.create_task(dialog.accept("answer!"))
45+
await dialog.accept("answer!")
4646

4747
page.on("dialog", on_dialog)
4848
assert await page.evaluate("prompt('question?', 'yes.')") == "answer!"
@@ -52,9 +52,9 @@ def on_dialog(dialog: Dialog):
5252
async def test_should_dismiss_the_prompt(page: Page, server):
5353
result = []
5454

55-
def on_dialog(dialog: Dialog):
55+
async def on_dialog(dialog: Dialog):
5656
result.append(True)
57-
asyncio.create_task(dialog.dismiss())
57+
await dialog.dismiss()
5858

5959
page.on("dialog", on_dialog)
6060
assert await page.evaluate("prompt('question?')") is None
@@ -64,9 +64,9 @@ def on_dialog(dialog: Dialog):
6464
async def test_should_accept_the_confirm_prompt(page: Page, server):
6565
result = []
6666

67-
def on_dialog(dialog: Dialog):
67+
async def on_dialog(dialog: Dialog):
6868
result.append(True)
69-
asyncio.create_task(dialog.accept())
69+
await dialog.accept()
7070

7171
page.on("dialog", on_dialog)
7272
assert await page.evaluate("confirm('boolean?')") is True
@@ -76,9 +76,9 @@ def on_dialog(dialog: Dialog):
7676
async def test_should_dismiss_the_confirm_prompt(page: Page, server):
7777
result = []
7878

79-
def on_dialog(dialog: Dialog):
79+
async def on_dialog(dialog: Dialog):
8080
result.append(True)
81-
asyncio.create_task(dialog.dismiss())
81+
await dialog.dismiss()
8282

8383
page.on("dialog", on_dialog)
8484
assert await page.evaluate("confirm('boolean?')") is False

0 commit comments

Comments
 (0)