Skip to content

The headless browser for AI agents and web scraping

Add deterministic animation capture sampling

SGavrl04277acd54a7
diff --git a/render-repros/paired-corpus.py b/render-repros/paired-corpus.py
index 4ad6f20d..73bc4a86 100755
--- a/render-repros/paired-corpus.py
+++ b/render-repros/paired-corpus.py
@@ -704,6 +704,58 @@ def capture_chromium_state(page, geometry_selectors=None):
return state
+def freeze_chromium_animations(page, sample_ms):
+ """Pause currently exposed Web Animations at one explicit local time.
+
+ This is opt-in: live wall-clock captures remain useful for runtime
+ failures, while deterministic CSS-animation comparisons need Chromium
+ sampled at the same T=0 used by the static renderer.
+ """
+ return page.evaluate(
+ """sampleMs => {
+ if (typeof document.getAnimations !== "function") {
+ return {
+ supported: false,
+ requested_ms: sampleMs,
+ discovered: 0,
+ frozen: 0,
+ failures: []
+ };
+ }
+ const animations = document.getAnimations();
+ const failures = [];
+ let frozen = 0;
+ for (let index = 0; index < animations.length; index++) {
+ const animation = animations[index];
+ try {
+ animation.pause();
+ animation.currentTime = sampleMs;
+ frozen++;
+ } catch (error) {
+ failures.push({
+ index,
+ name: error && error.name ? String(error.name) : "Error",
+ message: error && error.message
+ ? String(error.message)
+ : String(error)
+ });
+ }
+ }
+ if (document.documentElement) {
+ void document.documentElement.getBoundingClientRect().width;
+ }
+ return {
+ supported: true,
+ requested_ms: sampleMs,
+ discovered: animations.length,
+ frozen,
+ failures
+ };
+ }""",
+ sample_ms,
+ )
+
+
def load_rgb(path):
return np.asarray(Image.open(path).convert("RGB"))
@@ -871,6 +923,16 @@ def main():
parser.add_argument("--width", type=int, default=1280)
parser.add_argument("--height", type=int, default=1400)
parser.add_argument("--settle-ms", type=int, default=3000)
+ parser.add_argument(
+ "--animation-time-ms",
+ type=int,
+ choices=[0],
+ help=(
+ "pause Chromium animations currently exposed through Web Animations "
+ "at T=0 immediately before state sampling and screenshot paint; "
+ "matches Obscura's deterministic static animation sample"
+ ),
+ )
parser.add_argument(
"--geometry-selector",
action="append",
@@ -923,6 +985,19 @@ def main():
"controlled scroll is requested, the same interval runs once "
"after load and once after scrolling"
),
+ "animation_sampling": (
+ {
+ "mode": "deterministic-active-web-animations",
+ "sample_ms": args.animation_time_ms,
+ "chromium": (
+ "document.getAnimations() results are paused and assigned "
+ "currentTime immediately before state and screenshot capture"
+ ),
+ "obscura": "static renderer animation sample time T=0",
+ }
+ if args.animation_time_ms is not None
+ else {"mode": "live-wall-clock", "sample_ms": None}
+ ),
"controlled_scroll": (
{"x": controlled_scroll[0], "y": controlled_scroll[1]}
if controlled_scroll is not None
@@ -982,6 +1057,12 @@ def main():
"paint may then fetch retained renderer resources that are not "
"reflected back into HTMLImageElement or FontFaceSet state."
),
+ "animation_sampling": (
+ "The deterministic option controls only animations still "
+ "exposed by document.getAnimations() at capture time. Finished "
+ "fill-none animations and script-driven visual state that has "
+ "already been discarded cannot be rewound by this harness."
+ ),
},
"pages": [],
}
@@ -1119,11 +1200,20 @@ def main():
[scroll_x, scroll_y],
)
page.wait_for_timeout(args.settle_ms)
+ animation_sampling = None
+ if args.animation_time_ms is not None:
+ animation_sampling = freeze_chromium_animations(
+ page, args.animation_time_ms
+ )
chromium_state_ok = False
try:
chromium_state = capture_chromium_state(
page, args.geometry_selector
)
+ if animation_sampling is not None:
+ chromium_state["animation_sampling"] = (
+ animation_sampling
+ )
chromium_state["media"]["matches_configured"] = (
media_matches_configured(chromium_state["media"])
)
diff --git a/render-repros/test_paired_corpus.py b/render-repros/test_paired_corpus.py
index fd9165ef..b385d489 100644
--- a/render-repros/test_paired_corpus.py
+++ b/render-repros/test_paired_corpus.py
@@ -245,5 +245,33 @@ class GeometryProbeTests(unittest.TestCase):
self.assertEqual(comparison[1]["rects_compared"], 0)
+class AnimationSamplingTests(unittest.TestCase):
+ class FakePage:
+ def __init__(self):
+ self.calls = []
+
+ def evaluate(self, expression, *args):
+ self.calls.append((expression, args))
+ return {
+ "supported": True,
+ "requested_ms": args[0],
+ "discovered": 3,
+ "frozen": 3,
+ "failures": [],
+ }
+
+ def test_explicit_sample_pauses_and_seeks_current_animations(self):
+ page = self.FakePage()
+ result = paired_corpus.freeze_chromium_animations(page, 0)
+ self.assertEqual(result["requested_ms"], 0)
+ self.assertEqual(len(page.calls), 1)
+ expression, args = page.calls[0]
+ self.assertEqual(args, (0,))
+ self.assertIn("document.getAnimations()", expression)
+ self.assertIn("animation.pause()", expression)
+ self.assertIn("animation.currentTime = sampleMs", expression)
+ self.assertIn("getBoundingClientRect", expression)
+
+
if __name__ == "__main__":
unittest.main()