Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(emulation): apply focus overrides to loaded targets
ldm0f911b83d394c
diff --git a/moli-protocol/src/domains/emulation.rs b/moli-protocol/src/domains/emulation.rsindex af36f6727..04c0df9d5 100644--- a/moli-protocol/src/domains/emulation.rs+++ b/moli-protocol/src/domains/emulation.rs@@ -193,9 +193,7 @@ pub(crate) fn try_start_emulation_command_dispatch(EmulationCommandTaskStep::Complete(CommandOutputPlan::result(json!({}))),),Some(EmulationAction::SetFocusEmulationEnabled) => {- Some(EmulationCommandTaskStep::Complete(- focus_emulation_enabled_command_output_plan(conn, cmd),- ))+ Some(start_focus_emulation_enabled_command(conn, cmd))}Some(EmulationAction::SetDeviceMetricsOverride) => {Some(start_device_metrics_override_command(conn, cmd))@@ -241,26 +239,48 @@ pub(crate) fn try_start_emulation_command_dispatch(}}-fn focus_emulation_enabled_command_output_plan(+fn start_focus_emulation_enabled_command(conn: &mut CdpConnection,cmd: &Cmd<'_>,-) -> CommandOutputPlan {+) -> EmulationCommandTaskStep {let params: params::SetFocusEmulationEnabledParams = match cmd.get_params() {Ok(Some(params)) => params,- _ => return CommandOutputPlan::error(-32602, "InvalidParams"),+ _ => {+ return EmulationCommandTaskStep::Complete(CommandOutputPlan::error(+ -32602,+ "InvalidParams",+ ));+ }};if conn.browser_context.is_none() {- return CommandOutputPlan::result(json!({}));+ return EmulationCommandTaskStep::Complete(CommandOutputPlan::success());}- match page_session::update_page_emulation_state(conn, cmd.session_id, |mut state| {- state.set_focus_emulation_enabled(params.enabled);- }) {- Ok(()) => CommandOutputPlan::result(json!({})),- Err(message) if message == "BrowserContextNotLoaded" => {- CommandOutputPlan::error(-31998, "BrowserContextNotLoaded")+ if let Err(message) =+ page_session::update_page_emulation_state(conn, cmd.session_id, |mut state| {+ state.set_focus_emulation_enabled(params.enabled);+ })+ {+ let code = if message == "BrowserContextNotLoaded" {+ -31998+ } else {+ -32000+ };+ return EmulationCommandTaskStep::Complete(CommandOutputPlan::error(code, message));+ }+ let pending = match start_surface_override_page_commands(conn, cmd) {+ Ok(pending) => pending,+ Err(error) => {+ return EmulationCommandTaskStep::Complete(CommandOutputPlan::error(-32000, error));}- Err(message) => CommandOutputPlan::error(-32000, message),+ };+ if pending.is_empty() {+ return EmulationCommandTaskStep::Complete(CommandOutputPlan::success());}+ EmulationCommandTaskStep::Pending(PendingEmulationCommandDispatch {+ command_id: cmd.id,+ session_id: cmd.session_id.map(str::to_owned),+ pending: PendingEmulationRendererDispatch::Pages(pending),+ })}fn start_touch_emulation_enabled_command(@@ -669,7 +689,7 @@ fn start_update_geolocation_override_command("BrowserContextNotLoaded",));}- let pending = match start_geolocation_surface_override_page_commands(conn, cmd) {+ let pending = match start_surface_override_page_commands(conn, cmd) {Ok(pending) => pending,Err(error) => {return EmulationCommandTaskStep::Complete(CommandOutputPlan::error(-32000, error));@@ -2688,7 +2708,7 @@ fn start_context_emulated_media_page_commands(Ok(pending)}-fn start_geolocation_surface_override_page_commands(+fn start_surface_override_page_commands(conn: &mut CdpConnection,cmd: &Cmd<'_>,) -> Result<Vec<PendingEmulationPageCommand>, String> {diff --git a/moli-protocol/src/domains/emulation/tests/native_navigator.rs b/moli-protocol/src/domains/emulation/tests/native_navigator.rsindex 8e7ec2f60..dc26fa605 100644--- a/moli-protocol/src/domains/emulation/tests/native_navigator.rs+++ b/moli-protocol/src/domains/emulation/tests/native_navigator.rs@@ -219,3 +219,63 @@ async fn native_geolocation_result_uses_receiver_realm() {})()"#).await, json!([true, false, true, 1]));}++#[tokio::test(flavor = "multi_thread")]+async fn focus_override_updates_loaded_background_page_and_preserves_real_focus() {+ let mut ctx = TestContext::new();+ let mut bc = BrowserContext::new("BID-1".into());+ bc.set_active_target_id("TID-active");+ bc.attach_active_session("SID-active");+ bc.insert_page_target_host(PageTargetHost::new(+ "TID-1".into(),+ Some("SID-1".into()),+ TargetIdentityState::about_blank(),+ TargetPageSlot::empty_for_test_fixture(),+ ));+ install_geolocation_page_for_test(&mut ctx, bc).await;+ let snapshot = "[document.hasFocus(), document.hidden, document.visibilityState]";+ assert_eq!(+ evaluate(&mut ctx, snapshot).await,+ json!([false, true, "hidden"])+ );+ for enabled in [true, true, false] {+ expect_session_command_result(+ &mut ctx,+ 88001,+ "SID-1",+ "Emulation.setFocusEmulationEnabled",+ json!({"enabled": enabled}),+ )+ .await;+ assert_eq!(+ evaluate(&mut ctx, snapshot).await,+ json!([+ enabled,+ !enabled,+ if enabled { "visible" } else { "hidden" }+ ])+ );+ assert_eq!(+ ctx.conn+ .browser_context+ .as_ref()+ .unwrap()+ .active_target_id(),+ Some("TID-active")+ );+ }+ // Removing an override does not remove the real foreground target's focus.+ let mut foreground = setup().await;+ expect_session_command_result(+ &mut foreground,+ 88001,+ "SID-1",+ "Emulation.setFocusEmulationEnabled",+ json!({"enabled": false}),+ )+ .await;+ assert_eq!(+ evaluate(&mut foreground, snapshot).await,+ json!([true, false, "visible"])+ );+}