Skip to content

Commit 96fe9ff

Browse files
Merge pull request #2244 from IFRCGo/feature/debug-playwright
Add local Playwright debug
2 parents c57921c + 4230996 commit 96fe9ff

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

api/tasks.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
from playwright.sync_api import sync_playwright
1313
from rest_framework.authtoken.models import Token
1414

15-
from api.models import Export
16-
1715
from .logger import logger
16+
from .models import Export
17+
18+
# from .utils import DebugPlaywright
1819

1920

2021
def build_storage_state(tmp_dir, user, token):
@@ -102,6 +103,7 @@ def generate_url(url, export_id, user, title):
102103
)
103104
context = browser.new_context(storage_state=storage_state)
104105
page = context.new_page()
106+
# DebugPlaywright.debug(page)
105107
timeout = 300000
106108
page.goto(url, timeout=timeout)
107109
time.sleep(5)

api/utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@
55
from django.utils.translation import gettext
66

77

8+
class DebugPlaywright:
9+
"""Basic helpers to debug PlayWright issues locally"""
10+
11+
@staticmethod
12+
def log_console(msg):
13+
"""Console logs"""
14+
print("console:", msg.text)
15+
16+
@staticmethod
17+
def log_request(request):
18+
"""Network request logs"""
19+
# Add filter to remove noise: if request.url.startswith("http://api/v2"):
20+
print("Network >>:", request.method, request.url)
21+
print(" --- ", request.headers)
22+
23+
@staticmethod
24+
def log_response(response):
25+
"""Network response logs"""
26+
# Add filter to remove noise: if response.url.startswith("http://api/v2"):
27+
print("Network <<:", response.status, response.url)
28+
print(" --- ", response.headers)
29+
30+
@classmethod
31+
def debug(cls, page):
32+
"""Add hook to receive logs from playwright"""
33+
page.on("console", cls.log_console)
34+
page.on("request", cls.log_request)
35+
page.on("response", cls.log_response)
36+
37+
838
def pretty_request(request):
939
headers = ""
1040
for header, value in request.META.items():

0 commit comments

Comments
 (0)