htmlcmp 2.4.2__tar.gz → 2.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/PKG-INFO +16 -1
  2. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/README.md +15 -0
  3. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/pyproject.toml +1 -1
  4. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp/comparison_worker.py +3 -3
  5. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp/html_render_diff.py +64 -11
  6. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp.egg-info/PKG-INFO +16 -1
  7. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/tests/test_browser_transport.py +42 -0
  8. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/tests/test_comparison_worker.py +17 -3
  9. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/tests/test_html_render_diff.py +31 -1
  10. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/setup.cfg +0 -0
  11. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp/__init__.py +0 -0
  12. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp/common.py +0 -0
  13. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp/compare_output_cli.py +0 -0
  14. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp/compare_output_server.py +0 -0
  15. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp/static/common.js +0 -0
  16. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp/static/compare.js +0 -0
  17. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp/static/index.js +0 -0
  18. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp/static/style.css +0 -0
  19. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp/templates/compare.html +0 -0
  20. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp/templates/index.html +0 -0
  21. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp/templates/missing.html +0 -0
  22. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp/tidy_output.py +0 -0
  23. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp.egg-info/SOURCES.txt +0 -0
  24. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp.egg-info/dependency_links.txt +0 -0
  25. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp.egg-info/entry_points.txt +0 -0
  26. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp.egg-info/requires.txt +0 -0
  27. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/src/htmlcmp.egg-info/top_level.txt +0 -0
  28. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/tests/test_collect_tasks.py +0 -0
  29. {htmlcmp-2.4.2 → htmlcmp-2.5.0}/tests/test_compare_html_retries.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: htmlcmp
3
- Version: 2.4.2
3
+ Version: 2.5.0
4
4
  Summary: Compare HTML files by rendered output
5
5
  Author: Andreas Stefl
6
6
  Maintainer-email: Andreas Stefl <stefl.andreas@gmail.com>
@@ -30,12 +30,27 @@ Provides various entry points to run:
30
30
 
31
31
  Used for regression testing in https://github.com/opendocument-app/OpenDocument.core.
32
32
 
33
+ ## When a screenshot is taken
34
+
35
+ A page is screenshotted once it has stopped changing:
36
+
37
+ - the document has loaded and its web fonts have arrived
38
+ - no element carries `aria-busy="true"`, the WAI-ARIA way for a page that keeps
39
+ laying itself out after load to say it is not done yet
40
+ - finite animations, such as a fade-in, have finished; an infinite one is left
41
+ running
42
+ - two frames have been painted since
43
+
44
+ Waiting stops after 10 seconds, and the page is screenshotted as it is then.
45
+
33
46
  ## Browser timeouts in the CLI
34
47
 
35
48
  `compare-html --timeout 120` limits each browser comparison to 120 seconds,
36
49
  including browser startup, both screenshots, and any `--retries` of a pixel
37
50
  mismatch. The deadline starts when a worker takes the file, not while it waits
38
51
  in the queue. Use a larger positive value for especially expensive pages.
52
+ The same value configures WebDriver's HTTP and page-load timeouts, so increasing
53
+ the deadline is not cut short by Selenium's default 120-second HTTP timeout.
39
54
 
40
55
  A timeout is reported as an error and makes the command exit unsuccessfully.
41
56
  The worker and its browser processes are discarded; the next file gets a fresh
@@ -10,12 +10,27 @@ Provides various entry points to run:
10
10
 
11
11
  Used for regression testing in https://github.com/opendocument-app/OpenDocument.core.
12
12
 
13
+ ## When a screenshot is taken
14
+
15
+ A page is screenshotted once it has stopped changing:
16
+
17
+ - the document has loaded and its web fonts have arrived
18
+ - no element carries `aria-busy="true"`, the WAI-ARIA way for a page that keeps
19
+ laying itself out after load to say it is not done yet
20
+ - finite animations, such as a fade-in, have finished; an infinite one is left
21
+ running
22
+ - two frames have been painted since
23
+
24
+ Waiting stops after 10 seconds, and the page is screenshotted as it is then.
25
+
13
26
  ## Browser timeouts in the CLI
14
27
 
15
28
  `compare-html --timeout 120` limits each browser comparison to 120 seconds,
16
29
  including browser startup, both screenshots, and any `--retries` of a pixel
17
30
  mismatch. The deadline starts when a worker takes the file, not while it waits
18
31
  in the queue. Use a larger positive value for especially expensive pages.
32
+ The same value configures WebDriver's HTTP and page-load timeouts, so increasing
33
+ the deadline is not cut short by Selenium's default 120-second HTTP timeout.
19
34
 
20
35
  A timeout is reported as an error and makes the command exit unsuccessfully.
