1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use embedder_traits::WebDriverScriptCommand;
use serde_json::Value;
use servo_base::generic_channel;
use servo_base::id::BrowsingContextId;
use webdriver::command::JavascriptCommandParameters;
use webdriver::common::{ELEMENT_KEY, FRAME_KEY, SHADOW_KEY, WINDOW_KEY};
use webdriver::error::{ErrorStatus, WebDriverError, WebDriverResult};
use crate::{Handler, VerifyBrowsingContextIsOpen, wait_for_ipc_response_flatten};
impl Handler {
/// <https://w3c.github.io/webdriver/#dfn-extract-the-script-arguments-from-a-request>
pub(crate) fn extract_script_arguments(
&self,
parameters: JavascriptCommandParameters,
) -> WebDriverResult<(String, Vec<String>)> {
// Step 1. Let script be the result of getting a property named "script" from parameters
// Step 2. (Done) If script is not a String, return error with error code invalid argument.
let script = parameters.script;
// Step 3. Let args be the result of getting a property named "args" from parameters.
// Step 4. (Done) If args is not an Array return error with error code invalid argument.
// Step 5. Let `arguments` be JSON deserialize with session and args.
let args: Vec<String> = parameters
.args
.as_deref()
.unwrap_or(&[])
.iter()
.map(|value| self.json_deserialize(value))
.collect::<WebDriverResult<Vec<_>>>()?;
Ok((script, args))
}
/// <https://w3c.github.io/webdriver/#dfn-deserialize-a-web-element>
fn deserialize_web_element(&self, element: &Value) -> WebDriverResult<String> {
// Step 2. Let reference be the result of getting the web element identifier property from object.
let element_ref = match element {
Value::String(string) => string.clone(),
_ => return Err(WebDriverError::new(ErrorStatus::InvalidArgument, "")),
};
// Step 3. Let element be the result of trying to get a known element with session and reference.
let (sender, receiver) = generic_channel::channel().unwrap();
self.browsing_context_script_command(
WebDriverScriptCommand::GetKnownElement(element_ref.clone(), sender),
VerifyBrowsingContextIsOpen::No,
)?;
wait_for_ipc_response_flatten(receiver)?;
// Step 4. Return success with data element.
Ok(format!("window.webdriverElement(\"{}\")", element_ref))
}
/// <https://w3c.github.io/webdriver/#dfn-deserialize-a-shadow-root>
fn deserialize_shadow_root(&self, shadow_root: &Value) -> WebDriverResult<String> {
// Step 2. Let reference be the result of getting the shadow root identifier property from object.
let shadow_root_ref = match shadow_root {
Value::String(string) => string.clone(),
_ => return Err(WebDriverError::new(ErrorStatus::InvalidArgument, "")),
};
// Step 3. Let element be the result of trying to get a known element with session and reference.
let (sender, receiver) = generic_channel::channel().unwrap();
self.browsing_context_script_command(
WebDriverScriptCommand::GetKnownShadowRoot(shadow_root_ref.clone(), sender),
VerifyBrowsingContextIsOpen::No,
)?;
wait_for_ipc_response_flatten(receiver)?;
// Step 4. Return success with data element.
Ok(format!(
"window.webdriverShadowRoot(\"{}\")",
shadow_root_ref
))
}
/// <https://w3c.github.io/webdriver/#dfn-deserialize-a-web-frame>
fn deserialize_web_frame(&self, frame: &Value) -> WebDriverResult<String> {
// Step 2. Let reference be the result of getting the web frame identifier property from object.
let frame_ref = match frame {
Value::String(string) => string.clone(),
_ => return Err(WebDriverError::new(ErrorStatus::InvalidArgument, "")),
};
// Step 3. Let browsing context be the browsing context whose window handle is reference,
// or null if no such browsing context exists.
let Some(browsing_context_id) = BrowsingContextId::from_string(&frame_ref) else {
// Step 4. If browsing context is null or a top-level browsing context,
// return error with error code no such frame.
return Err(WebDriverError::new(ErrorStatus::NoSuchFrame, ""));
};
match self.verify_browsing_context_is_open(browsing_context_id) {
// Step 5. Return success with data browsing context's associated window.
Ok(_) => Ok(format!("window.webdriverFrame(\"{frame_ref}\")")),
// Part of Step 4.
Err(_) => Err(WebDriverError::new(ErrorStatus::NoSuchFrame, "")),
}
}
/// <https://w3c.github.io/webdriver/#dfn-deserialize-a-web-window>
fn deserialize_web_window(&self, window: &Value) -> WebDriverResult<String> {
// Step 2. Let reference be the result of getting the web window identifier property from object.
let window_ref = match window {
Value::String(string) => string.clone(),
_ => return Err(WebDriverError::new(ErrorStatus::InvalidArgument, "")),
};
// Step 3. Let browsing context be the browsing context whose window handle is reference,
// or null if no such browsing context exists.
let (sender, receiver) = generic_channel::channel().unwrap();
self.browsing_context_script_command(
WebDriverScriptCommand::GetKnownWindow(window_ref.clone(), sender),
VerifyBrowsingContextIsOpen::No,
)?;
// Step 4. If browsing context is null or not a top-level browsing context,
// return error with error code no such window.
wait_for_ipc_response_flatten(receiver)?;
// Step 5. Return success with data browsing context's associated window.
Ok(format!("window.webdriverWindow(\"{window_ref}\")"))
}
/// <https://w3c.github.io/webdriver/#dfn-json-deserialize>
fn json_deserialize(&self, v: &Value) -> WebDriverResult<String> {
let res = match v {
Value::Array(list) => {
let elems = list
.iter()
.map(|v| self.json_deserialize(v))
.collect::<WebDriverResult<Vec<_>>>()?;
format!("[{}]", elems.join(", "))
},
Value::Object(map) => {
if let Some(id) = map.get(ELEMENT_KEY) {
return self.deserialize_web_element(id);
}
if let Some(id) = map.get(SHADOW_KEY) {
return self.deserialize_shadow_root(id);
}
if let Some(id) = map.get(FRAME_KEY) {
return self.deserialize_web_frame(id);
}
if let Some(id) = map.get(WINDOW_KEY) {
return self.deserialize_web_window(id);
}
let elems = map
.iter()
.map(|(k, v)| {
let key = serde_json::to_string(k)?;
let arg = self.json_deserialize(v)?;
Ok(format!("{key}: {arg}"))
})
.collect::<WebDriverResult<Vec<String>>>()?;
format!("{{{}}}", elems.join(", "))
},
_ => serde_json::to_string(v)?,
};
Ok(res)
}
}