-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathconftest.py
More file actions
167 lines (132 loc) · 5.43 KB
/
Copy pathconftest.py
File metadata and controls
167 lines (132 loc) · 5.43 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Copyright 2025 Streamlit PDF Component
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from pathlib import Path
from typing import Generator, Any, Dict
from playwright.sync_api import Browser, BrowserContext, Page, expect
from tests import ROOT_DIRECTORY
from tests.e2e_utils import StreamlitRunner
@pytest.fixture(scope="function")
def context(browser: Browser) -> Generator[BrowserContext, None, None]:
"""Create a new browser context with custom settings."""
# Browser-specific context options
context_options: Dict[str, Any] = {
"accept_downloads": True,
"service_workers": "block"
if browser.browser_type.name == "chromium"
else "allow",
}
context = browser.new_context(**context_options)
# Set default timeout for all operations
context.set_default_timeout(30000)
yield context
context.close()
@pytest.fixture(scope="function")
def page(context: BrowserContext) -> Generator[Page, None, None]:
"""Create a new page with error handling."""
page = context.new_page()
# Add console message listener for debugging
def handle_console(msg):
# Also log console messages from iframes
if msg.type == "error":
print(f"[CONSOLE ERROR] {msg.text}")
if msg.location:
print(f" at {msg.location}")
elif msg.type == "warning":
print(f"[CONSOLE WARNING] {msg.text}")
else:
print(f"[{msg.type}] {msg.text}")
page.on("console", handle_console)
# Add page error listener
page.on("pageerror", lambda err: print(f"[PAGE ERROR] {err}"))
yield page
page.close()
def pytest_configure(config):
"""Configure pytest with custom markers and settings."""
config.addinivalue_line(
"markers", "slow: marks tests as slow (deselect with '-m \"not slow\"')"
)
config.addinivalue_line(
"markers", "accessibility: marks tests as accessibility tests"
)
config.addinivalue_line(
"markers", "performance: marks tests as performance tests"
)
config.addinivalue_line(
"markers", "edge_case: marks tests as edge case tests"
)
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
"""Add extra information to test reports on failure."""
outcome = yield
rep = outcome.get_result()
# Add browser name to test report
if hasattr(item, "funcargs") and "page" in item.funcargs:
page = item.funcargs["page"]
browser_name = page.context.browser.browser_type.name
rep.browser = browser_name
# On failure, add more debugging info
if rep.failed and call.when == "call":
print(f"\n[FAILED in {browser_name}] Test: {item.name}")
try:
# Try to get page title for context
title = page.title()
print(f"Page title: {title}")
# Check if there are any iframes
frames = page.frames
print(f"Number of frames: {len(frames)}")
# Get page URL
print(f"Page URL: {page.url}")
except Exception as e:
print(f"Could not get page info: {e}")
@pytest.fixture(scope="session")
def browser_type_launch_args(browser_type_launch_args):
"""Configure browser launch arguments for PDF testing."""
return {
**browser_type_launch_args,
"firefox_user_prefs": {
"pdfjs.disabled": False,
}
}
@pytest.fixture(scope="session")
def default_test_app_file():
"""Default test app file for most tests."""
return Path(__file__).parent / "streamlit_apps" / "example_zoom_auto.py"
@pytest.fixture(scope="module")
def default_streamlit_app(default_test_app_file):
"""Default streamlit app fixture for tests that use the default test app."""
with StreamlitRunner(default_test_app_file) as runner:
yield runner
@pytest.fixture(scope="function")
def default_go_to_app(page: Page, default_streamlit_app: StreamlitRunner):
"""Navigate to the default streamlit app and wait for it to load."""
page.goto(default_streamlit_app.server_url)
# Wait for app to load
expect(page.get_by_role("img", name="Running...")).not_to_be_visible()
@pytest.fixture(autouse=True, scope="module")
def streamlit_app(default_test_app_file):
"""Streamlit app fixture for tests that use the default test app.
Tests can override this by defining their own streamlit_app fixture.
"""
with StreamlitRunner(default_test_app_file) as runner:
yield runner
@pytest.fixture(autouse=True, scope="function")
def go_to_app(page: Page, streamlit_app: StreamlitRunner):
"""Navigate to the streamlit app and wait for it to load.
Tests can override this by defining their own go_to_app fixture.
"""
page.goto(streamlit_app.server_url)
# Wait for app to load
expect(page.get_by_role("img", name="Running...")).not_to_be_visible()