use std::collections::HashMap; use serde_json::Value; use super::cdp::client::CdpClient; use super::cdp::types::*; use super::element::{resolve_element_center, resolve_element_object_id, RefMap}; /// Outcome of a click. `dialog_opened` is true if a JavaScript dialog opened /// mid-sequence (the page is then blocked until `dialog accept`/`dismiss`). /// `pending_release` is set only when the dialog opened after mousePressed but /// before mouseReleased: the button is logically held until the caller /// dispatches the release (done once the dialog is resolved), otherwise the /// next click would register as a drag or double-click. #[derive(Default)] pub struct ClickResult { pub dialog_opened: bool, pub pending_release: Option, } pub struct PendingRelease { pub session_id: String, pub x: f64, pub y: f64, pub button: String, } pub async fn click( client: &CdpClient, session_id: &str, ref_map: &RefMap, selector_or_ref: &str, button: &str, click_count: i32, iframe_sessions: &HashMap, ) -> Result { let (x, y, effective_session_id) = resolve_element_center( client, session_id, ref_map, selector_or_ref, iframe_sessions, ) .await?; // A click-triggered dialog can fire on the frame's own session (OOPIF) or // on the top-level page session; both count as "ours". A dialog on any // other session belongs to a background tab and must not abort this click. dispatch_click( client, &effective_session_id, &[effective_session_id.as_str(), session_id], x, y, button, click_count, ) .await } pub async fn dblclick( client: &CdpClient, session_id: &str, ref_map: &RefMap, selector_or_ref: &str, iframe_sessions: &HashMap, ) -> Result { click( client, session_id, ref_map, selector_or_ref, "left", 2, iframe_sessions, ) .await } pub async fn hover( client: &CdpClient, session_id: &str, ref_map: &RefMap, selector_or_ref: &str, iframe_sessions: &HashMap, ) -> Result<(), String> { let (x, y, effective_session_id) = resolve_element_center( client, session_id, ref_map, selector_or_ref, iframe_sessions, ) .await?; client .send_command_typed::<_, Value>( "Input.dispatchMouseEvent", &DispatchMouseEventParams { event_type: "mouseMoved".to_string(), x, y, button: None, buttons: None, click_count: None, delta_x: None, delta_y: None, modifiers: None, }, Some(&effective_session_id), ) .await?; Ok(()) } pub async fn fill( client: &CdpClient, session_id: &str, ref_map: &RefMap, selector_or_ref: &str, value: &str, iframe_sessions: &HashMap, ) -> Result<(), String> { let (object_id, effective_session_id) = resolve_element_object_id( client, session_id, ref_map, selector_or_ref, iframe_sessions, ) .await?; // Focus the element client .send_command_typed::<_, Value>( "Runtime.callFunctionOn", &CallFunctionOnParams { function_declaration: "function() { this.focus(); }".to_string(), object_id: Some(object_id.clone()), arguments: None, return_by_value: Some(true), await_promise: Some(false), }, Some(&effective_session_id), ) .await?; // Select all + delete to clear client .send_command_typed::<_, Value>( "Runtime.callFunctionOn", &CallFunctionOnParams { function_declaration: r#"function() { this.select && this.select(); this.value = ''; this.dispatchEvent(new Event('input', { bubbles: true })); }"# .to_string(), object_id: Some(object_id), arguments: None, return_by_value: Some(true), await_promise: Some(false), }, Some(&effective_session_id), ) .await?; // Insert text (keyboard input dispatched at page level, use parent session_id) client .send_command_typed::<_, Value>( "Input.insertText", &InsertTextParams { text: value.to_string(), }, Some(session_id), ) .await?; Ok(()) } #[allow(clippy::too_many_arguments)] pub async fn type_text( client: &CdpClient, session_id: &str, ref_map: &RefMap, selector_or_ref: &str, text: &str, clear: bool, delay_ms: Option, iframe_sessions: &HashMap, ) -> Result<(), String> { let (object_id, effective_session_id) = resolve_element_object_id( client, session_id, ref_map, selector_or_ref, iframe_sessions, ) .await?; // Focus client .send_command_typed::<_, Value>( "Runtime.callFunctionOn", &CallFunctionOnParams { function_declaration: "function() { this.focus(); }".to_string(), object_id: Some(object_id.clone()), arguments: None, return_by_value: Some(true), await_promise: Some(false), }, Some(&effective_session_id), ) .await?; if clear { client .send_command_typed::<_, Value>( "Runtime.callFunctionOn", &CallFunctionOnParams { function_declaration: r#"function() { this.select && this.select(); this.value = ''; this.dispatchEvent(new Event('input', { bubbles: true })); }"# .to_string(), object_id: Some(object_id), arguments: None, return_by_value: Some(true), await_promise: Some(false), }, Some(&effective_session_id), ) .await?; } type_text_into_active_context(client, session_id, text, delay_ms).await } pub async fn type_text_into_active_context( client: &CdpClient, session_id: &str, text: &str, delay_ms: Option, ) -> Result<(), String> { let delay = delay_ms.unwrap_or(0); for ch in text.chars() { if matches!(ch, '\n' | '\r' | '\t') { let (key, code, key_code) = char_to_key_info(ch); let text_str = key_text(&key); client .send_command_typed::<_, Value>( "Input.dispatchKeyEvent", &DispatchKeyEventParams { event_type: "keyDown".to_string(), key: Some(key.clone()), code: Some(code.clone()), text: text_str.clone(), unmodified_text: text_str, windows_virtual_key_code: Some(key_code), native_virtual_key_code: Some(key_code), modifiers: None, }, Some(session_id), ) .await?; client .send_command_typed::<_, Value>( "Input.dispatchKeyEvent", &DispatchKeyEventParams { event_type: "keyUp".to_string(), key: Some(key), code: Some(code), text: None, unmodified_text: None, windows_virtual_key_code: Some(key_code), native_virtual_key_code: Some(key_code), modifiers: None, }, Some(session_id), ) .await?; } else { // VS Code/Electron webviews reject repeated dispatchKeyEvent calls // carrying printable `text`. Insert printable characters directly // and reserve key events for controls like Enter and Tab. client .send_command_typed::<_, Value>( "Input.insertText", &InsertTextParams { text: ch.to_string(), }, Some(session_id), ) .await?; } if delay > 0 { tokio::time::sleep(tokio::time::Duration::from_millis(delay)).await; } } Ok(()) } pub async fn press_key(client: &CdpClient, session_id: &str, key: &str) -> Result<(), String> { press_key_with_modifiers(client, session_id, key, None).await } /// Dispatch a keyDown+keyUp sequence for `key` with an optional CDP modifier bitmask. /// /// Modifier values follow the CDP `Input.dispatchKeyEvent` spec: /// 1 = Alt, 2 = Control, 4 = Meta (Cmd), 8 = Shift. /// /// Callers that need a platform-appropriate modifier (e.g. Cmd on macOS, /// Ctrl elsewhere) must choose the value themselves -- see `cfg!(target_os)`. pub async fn press_key_with_modifiers( client: &CdpClient, session_id: &str, key: &str, modifiers: Option, ) -> Result<(), String> { let (key_name, code, key_code) = named_key_info(key); // Suppress text insertion when Control (2) or Meta (4) modifiers are active, // since these are command chords (e.g. Ctrl+A = select-all), not text input. let has_command_modifier = modifiers.is_some_and(|m| m & (2 | 4) != 0); let text = if has_command_modifier { None } else { key_text(&key_name) }; client .send_command_typed::<_, Value>( "Input.dispatchKeyEvent", &DispatchKeyEventParams { event_type: "keyDown".to_string(), key: Some(key_name.clone()), code: Some(code.clone()), text: text.clone(), unmodified_text: text.clone(), windows_virtual_key_code: Some(key_code), native_virtual_key_code: Some(key_code), modifiers, }, Some(session_id), ) .await?; client .send_command_typed::<_, Value>( "Input.dispatchKeyEvent", &DispatchKeyEventParams { event_type: "keyUp".to_string(), key: Some(key_name), code: Some(code), text: None, unmodified_text: None, windows_virtual_key_code: Some(key_code), native_virtual_key_code: Some(key_code), modifiers, }, Some(session_id), ) .await?; Ok(()) } pub async fn scroll( client: &CdpClient, session_id: &str, ref_map: &RefMap, selector_or_ref: Option<&str>, delta_x: f64, delta_y: f64, iframe_sessions: &HashMap, ) -> Result<(), String> { if let Some(sel) = selector_or_ref { let (object_id, effective_session_id) = resolve_element_object_id(client, session_id, ref_map, sel, iframe_sessions).await?; let js = "function(dx, dy) { this.scrollBy(dx, dy); }".to_string(); client .send_command_typed::<_, Value>( "Runtime.callFunctionOn", &CallFunctionOnParams { function_declaration: js, object_id: Some(object_id), arguments: Some(vec![ CallArgument { value: Some(serde_json::json!(delta_x)), object_id: None, }, CallArgument { value: Some(serde_json::json!(delta_y)), object_id: None, }, ]), return_by_value: Some(true), await_promise: Some(false), }, Some(&effective_session_id), ) .await?; } else { let js = format!("window.scrollBy({}, {})", delta_x, delta_y); client .send_command_typed::<_, Value>( "Runtime.evaluate", &EvaluateParams { expression: js, return_by_value: Some(true), await_promise: Some(false), }, Some(session_id), ) .await?; } Ok(()) } pub async fn select_option( client: &CdpClient, session_id: &str, ref_map: &RefMap, selector_or_ref: &str, values: &[String], iframe_sessions: &HashMap, ) -> Result<(), String> { let (object_id, effective_session_id) = resolve_element_object_id( client, session_id, ref_map, selector_or_ref, iframe_sessions, ) .await?; // Matching nothing must be an error, not a silent success: an agent that // selects a misspelled option otherwise sees "Done", and only discovers // the page state is wrong after more commands. List what was available. let js = r#"function(vals) { const options = Array.from(this.options); let matched = 0; for (const opt of options) { opt.selected = vals.includes(opt.value) || vals.includes(opt.textContent.trim()); if (opt.selected) matched += 1; } if (matched === 0) { const available = options.map(o => o.value + ' ("' + o.textContent.trim() + '")').join(', '); return { error: 'No option matched ' + JSON.stringify(vals) + '. Available options: ' + available }; } this.dispatchEvent(new Event('change', { bubbles: true })); return { matched }; }"# .to_string(); let result = client .send_command_typed::<_, Value>( "Runtime.callFunctionOn", &CallFunctionOnParams { function_declaration: js, object_id: Some(object_id), arguments: Some(vec![CallArgument { value: Some(serde_json::json!(values)), object_id: None, }]), return_by_value: Some(true), await_promise: Some(false), }, Some(&effective_session_id), ) .await?; if let Some(error) = result .get("result") .and_then(|r| r.get("value")) .and_then(|v| v.get("error")) .and_then(|e| e.as_str()) { return Err(error.to_string()); } Ok(()) } pub async fn check( client: &CdpClient, session_id: &str, ref_map: &RefMap, selector_or_ref: &str, iframe_sessions: &HashMap, ) -> Result<(), String> { let is_checked = super::element::is_element_checked( client, session_id, ref_map, selector_or_ref, iframe_sessions, ) .await?; if !is_checked { click( client, session_id, ref_map, selector_or_ref, "left", 1, iframe_sessions, ) .await?; // Verify the click changed the state (Playwright parity: _setChecked re-checks). // If the coordinate-based click missed (e.g. hidden input, overlay), retry // with a JS .click() on the element and its associated input. if !super::element::is_element_checked( client, session_id, ref_map, selector_or_ref, iframe_sessions, ) .await? { js_click_checkbox( client, session_id, ref_map, selector_or_ref, iframe_sessions, ) .await?; } } Ok(()) } pub async fn uncheck( client: &CdpClient, session_id: &str, ref_map: &RefMap, selector_or_ref: &str, iframe_sessions: &HashMap, ) -> Result<(), String> { let is_checked = super::element::is_element_checked( client, session_id, ref_map, selector_or_ref, iframe_sessions, ) .await?; if is_checked { click( client, session_id, ref_map, selector_or_ref, "left", 1, iframe_sessions, ) .await?; // Same verify-and-retry as check(). if super::element::is_element_checked( client, session_id, ref_map, selector_or_ref, iframe_sessions, ) .await? { js_click_checkbox( client, session_id, ref_map, selector_or_ref, iframe_sessions, ) .await?; } } Ok(()) } /// Fallback for when the coordinate-based CDP click did not toggle the /// checkbox/radio state. This mirrors how Playwright dispatches clicks /// through the DOM rather than via raw Input.dispatchMouseEvent coordinates. /// /// Uses the same follow-label resolution as `is_element_checked`: /// 1. If the element is a native input → `.click()` it directly. /// 2. If the element is inside a `