21
36
  The worker and its browser processes are discarded; the next file gets a fresh
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "htmlcmp"
3
- version = "2.4.2"
3
+ version = "2.5.0"
4
4
  description = "Compare HTML files by rendered output"
5
5
  classifiers = []
6
6
  authors = [
@@ -15,7 +15,7 @@ from htmlcmp.html_render_diff import get_browser
15
15
  logger = logging.getLogger(__name__)
16
16
 
17
17
 
18
- def _compare_loop(connection, driver):
18
+ def _compare_loop(connection, driver, timeout):
19
19
  browser = None
20
20
  try:
21
21
  while True:
@@ -28,7 +28,7 @@ def _compare_loop(connection, driver):
28
28
  and a.suffix == ".html"
29
29
  and not filecmp.cmp(a, b, shallow=False)
30
30
  ):
31
- browser = get_browser(driver)
31
+ browser = get_browser(driver, timeout=timeout)
32
32
  result = compare_files(a, b, browser=browser, **kwargs)
33
33
  connection.send((True, result))
34
34
  except Exception as exc:
@@ -52,7 +52,7 @@ class ComparisonWorker:
52
52
  context = multiprocessing.get_context("spawn")
53
53
  self._connection, child = context.Pipe()
54
54
  self._process = context.Process(
55
- target=_compare_loop, args=(child, self.driver), daemon=True
55
+ target=_compare_loop, args=(child, self.driver, self.timeout), daemon=True
56
56
  )
57
57
  try:
58
58
  self._process.start()
