cdp_driver/types.rs
1//! Plain data types returned by CDP commands and events.
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6/// Response body of `GET /json/version`.
7#[derive(Debug, Clone, Deserialize, Serialize)]
8pub struct BrowserVersion {
9 /// Browser name and version, e.g. `"HeadlessChrome/120.0.0.0"`.
10 #[serde(rename = "Browser")]
11 pub browser: String,
12 /// The CDP protocol version Chrome speaks.
13 #[serde(rename = "Protocol-Version")]
14 pub protocol_version: String,
15 /// WebSocket URL for the browser-level debugger, if exposed.
16 #[serde(rename = "webSocketDebuggerUrl")]
17 pub web_socket_debugger_url: Option<String>,
18}
19
20/// A debuggable target (tab, worker, etc.) as reported by `GET /json/list`.
21#[derive(Debug, Clone, Deserialize, Serialize)]
22pub struct Target {
23 /// Chrome's opaque target id.
24 pub id: String,
25 /// Target kind, e.g. `"page"`. [`CdpClient::connect_to_page`](crate::client::CdpClient::connect_to_page)
26 /// only connects to targets of this type.
27 #[serde(rename = "type")]
28 pub target_type: String,
29 /// Current page/tab title.
30 pub title: String,
31 /// Current page/tab URL.
32 pub url: String,
33 /// WebSocket URL to open a CDP session against this target, if debuggable.
34 #[serde(rename = "webSocketDebuggerUrl")]
35 pub web_socket_debugger_url: Option<String>,
36}
37
38/// Result of `Page.navigate`.
39///
40/// A frame id and loader id come back even when the page never loaded, so
41/// `error_text` is the only field that says whether the navigation worked.
42#[derive(Debug, Clone, Deserialize, Serialize)]
43pub struct NavigationResult {
44 /// The frame that started navigating.
45 #[serde(rename = "frameId")]
46 pub frame_id: String,
47 /// The loader driving this navigation, if one has been assigned yet.
48 #[serde(rename = "loaderId")]
49 pub loader_id: Option<String>,
50 /// Why the navigation failed, e.g. `"net::ERR_NAME_NOT_RESOLVED"`. Absent
51 /// when the navigation started successfully.
52 #[serde(rename = "errorText")]
53 pub error_text: Option<String>,
54 /// Whether the URL turned into a download instead of a page load. Older
55 /// Chrome builds omit the field, which reads as `false`.
56 #[serde(rename = "isDownload", default)]
57 pub is_download: bool,
58}
59
60/// A JS value as CDP's `Runtime` domain represents it.
61#[derive(Debug, Clone, Deserialize)]
62pub struct RemoteObject {
63 /// JS `typeof` of the value, e.g. `"string"`, `"object"`.
64 #[serde(rename = "type")]
65 pub object_type: String,
66 /// The value itself, present when `returnByValue: true` was requested.
67 pub value: Option<Value>,
68 /// Human-readable description Chrome generates for the value.
69 pub description: Option<String>,
70}
71
72/// Result of `Runtime.evaluate`.
73#[derive(Debug, Clone, Deserialize)]
74pub struct EvaluateResult {
75 /// The evaluated expression's value.
76 pub result: RemoteObject,
77 /// Present when the expression threw; contains Chrome's exception details.
78 #[serde(rename = "exceptionDetails")]
79 pub exception_details: Option<Value>,
80}
81
82/// A DOM node as returned by `DOM.getDocument`.
83#[derive(Debug, Clone, Deserialize, Serialize)]
84pub struct DocumentNode {
85 /// CDP node id, used by other `DOM.*` commands.
86 #[serde(rename = "nodeId")]
87 pub node_id: i64,
88 /// Node tag/name, e.g. `"HTML"` for the document element.
89 #[serde(rename = "nodeName")]
90 pub node_name: String,
91 /// Number of direct children, if known.
92 #[serde(rename = "childNodeCount")]
93 pub child_node_count: Option<i64>,
94}
95
96/// A browser cookie as returned by `Network.getCookies`.
97#[derive(Debug, Clone, Deserialize, Serialize)]
98pub struct Cookie {
99 /// Cookie name.
100 pub name: String,
101 /// Cookie value.
102 pub value: String,
103 /// Domain the cookie applies to.
104 pub domain: String,
105 /// Path the cookie applies to.
106 pub path: String,
107}
108
109/// A single metric from `Performance.getMetrics`.
110#[derive(Debug, Clone, Deserialize)]
111pub struct PerformanceMetric {
112 /// Metric name, e.g. `"JSHeapUsedSize"`.
113 pub name: String,
114 /// Metric value.
115 pub value: f64,
116}
117
118/// A `console.*` call captured via [`BrowserAgent::capture_console`](crate::agent::BrowserAgent::capture_console).
119#[derive(Debug, Clone)]
120pub struct ConsoleMessage {
121 /// Console method used, e.g. `"log"`, `"warn"`, `"error"`.
122 pub level: String,
123 /// First stringified argument passed to the console call.
124 pub text: String,
125 /// Source file the call originated from, if available.
126 pub url: Option<String>,
127 /// Source line the call originated from, if available.
128 pub line: Option<u64>,
129}