-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_events.py
More file actions
50 lines (37 loc) · 1.31 KB
/
test_events.py
File metadata and controls
50 lines (37 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from playwright.sync_api import Dialog, ConsoleMessage, Page, Response
from contextlib import contextmanager
import json
import time
def dialog_handler(dialog: Dialog):
# just to show it works
time.sleep(2)
print('\n\n====== close dialog ====== \n\n')
dialog.accept()
def console_handler(message: ConsoleMessage):
print(message.text)
@contextmanager
def catch_response(page: Page):
def response_handler(response: Response):
print(json.dumps(response.json(), indent=4))
page.on('response', response_handler)
yield
page.remove_listener('response', response_handler)
# read more https://playwright.dev/python/docs/events
def test_events(alice):
alice.on('dialog', dialog_handler)
alice.on('console', console_handler)
alice.click('[href="/demo/"]')
alice.click('.newPage')
# alice.click('[href="/"]')
# Example of event handling within context manager
def test_events_once(alice):
alice.click('[href="/"]')
alice.once('response', lambda response: print('hello world'))
alice.click('.refresh input')
alice.wait_for_timeout(1000)
# Example of event handling within context manager
def test_events_with_cm(alice):
alice.click('[href="/"]')
with catch_response(alice):
alice.click('.refresh input')
alice.wait_for_timeout(1000)