forked from streamlit/streamlit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconftest.py
589 lines (486 loc) · 19.7 KB
/
conftest.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
#
# 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.
"""
Global pytest fixtures for e2e tests.
This file is automatically run by pytest before tests are executed.
"""
from __future__ import annotations
import hashlib
import os
import re
import shlex
import shutil
import socket
import subprocess
import sys
import time
from io import BytesIO
from pathlib import Path
from random import randint
from tempfile import TemporaryFile
from types import ModuleType
from typing import Any, Dict, Generator, List, Literal, Protocol, Tuple
from urllib import parse
import pytest
import requests
from PIL import Image
from playwright.sync_api import ElementHandle, Locator, Page
from pytest import FixtureRequest
def reorder_early_fixtures(metafunc: pytest.Metafunc):
"""Put fixtures with `pytest.mark.early` first during execution
This allows patch of configurations before the application is initialized
Copied from: https://github.com/pytest-dev/pytest/issues/1216#issuecomment-456109892
"""
for fixturedef in metafunc._arg2fixturedefs.values():
fixturedef = fixturedef[0]
for mark in getattr(fixturedef.func, "pytestmark", []):
if mark.name == "early":
order = metafunc.fixturenames
order.insert(0, order.pop(order.index(fixturedef.argname)))
break
def pytest_generate_tests(metafunc: pytest.Metafunc):
reorder_early_fixtures(metafunc)
class AsyncSubprocess:
"""A context manager. Wraps subprocess. Popen to capture output safely."""
def __init__(self, args, cwd=None, env=None):
self.args = args
self.cwd = cwd
self.env = env or {}
self._proc = None
self._stdout_file = None
def terminate(self):
"""Terminate the process and return its stdout/stderr in a string."""
if self._proc is not None:
self._proc.terminate()
self._proc.wait()
self._proc = None
# Read the stdout file and close it
stdout = None
if self._stdout_file is not None:
self._stdout_file.seek(0)
stdout = self._stdout_file.read()
self._stdout_file.close()
self._stdout_file = None
return stdout
def __enter__(self):
self.start()
return self
def start(self):
# Start the process and capture its stdout/stderr output to a temp
# file. We do this instead of using subprocess.PIPE (which causes the
# Popen object to capture the output to its own internal buffer),
# because large amounts of output can cause it to deadlock.
self._stdout_file = TemporaryFile("w+")
print(f"Running: {shlex.join(self.args)}")
self._proc = subprocess.Popen(
self.args,
cwd=self.cwd,
stdout=self._stdout_file,
stderr=subprocess.STDOUT,
text=True,
env={**os.environ.copy(), **self.env},
)
def __exit__(self, exc_type, exc_val, exc_tb):
if self._proc is not None:
self._proc.terminate()
self._proc = None
if self._stdout_file is not None:
self._stdout_file.close()
self._stdout_file = None
def resolve_test_to_script(test_module: ModuleType) -> str:
"""Resolve the test module to the corresponding test script filename."""
assert test_module.__file__ is not None
return test_module.__file__.replace("_test.py", ".py")
def hash_to_range(
text: str,
min: int = 10000,
max: int = 65535,
) -> int:
sha256_hash = hashlib.sha256(text.encode("utf-8")).hexdigest()
return min + (int(sha256_hash, 16) % (max - min + 1))
def is_port_available(port: int, host: str) -> bool:
"""Check if a port is available on the given host."""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
return sock.connect_ex((host, port)) != 0
def find_available_port(
min_port: int = 10000,
max_port: int = 65535,
max_tries: int = 50,
host: str = "localhost",
) -> int:
"""Find an available port on the given host."""
for _ in range(max_tries):
selected_port = randint(min_port, max_port)
if is_port_available(selected_port, host):
return selected_port
raise RuntimeError("Unable to find an available port.")
def is_app_server_running(port: int, host: str = "localhost") -> bool:
"""Check if the app server is running."""
try:
return (
requests.get(f"http://{host}:{port}/_stcore/health", timeout=1).text == "ok"
)
except Exception:
return False
def wait_for_app_server_to_start(port: int, timeout: int = 5) -> bool:
"""Wait for the app server to start.
Parameters
----------
port : int
The port on which the app server is running.
timeout : int
The number of minutes to wait for the app server to start.
Returns
-------
bool
True if the app server is started, False otherwise.
"""
print(f"Waiting for app to start... {port}")
start_time = time.time()
while not is_app_server_running(port):
time.sleep(3)
if time.time() - start_time > 60 * timeout:
return False
return True
@pytest.fixture(scope="module")
def app_port(worker_id: str) -> int:
"""Fixture that returns an available port on localhost."""
if worker_id and worker_id != "master":
# This is run with xdist, we try to get a port by hashing the worker ID
port = hash_to_range(worker_id)
if is_port_available(port, "localhost"):
return port
# Find a random available port:
return find_available_port()
@pytest.fixture(scope="module", autouse=True)
def app_server(
app_port: int, request: FixtureRequest
) -> Generator[AsyncSubprocess, None, None]:
"""Fixture that starts and stops the Streamlit app server."""
streamlit_proc = AsyncSubprocess(
[
"streamlit",
"run",
resolve_test_to_script(request.module),
"--server.headless",
"true",
"--global.developmentMode",
"false",
"--server.port",
str(app_port),
"--browser.gatherUsageStats",
"false",
"--server.fileWatcherType",
"none",
],
cwd=".",
)
streamlit_proc.start()
if not wait_for_app_server_to_start(app_port):
streamlit_stdout = streamlit_proc.terminate()
print(streamlit_stdout)
raise RuntimeError("Unable to start Streamlit app")
yield streamlit_proc
streamlit_stdout = streamlit_proc.terminate()
print(streamlit_stdout)
@pytest.fixture(scope="function")
def app(page: Page, app_port: int) -> Page:
"""Fixture that opens the app."""
page.goto(f"http://localhost:{app_port}/")
wait_for_app_loaded(page)
return page
@pytest.fixture(scope="function")
def app_with_query_params(
page: Page, app_port: int, request: FixtureRequest
) -> Tuple[Page, Dict]:
"""Fixture that opens the app with additional query parameters.
The query parameters are passed as a dictionary in the 'param' key of the request.
"""
query_params = request.param
query_string = parse.urlencode(query_params, doseq=True)
url = f"http://localhost:{app_port}/?{query_string}"
page.goto(url)
wait_for_app_loaded(page)
return page, query_params
@pytest.fixture(scope="session")
def browser_type_launch_args(browser_type_launch_args: Dict, browser_name: str):
"""Fixture that adds the fake device and ui args to the browser type launch args."""
# The browser context fixture in pytest-playwright is defined in session scope, and
# depends on the browser_type_launch_args fixture. This means that we can't
# redefine the browser_type_launch_args fixture more narrow scope
# e.g. function or module scope.
# https://github.com/microsoft/playwright-pytest/blob/ef99541352b307411dbc15c627e50f95de30cc71/pytest_playwright/pytest_playwright.py#L128
# We need to extend browser launch args to support fake video stream for
# st.camera_input test.
# https://github.com/microsoft/playwright/issues/4532#issuecomment-1491761713
if browser_name == "chromium":
browser_type_launch_args = {
**browser_type_launch_args,
"args": [
"--use-fake-device-for-media-stream",
"--use-fake-ui-for-media-stream",
],
}
elif browser_name == "firefox":
browser_type_launch_args = {
**browser_type_launch_args,
"firefox_user_prefs": {
"media.navigator.streams.fake": True,
"media.navigator.permission.disabled": True,
"permissions.default.microphone": 1,
"permissions.default.camera": 1,
},
}
return browser_type_launch_args
@pytest.fixture(scope="function", params=["light_theme", "dark_theme"])
def app_theme(request) -> str:
"""Fixture that returns the theme name."""
return str(request.param)
@pytest.fixture(scope="function")
def themed_app(page: Page, app_port: int, app_theme: str) -> Page:
"""Fixture that opens the app with the given theme."""
page.goto(f"http://localhost:{app_port}/?embed_options={app_theme}")
wait_for_app_loaded(page)
return page
class ImageCompareFunction(Protocol):
def __call__(
self,
element: ElementHandle | Locator | Page,
*,
image_threshold: float = 0.002,
pixel_threshold: float = 0.05,
name: str | None = None,
fail_fast: bool = False,
) -> None:
"""Compare a screenshot with screenshot from a past run.
Parameters
----------
element : ElementHandle or Locator
The element to take a screenshot of.
image_threshold : float, optional
The allowed percentage of different pixels in the image.
pixel_threshold : float, optional
The allowed percentage of difference for a single pixel.
name : str | None, optional
The name of the screenshot without an extension. If not provided, the name
of the test function will be used.
fail_fast : bool, optional
If True, the comparison will stop at the first pixel mismatch.
"""
@pytest.fixture(scope="session")
def output_folder(pytestconfig: Any) -> Path:
"""Fixture that returns the directory that is used for all test failures information.
This includes:
- snapshot-tests-failures: This directory contains all the snapshots that did not
match with the snapshots from past runs. The folder structure is based on the folder
structure used in the main snapshots folder.
- snapshot-updates: This directory contains all the snapshots that got updated in
the current run based on folder structure used in the main snapshots folder.
"""
return Path(pytestconfig.getoption("--output")).resolve()
@pytest.fixture(scope="function")
def assert_snapshot(
request: FixtureRequest, output_folder: Path
) -> Generator[ImageCompareFunction, None, None]:
"""Fixture that compares a screenshot with screenshot from a past run."""
root_path = Path(os.getcwd()).resolve()
platform = str(sys.platform)
module_name = request.module.__name__.split(".")[-1]
test_function_name = request.node.originalname
snapshot_dir: Path = root_path / "__snapshots__" / platform / module_name
module_snapshot_failures_dir: Path = (
output_folder / "snapshot-tests-failures" / platform / module_name
)
module_snapshot_updates_dir: Path = (
output_folder / "snapshot-updates" / platform / module_name
)
snapshot_file_suffix = ""
# Extract the parameter ids if they exist
match = re.search(r"\[(.*?)\]", request.node.name)
if match:
snapshot_file_suffix = f"[{match.group(1)}]"
snapshot_default_file_name: str = test_function_name + snapshot_file_suffix
test_failure_messages: List[str] = []
def compare(
element: ElementHandle | Locator | Page,
*,
image_threshold: float = 0.002,
pixel_threshold: float = 0.05,
name: str | None = None,
fail_fast: bool = False,
file_type: Literal["png", "jpg"] = "png",
) -> None:
"""Compare a screenshot with screenshot from a past run.
Parameters
----------
element : ElementHandle or Locator
The element to take a screenshot of.
image_threshold : float, optional
The allowed percentage of different pixels in the image.
pixel_threshold : float, optional
The allowed percentage of difference for a single pixel to be considered
different.
name : str | None, optional
The name of the screenshot without an extension. If not provided, the name
of the test function will be used.
fail_fast : bool, optional
If True, the comparison will stop at the first pixel mismatch.
file_type: "png" or "jpg"
The file type of the screenshot. Defaults to "png".
"""
nonlocal test_failure_messages
nonlocal snapshot_default_file_name
nonlocal module_snapshot_updates_dir
nonlocal module_snapshot_failures_dir
nonlocal snapshot_file_suffix
if file_type == "jpg":
file_extension = ".jpg"
img_bytes = element.screenshot(
type="jpeg", quality=90, animations="disabled"
)
else:
file_extension = ".png"
img_bytes = element.screenshot(type="png", animations="disabled")
snapshot_file_name: str = snapshot_default_file_name
if name:
snapshot_file_name = name + snapshot_file_suffix
snapshot_file_path: Path = (
snapshot_dir / f"{snapshot_file_name}{file_extension}"
)
snapshot_updates_file_path: Path = (
module_snapshot_updates_dir / f"{snapshot_file_name}{file_extension}"
)
snapshot_file_path.parent.mkdir(parents=True, exist_ok=True)
test_failures_dir = module_snapshot_failures_dir / snapshot_file_name
if test_failures_dir.exists():
# Remove the past runs failure dir for this specific screenshot
shutil.rmtree(test_failures_dir)
if not snapshot_file_path.exists():
snapshot_file_path.write_bytes(img_bytes)
# Update this in updates folder:
snapshot_updates_file_path.parent.mkdir(parents=True, exist_ok=True)
snapshot_updates_file_path.write_bytes(img_bytes)
# For missing snapshots, we don't want to directly fail in order to generate
# all missing snapshots in one run.
test_failure_messages.append(f"Missing snapshot for {snapshot_file_name}")
return
from pixelmatch.contrib.PIL import pixelmatch
# Compare the new screenshot with the screenshot from past runs:
img_a = Image.open(BytesIO(img_bytes))
img_b = Image.open(snapshot_file_path)
img_diff = Image.new("RGBA", img_a.size)
try:
mismatch = pixelmatch(
img_a,
img_b,
img_diff,
threshold=pixel_threshold,
fail_fast=fail_fast,
alpha=0,
)
except ValueError as ex:
# ValueError is thrown when the images have different sizes
# Update this in updates folder:
snapshot_updates_file_path.parent.mkdir(parents=True, exist_ok=True)
snapshot_updates_file_path.write_bytes(img_bytes)
pytest.fail(f"Snapshot matching for {snapshot_file_name} failed: {ex}")
max_diff_pixels = int(image_threshold * img_a.size[0] * img_a.size[1])
if mismatch < max_diff_pixels:
return
# Update this in updates folder:
snapshot_updates_file_path.parent.mkdir(parents=True, exist_ok=True)
snapshot_updates_file_path.write_bytes(img_bytes)
# Create new failures folder for this test:
test_failures_dir.mkdir(parents=True, exist_ok=True)
img_diff.save(f"{test_failures_dir}/diff_{snapshot_file_name}{file_extension}")
img_a.save(f"{test_failures_dir}/actual_{snapshot_file_name}{file_extension}")
img_b.save(f"{test_failures_dir}/expected_{snapshot_file_name}{file_extension}")
pytest.fail(
f"Snapshot mismatch for {snapshot_file_name} ({mismatch} pixels difference)"
)
yield compare
if test_failure_messages:
pytest.fail("Missing snapshots: \n" + "\n".join(test_failure_messages))
# Public utility methods:
def wait_for_app_run(page: Page, wait_delay: int = 100):
"""Wait for the given page to finish running."""
page.wait_for_selector(
"[data-testid='stStatusWidget']", timeout=20000, state="detached"
)
if wait_delay > 0:
# Give the app a little more time to render everything
page.wait_for_timeout(wait_delay)
def wait_for_app_loaded(page: Page, embedded: bool = False):
"""Wait for the app to fully load."""
# Wait for the app view container to appear:
page.wait_for_selector(
"[data-testid='stAppViewContainer']", timeout=30000, state="attached"
)
# Wait for the main menu to appear:
if not embedded:
page.wait_for_selector(
"[data-testid='stMainMenu']", timeout=20000, state="attached"
)
wait_for_app_run(page)
def rerun_app(page: Page):
"""Triggers an app rerun and waits for the run to be finished."""
# Click somewhere to clear the focus from elements:
page.get_by_test_id("stApp").click(position={"x": 0, "y": 0})
# Press "r" to rerun the app:
page.keyboard.press("r")
wait_for_app_run(page)
def wait_until(page: Page, fn: callable, timeout: int = 5000, interval: int = 100):
"""Run a test function in a loop until it evaluates to True
or times out.
For example:
>>> wait_until(lambda: x.values() == ['x'], page)
Parameters
----------
page : playwright.sync_api.Page
Playwright page
fn : callable
Callback
timeout : int, optional
Total timeout in milliseconds, by default 5000
interval : int, optional
Waiting interval, by default 100
Adapted from panel.
"""
# Hide this function traceback from the pytest output if the test fails
__tracebackhide__ = True
start = time.time()
def timed_out():
elapsed = time.time() - start
elapsed_ms = elapsed * 1000
return elapsed_ms > timeout
timeout_msg = f"wait_until timed out in {timeout} milliseconds"
while True:
try:
result = fn()
except AssertionError as e:
if timed_out():
raise TimeoutError(timeout_msg) from e
else:
if result not in (None, True, False):
raise ValueError(
"`wait_until` callback must return None, True or "
f"False, returned {result!r}"
)
# Stop is result is True or None
# None is returned when the function has an assert
if result is None or result:
return
if timed_out():
raise TimeoutError(timeout_msg)
page.wait_for_timeout(interval)