@@ -53,31 +53,76 @@ def screenshot(
53
53
  return Image.open(io.BytesIO(png))
54
54
 
55
55
 
56
- #: Waits for the fonts and then for two frames, and answers when both are done.
57
- #: `readyState` does not cover a web font: the load event fires while the face
58
- #: is still arriving, and the text is laid out again once it lands. Two frames
59
- #: then say a paint has happened rather than merely been asked for.
56
+ #: Waits until the page has stopped changing, then for two frames, and answers
57
+ #: when that is done or the deadline passes.
58
+ #:
59
+ #: - `readyState` does not cover a web font: the load event fires while the face
60
+ #: is still arriving, and the text is laid out again once it lands.
61
+ #: - A page that keeps laying itself out after load marks what is still changing
62
+ #: with `aria-busy="true"` (WAI-ARIA), and clears it once that is done.
63
+ #: - A finite animation, such as a fade-in, is waited out; an infinite one never
64
+ #: ends, so it is left running.
65
+ #:
66
+ #: Two frames then say a paint has happened rather than merely been asked for.
67
+ #: The checks repeat until one round finds nothing to wait for, since finishing
68
+ #: one kind of work can start another.
60
69
  _SETTLE = """
61
70
  const done = arguments[arguments.length - 1];
62
- const frames = () =>
63
- requestAnimationFrame(() => requestAnimationFrame(() => done(true)));
64
- (document.fonts ? document.fonts.ready : Promise.resolve()).then(frames, frames);
71
+ const deadline = performance.now() + arguments[0];
72
+ const left = () => Math.max(0, deadline - performance.now());
73
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
74
+ const frame = () => new Promise((resolve) => requestAnimationFrame(resolve));
75
+ const busy = () => document.querySelector('[aria-busy="true"]') !== null;
76
+ const animations = () =>
77
+ document.getAnimations
78
+ ? document.getAnimations().filter((animation) => {
79
+ const timing = animation.effect && animation.effect.getComputedTiming();
80
+ return (
81
+ animation.playState === "running" &&
82
+ timing !== null &&
83
+ timing.endTime !== Infinity
84
+ );
85
+ })
86
+ : [];
87
+ (async () => {
88
+ if (document.fonts) {
89
+ await Promise.race([document.fonts.ready.catch(() => {}), sleep(left())]);
90
+ }
91
+ for (;;) {
92
+ while (busy() && left() > 0) await sleep(50);
93
+ const running = animations();
94
+ if (running.length) {
95
+ await Promise.race([
96
+ Promise.all(running.map((a) => a.finished.catch(() => {}))),
97
+ sleep(left()),
98
+ ]);
99
+ }
100
+ await frame();
101
+ await frame();
102
+ if (left() === 0 || (!busy() && animations().length === 0)) break;
103
+ }
104
+ done(true);
105
+ })();
65
106
  """
66
107
 
108
+ #: How long `settle` waits for the page, in seconds. The page gives up a second
109
+ #: before WebDriver does, so the screenshot follows either way.
110
+ _SETTLE_TIMEOUT = 10
111
+
67
112
 
68
113
  def settle(browser: webdriver.Remote, settling_time: float = 0) -> None:
69
114
  """Waits until the page has finished painting what it loaded.
70
115
 
71
116
  A screenshot taken before that catches the page mid-render, which is what
72
117
  makes an otherwise identical pair compare as different from one run to the
73
- next.
118
+ next. A page still busy at the deadline is screenshotted as it is.
74
119
  """
75
120
  if not isinstance(browser, webdriver.Remote):
76
121
  raise TypeError(f"Expected webdriver.Remote, got {type(browser)}")
77
122
 
78
- browser.set_script_timeout(10)
123
+ browser.set_script_timeout(_SETTLE_TIMEOUT)
79
124
  try:
80
- browser.execute_async_script(_SETTLE)
125
+ browser.execute_async_script(_SETTLE, (_SETTLE_TIMEOUT - 1) * 1000)
81
126
  except WebDriverException:
82
127
  # an old driver without async scripts still gets the sleep below
83
128
  pass
@@ -100,7 +145,11 @@ def content_bottom(image: Image.Image) -> int:
100
145
 
101
146
 
102
147
  def get_browser(
103
- driver: str, max_width: int = 1000, max_height: int = 10000
148
+ driver: str,
149
+ max_width: int = 1000,
150
+ max_height: int = 10000,
151
+ *,
152
+ timeout: float | None = None,
104
153
  ) -> webdriver.Remote:
105
154
  if not isinstance(driver, str):
106
155
  raise TypeError(f"Expected str, got {type(driver)}")
@@ -122,6 +171,10 @@ def get_browser(
122
171
  # Local WebDriver constructors do not expose ClientConfig. Disable urllib3's
123
172
  # automatic GET retries on their pool: a stalled screenshot must fail once.
124
173
  browser.command_executor._conn.connection_pool_kw["retries"] = 0
174
+ if timeout is not None:
175
+ # The CLI's parent process enforces the whole comparison deadline.
176
+ browser.command_executor._client_config.timeout = timeout
177
+ browser.set_page_load_timeout(timeout)
125
178
  browser.set_window_size(max_width, max_height)
126
179
  return browser
127
180
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: htmlcmp
3
- Version: 2.4.2
3
+ Version: 2.5.0
4
4
  Summary: Compare HTML files by rendered output
5
5
  Author: Andreas Stefl
6
6
  Maintainer-email: Andreas Stefl <stefl.andreas@gmail.com>
@@ -30,12 +30,27 @@ Provides various entry points to run:
30
30
 
31
31
  Used for regression testing in https://github.com/opendocument-app/OpenDocument.core.
32
32
 
33
+ ## When a screenshot is taken
34
+
35
+ A page is screenshotted once it has stopped changing:
36
+
37
+ - the document has loaded and its web fonts have arrived
38
+ - no element carries `aria-busy="true"`, the WAI-ARIA way for a page that keeps
39
+ laying itself out after load to say it is not done yet
40
+ - finite animations, such as a fade-in, have finished; an infinite one is left
41
+ running
42
+ - two frames have been painted since
43
+
44
+ Waiting stops after 10 seconds, and the page is screenshotted as it is then.
45
+
33
46
  ## Browser timeouts in the CLI
34
47
 
35
48
  `compare-html --timeout 120` limits each browser comparison to 120 seconds,
36
49
  including browser startup, both screenshots, and any `--retries` of a pixel
37
50
  mismatch. The deadline starts when a worker takes the file, not while it waits
38
51
  in the queue. Use a larger positive value for especially expensive pages.
52
+ The same value configures WebDriver's HTTP and page-load timeouts, so increasing
53
+ the deadline is not cut short by Selenium's default 120-second HTTP timeout.
39
54
 
40
55
  A timeout is reported as an error and makes the command exit unsuccessfully.
41
56
  The worker and its browser processes are discarded; the next file gets a fresh
@@ -1,6 +1,7 @@
1
1
  """A stalled screenshot gets one HTTP request, not four timeout periods."""
2
2
 
3
3
  import threading
4
+ import time
4
5
  from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
5
6
  from unittest.mock import Mock
6
7
 
@@ -49,3 +50,44 @@ def test_screenshot_transport_does_not_retry(monkeypatch, driver, factory):
49
50
  server.shutdown()
50
51
  server.server_close()
51
52
  thread.join()
53
+
54
+
55
+ @pytest.mark.parametrize(
56
+ "driver,factory", [("firefox", "Firefox"), ("chrome", "Chrome")]
57
+ )
58
+ def test_longer_deadline_allows_a_slow_screenshot(monkeypatch, driver, factory):
59
+ class Handler(BaseHTTPRequestHandler):
60
+ def do_GET(self):
61
+ time.sleep(0.25)
62
+ body = b'{"value":"image"}'
63
+ self.send_response(200)
64
+ self.send_header("Content-Type", "application/json")
65
+ self.send_header("Content-Length", str(len(body)))
66
+ self.end_headers()
67
+ self.wfile.write(body)
68
+
69
+ def log_message(self, *args):
70
+ pass
71
+
72
+ server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
73
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
74
+ thread.start()
75
+ connection = RemoteConnection(
76
+ client_config=ClientConfig(
77
+ remote_server_addr=f"http://127.0.0.1:{server.server_port}",
78
+ timeout=0.1,
79
+ )
80
+ )
81
+ browser = Mock(command_executor=connection)
82
+ monkeypatch.setattr(render.webdriver, factory, Mock(return_value=browser))
83
+ try:
84
+ render.get_browser(driver, timeout=2)
85
+ browser.set_page_load_timeout.assert_called_once_with(2)
86
+ assert connection.execute(Command.SCREENSHOT, {"sessionId": "test"}) == {
87
+ "value": "image"
88
+ }
89
+ finally:
90
+ connection.close()
91
+ server.shutdown()
92
+ server.server_close()
93
+ thread.join()
@@ -14,7 +14,7 @@ from htmlcmp import comparison_worker as worker_module
14
14
  from htmlcmp.comparison_worker import ComparisonExecutor, ComparisonWorker
15
15
 
16
16
 
17
- def controlled_worker(connection, driver):
17
+ def controlled_worker(connection, driver, timeout):
18
18
  while True:
19
19
  request = connection.recv()
20
20
  if request is None:
@@ -35,14 +35,14 @@ def controlled_worker(connection, driver):
35
35
  connection.send((True, os.getpid()))
36
36
 
37
37
 
38
- def hanging_shutdown(connection, driver):
38
+ def hanging_shutdown(connection, driver, timeout):
39
39
  connection.recv()
40
40
  connection.send((True, os.getpid()))
41
41
  connection.recv()
42
42
  time.sleep(60)
43
43
 
44
44
 
45
- def no_startup_response(connection, driver):
45
+ def no_startup_response(connection, driver, timeout):
46
46
  time.sleep(60)
47
47
 
48
48
 
@@ -140,6 +140,20 @@ def test_identical_html_does_not_need_a_browser(tmp_path):
140
140
  assert executor.submit(executor.compare, a, b).result() is True
141
141
 
142
142
 
143
+ def test_comparison_deadline_reaches_browser_transport(monkeypatch, tmp_path):
144
+ a, b = tmp_path / "a.html", tmp_path / "b.html"
145
+ a.write_text("one")
146
+ b.write_text("two")
147
+ connection = Mock()
148
+ connection.recv.side_effect = [(a, b, {}), None]
149
+ factory = Mock()
150
+ monkeypatch.setattr(worker_module, "get_browser", factory)
151
+ monkeypatch.setattr(worker_module, "compare_files", Mock(return_value=True))
152
+ worker_module._compare_loop(connection, "firefox", 900)
153
+ factory.assert_called_once_with("firefox", timeout=900)
154
+ connection.send.assert_called_once_with((True, True))
155
+
156
+
143
157
  def test_startup_failure_is_reported_per_file(tmp_path):
144
158
  a, b = tmp_path / "a.html", tmp_path / "b.html"
145
159
  a.write_text("one")
@@ -2,7 +2,14 @@ import pytest
2
2
  from pathlib import Path
3
3
  import shutil
4
4
 
5
- from htmlcmp.html_render_diff import to_url, get_browser, html_render_diff
5
+ import time
6
+
7
+ from htmlcmp.html_render_diff import (
8
+ to_url,
9
+ get_browser,
10
+ html_render_diff,
11
+ screenshot,
12
+ )
6
13
 
7
14
 
8
15
  def test_to_url():
@@ -49,3 +56,26 @@ def test_html_render_diff():
49
56
  assert diff.getbbox() is not None
50
57
  finally:
51
58
  browser.quit()
59
+
60
+
61
+ @pytest.mark.parametrize("driver", ["chrome", "firefox"])
62
+ @pytest.mark.parametrize("page", ["busy.html", "animated.html"])
63
+ def test_screenshot_waits_for_the_page_to_settle(driver, page):
64
+ here = Path(__file__).parent
65
+ browser = get_browser(driver)
66
+ try:
67
+ diff, _ = html_render_diff(here / page, here / "settled.html", browser)
68
+ assert diff.getbbox() is None
69
+ finally:
70
+ browser.quit()
71
+
72
+
73
+ @pytest.mark.parametrize("driver", ["chrome", "firefox"])
74
+ def test_screenshot_leaves_an_infinite_animation_running(driver):
75
+ browser = get_browser(driver)
76
+ try:
77
+ started = time.monotonic()
78
+ screenshot(browser, to_url(Path(__file__).parent / "spinning.html"))
79
+ assert time.monotonic() - started < 5
80
+ finally:
81
+ browser.quit()
File without changes
File without changes
File without changes