-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
116 lines (97 loc) · 3.21 KB
/
Copy pathconftest.py
File metadata and controls
116 lines (97 loc) · 3.21 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
import pytest
import re
from playwright.sync_api import sync_playwright, TimeoutError as PlaywrightTimeoutError
BASE_URL = "https://www.automationexercise.com"
SSL_ERROR_PATTERNS = re.compile(
r"(ERR_SSL|SSL_HANDSHAKE|CERT_|certificate)",
re.IGNORECASE,
)
CONSENT_INIT_SCRIPT = r"""
(() => {
const kill = () => {
const nodes = document.querySelectorAll(
'#fc-consent-root, .fc-consent-root, .fc-dialog-overlay, .fc-overlay, .fc-consent-dialog, .fc-dialog'
);
for (const el of nodes) {
el.style.setProperty('pointer-events', 'none', 'important');
el.style.setProperty('display', 'none', 'important');
el.style.setProperty('visibility', 'hidden', 'important');
el.style.setProperty('opacity', '0', 'important');
el.remove();
}
document.documentElement.style.overflow = 'auto';
document.body.style.overflow = 'auto';
};
kill();
// Re-injection safe: mutation observer
const obs = new MutationObserver(() => kill());
obs.observe(document.documentElement, { childList: true, subtree: true });
// Some consent managers inject on interaction
window.addEventListener('load', kill, { once: false });
window.addEventListener('scroll', kill, { passive: true });
window.addEventListener('pointerdown', kill, { passive: true });
})();
"""
@pytest.fixture(scope="session")
def browser():
headless = os.getenv("HEADLESS", "1") == "1"
with sync_playwright() as p:
browser = p.chromium.launch(headless=headless)
context = browser.new_context()
page = context.new_page()
page.goto("https://www.automationexercise.com", wait_until="domcontentloaded")
try:
page.locator(
"button:has-text('Consent'), button:has-text('Accept')"
).first.click(timeout=3000)
except PlaywrightTimeoutError:
pass
context.storage_state(path="auth_state.json")
context.close()
yield browser
browser.close()
@pytest.fixture()
def page(browser):
context = browser.new_context(
accept_downloads=True,
bypass_csp=True,
viewport={"width": 1280, "height": 720},
ignore_https_errors=True,
storage_state="auth_state.json"
)
context.add_init_script(CONSENT_INIT_SCRIPT)
p = context.new_page()
p.set_default_timeout(15000)
p.set_default_navigation_timeout(30000)
def _consent_guard():
try:
p.evaluate("""() => {
const nodes = document.querySelectorAll(
'#fc-consent-root, .fc-consent-root, .fc-dialog-overlay, .fc-overlay, .fc-consent-dialog, .fc-dialog'
);
nodes.forEach(el => { el.remove(); });
document.documentElement.style.overflow = 'auto';
document.body.style.overflow = 'auto';
}""")
except Exception:
pass
p.on("domcontentloaded", lambda: _consent_guard())
p.on("load", lambda: _consent_guard())
try:
p.locator(
"button:has-text('Consent'), button:has-text('Accept')"
).first.click(timeout=3000)
except:
pass
yield p
context.close()
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item):
try:
yield
except Exception as e:
msg = str(e)
if SSL_ERROR_PATTERNS.search(msg):
pytest.skip(f"Infrastructure SSL failure: {msg}")
raise