Bug 1895738 - [geckodriver] Update validation of "unhandledPromptBehavior" capability for WebDriver BiDi. r=webdriver-reviewers,jgraham,jdescottes

Depends on D210730

Differential Revision: https://phabricator.services.mozilla.com/D210731
This commit is contained in:
Henrik Skupin 2024-05-23 18:30:05 +00:00
parent ca131ac9dc
commit cb35fc63ba

View file

@ -180,7 +180,7 @@ impl SpecNewSessionParameters {
"proxy" => SpecNewSessionParameters::validate_proxy(value)?,
"timeouts" => SpecNewSessionParameters::validate_timeouts(value)?,
"unhandledPromptBehavior" => {
SpecNewSessionParameters::validate_unhandled_prompt_behaviour(value)?
SpecNewSessionParameters::validate_unhandled_prompt_behavior(value)?
}
x => {
if !x.contains(':') {
@ -415,14 +415,50 @@ impl SpecNewSessionParameters {
Ok(())
}
fn validate_unhandled_prompt_behaviour(value: &Value) -> WebDriverResult<()> {
let behaviour = try_opt!(
fn validate_unhandled_prompt_behavior(value: &Value) -> WebDriverResult<()> {
match value {
Value::Object(obj) => {
// Unhandled Prompt Behavior type as used by WebDriver BiDi
for (key, value) in obj {
match &**key {
x @ "alert"
| x @ "beforeUnload"
| x @ "confirm"
| x @ "default"
| x @ "prompt" => {
let behavior = try_opt!(
value.as_str(),
ErrorStatus::InvalidArgument,
format!("unhandledPromptBehavior is not a string: {}", value)
format!(
"'{}' unhandledPromptBehavior value is not a string: {}",
x, value
)
);
match behaviour {
match behavior {
"accept" | "accept and notify" | "dismiss"
| "dismiss and notify" | "ignore" => {}
x => {
return Err(WebDriverError::new(
ErrorStatus::InvalidArgument,
format!(
"'{}' unhandledPromptBehavior value is invalid: {}",
x, behavior
),
))
}
}
}
x => {
return Err(WebDriverError::new(
ErrorStatus::InvalidArgument,
format!("Invalid unhandledPromptBehavior entry: {}", x),
))
}
}
}
}
Value::String(behavior) => match behavior.as_str() {
"accept" | "accept and notify" | "dismiss" | "dismiss and notify" | "ignore" => {}
x => {
return Err(WebDriverError::new(
@ -430,6 +466,16 @@ impl SpecNewSessionParameters {
format!("Invalid unhandledPromptBehavior value: {}", x),
))
}
},
_ => {
return Err(WebDriverError::new(
ErrorStatus::InvalidArgument,
format!(
"unhandledPromptBehavior is neither an object nor a string: {}",
value
),
))
}
}
Ok(())
@ -793,6 +839,41 @@ mod tests {
assert_de(&caps, json);
}
#[test]
fn test_validate_unhandled_prompt_behavior() {
fn validate_prompt_behavior(v: Value) -> WebDriverResult<()> {
SpecNewSessionParameters::validate_unhandled_prompt_behavior(&v)
}
// capability as string
validate_prompt_behavior(json!("accept")).unwrap();
validate_prompt_behavior(json!("accept and notify")).unwrap();
validate_prompt_behavior(json!("dismiss")).unwrap();
validate_prompt_behavior(json!("dismiss and notify")).unwrap();
validate_prompt_behavior(json!("ignore")).unwrap();
assert!(validate_prompt_behavior(json!("foo")).is_err());
// capability as object
let types = ["alert", "beforeUnload", "confirm", "default", "prompt"];
let handlers = [
"accept",
"accept and notify",
"dismiss",
"dismiss and notify",
"ignore",
];
for promptType in types {
assert!(validate_prompt_behavior(json!({promptType: "foo"})).is_err());
for handler in handlers {
validate_prompt_behavior(json!({promptType: handler})).unwrap();
}
}
for handler in handlers {
assert!(validate_prompt_behavior(json!({"foo": handler})).is_err());
}
}
#[test]
fn test_validate_proxy() {
fn validate_proxy(v: Value) -> WebDriverResult<()> {