The headless browser for AI agents and web scraping
Merge remote-tracking branch 'origin/main' into maintainer/pr-492-fix
SGavrl108e7774182d
diff --cc crates/obscura-js/src/runtime.rsindex ebaeed37,de4d7345..6ef38862--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@@ -1317,66 -1317,104 +1317,164 @@@ mod testsrt}+ #[tokio::test(flavor = "current_thread")]+ async fn string_timeout_handler_executes_in_global_scope() {+ let mut rt = setup_runtime("<html><body></body></html>");+ rt.evaluate(+ "var __timerValue='pending'; setTimeout('__timerValue=\"done\"', 0)",+ )+ .unwrap();+ rt.run_event_loop_bounded(100).await.unwrap();+ assert_eq!(+ rt.evaluate("globalThis.__timerValue").unwrap(),+ serde_json::json!("done")+ );+ }++ #[tokio::test(flavor = "current_thread")]+ async fn string_interval_handler_repeats_and_can_clear_itself() {+ let mut rt = setup_runtime("<html><body></body></html>");+ rt.evaluate("globalThis.__ticks=0").unwrap();+ rt.evaluate(+ "globalThis.__timerId=setInterval('__ticks++;if(__ticks===2)clearInterval(__timerId)',1)",+ )+ .unwrap();+ rt.run_event_loop_bounded(100).await.unwrap();+ assert_eq!(+ rt.evaluate("globalThis.__ticks").unwrap(),+ serde_json::json!(2.0)+ );+ }++ #[test]+ fn performance_now_is_monotonic_under_bursty_calls() {+ let mut rt = setup_runtime("<html><body></body></html>");+ // Hammer performance.now() so many calls land in the same millisecond and+ // the wall clock rolls over repeatedly; the value must never go backwards.+ let violations = rt+ .evaluate(+ "(function(){var prev=-Infinity, bad=0; for(var i=0;i<500000;i++){var t=performance.now(); if(t<prev) bad++; prev=t;} return bad;})()",+ )+ .unwrap();+ assert_eq!(violations.as_f64(), Some(0.0), "performance.now() went backwards");+ }++ #[test]+ fn performance_now_does_not_outrun_elapsed_time() {+ let mut rt = setup_runtime("<html><body></body></html>");+ let lead = rt+ .evaluate(+ "(function(){for(var i=0;i<500000;i++)performance.now(); return performance.now()-(Date.now()-performance.timeOrigin);})()",+ )+ .unwrap();+ assert!(+ lead.as_f64().unwrap() <= 1.0,+ "performance.now() advanced ahead of elapsed time: {lead}"+ );+ }++ #[test]+ fn childnode_helpers_coerce_non_string_primitives_to_text() {+ let mut rt = setup_runtime(r#"<html><body><div id="p"><span id="t">x</span></div></body></html>"#);+ let before = rt+ .evaluate("(function(){var t=document.getElementById('t'); t.before(5); return t.previousSibling ? t.previousSibling.textContent : 'NULL';})()")+ .unwrap();+ assert_eq!(before, serde_json::json!("5"));+ let after = rt+ .evaluate("(function(){var t=document.getElementById('t'); t.after(true); return t.nextSibling ? t.nextSibling.textContent : 'NULL';})()")+ .unwrap();+ assert_eq!(after, serde_json::json!("true"));+ let replaced = rt+ .evaluate("(function(){var t=document.getElementById('t'); t.replaceWith(42); return document.getElementById('p').textContent;})()")+ .unwrap();+ assert!(+ replaced.as_str().unwrap().contains("42"),+ "replaceWith(42) should leave text '42': {replaced}"+ );+ }++ #[test]+ fn replace_state_without_url_preserves_current_location() {+ let mut rt = setup_runtime("<html><body></body></html>");+ let path = rt+ .evaluate(+ "(function(){history.pushState({}, '', '/dashboard'); history.replaceState({scroll:1}); return location.pathname;})()",+ )+ .unwrap();+ assert_eq!(path, serde_json::json!("/dashboard"));+ }++ #[test]+ fn push_state_without_url_preserves_current_location() {+ let mut rt = setup_runtime("<html><body></body></html>");+ let path = rt+ .evaluate(+ "(function(){history.pushState({}, '', '/a'); history.pushState({b:1}); return location.pathname;})()",+ )+ .unwrap();+ assert_eq!(path, serde_json::json!("/a"));+ }++ #[test]+ fn style_attribute_parses_into_style_object() {+ // Inline styles present in the parsed HTML must be visible via el.style.*+ let mut rt = setup_runtime(+ r#"<html><body><div id="d" style="color: red; display: none">hi</div></body></html>"#,+ );+ assert_eq!(+ rt.evaluate("document.getElementById('d').style.color").unwrap(),+ serde_json::json!("red")+ );+ assert_eq!(+ rt.evaluate("document.getElementById('d').style.display").unwrap(),+ serde_json::json!("none")+ );+ }++ #[test]+ fn set_style_attribute_updates_style_object() {+ let mut rt = setup_runtime(r#"<html><body><div id="d">hi</div></body></html>"#);+ let margin = rt+ .evaluate(+ "(function(){var e=document.getElementById('d'); e.setAttribute('style','margin: 5px'); return e.style.margin;})()",+ )+ .unwrap();+ assert_eq!(margin, serde_json::json!("5px"));+ }++ #[test]+ fn setting_style_property_updates_the_attribute_and_serialization() {+ let mut rt = setup_runtime(r#"<html><body><div id="d">hi</div></body></html>"#);+ let attr = rt+ .evaluate(+ "(function(){var e=document.getElementById('d'); e.style.color='blue'; return e.getAttribute('style');})()",+ )+ .unwrap();+ assert_eq!(attr, serde_json::json!("color: blue;"));+ let html = rt+ .evaluate("document.getElementById('d').outerHTML")+ .unwrap();+ assert!(+ html.as_str().unwrap().contains("color: blue"),+ "outerHTML should carry the style set via el.style: {html}"+ );+ }++ #[test]+ fn style_object_reflects_external_attribute_change() {+ // A later setAttribute('style', …) must supersede an earlier value read+ // through el.style (the declaration re-syncs from the attribute).+ let mut rt = setup_runtime(+ r#"<html><body><div id="d" style="color: red">hi</div></body></html>"#,+ );+ let color = rt+ .evaluate(+ "(function(){var e=document.getElementById('d'); e.style.color; e.setAttribute('style','color: green'); return e.style.color;})()",+ )+ .unwrap();+ assert_eq!(color, serde_json::json!("green"));+ }+#[test]fn test_document_title() {let mut rt = setup_runtime("<html><head><title>Test</title></head><body></body></html>");