-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisit_and_idle.py
54 lines (44 loc) · 1.84 KB
/
visit_and_idle.py
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
51
52
53
54
import contextlib
import sys
from time import time_ns, sleep
from playwright.sync_api import Playwright, sync_playwright, expect, TimeoutError
def log_note(message: str) -> None:
timestamp = str(time_ns())[:16]
print(f"{timestamp} {message}")
def run(playwright: Playwright, browser_name: str, url: str) -> None:
log_note(f"Launch browser {browser_name}")
if browser_name == "firefox":
browser = playwright.firefox.launch(headless=True)
else:
# this leverages new headless mode by Chromium: https://developer.chrome.com/articles/new-headless/
# This mode is less detectable by websites and thus no mitgations will be applied by test candidate
# The mode is however ~40% slower: https://github.com/microsoft/playwright/issues/21216
browser = playwright.chromium.launch(headless=False,args=["--headless=new"])
context = browser.new_context()
page = context.new_page()
try:
log_note("Opening URL")
page.goto(url)
page.wait_for_load_state('load')
log_note("DOM Content Loaded. Staying Idle for 60 seconds to log animations")
sleep(60)
except Exception as e:
if hasattr(e, 'message'): # only Playwright error class has this member
log_note(f"Exception occurred: {e.message}")
log_note("Page content was:")
log_note(page.content())
raise e
# ---------------------
context.close()
browser.close()
if __name__ == "__main__":
if len(sys.argv) > 2:
browser_name = sys.argv[2].lower()
if browser_name not in ["chromium", "firefox"]:
print("Invalid browser name. Please choose either 'chromium' or 'firefox'.")
sys.exit(1)
else:
browser_name = "chromium"
url = sys.argv[1]
with sync_playwright() as playwright:
run(playwright, browser_name, url)