Skip to content

Commit

Permalink
replace RequestMethod with http::Method
Browse files Browse the repository at this point in the history
  • Loading branch information
stevepryde committed Apr 23, 2024
1 parent 9135903 commit 0c7edff
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 176 deletions.
2 changes: 1 addition & 1 deletion thirtyfour/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "thirtyfour"
version = "0.33.0-alpha.1"
version = "0.33.0-alpha.2"
authors = ["Steve Pryde <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down
201 changes: 96 additions & 105 deletions thirtyfour/src/common/command.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use http::Method;
use serde_json::{json, Value};

use crate::common::{
Expand All @@ -7,7 +8,7 @@ use crate::common::{
types::{ElementId, OptionRect, SessionId, TimeoutConfiguration, WindowHandle},
};
use crate::IntoArcStr;
use crate::{RequestData, RequestMethod};
use crate::RequestData;
use std::fmt;
use std::fmt::Debug;
use std::sync::Arc;
Expand Down Expand Up @@ -171,7 +172,7 @@ pub trait ExtensionCommand: Debug {
fn parameters_json(&self) -> Option<Value>;

/// HTTP method accepting by the webdriver
fn method(&self) -> RequestMethod;
fn method(&self) -> Method;

/// Endpoint URL without `/session/{sessionId}` prefix
///
Expand Down Expand Up @@ -256,250 +257,240 @@ impl FormatRequestData for Command {
match self {
Command::NewSession(caps) => {
let w3c_caps = make_w3c_caps(caps);
RequestData::new(RequestMethod::Post, "/session").add_body(json!({
RequestData::new(Method::POST, "/session").add_body(json!({
"capabilities": w3c_caps,
"desiredCapabilities": caps
}))
}
Command::DeleteSession => {
RequestData::new(RequestMethod::Delete, format!("/session/{}", session_id))
RequestData::new(Method::DELETE, format!("/session/{}", session_id))
}
Command::Status => RequestData::new(RequestMethod::Get, "/status"),
Command::Status => RequestData::new(Method::GET, "/status"),
Command::GetTimeouts => {
RequestData::new(RequestMethod::Get, format!("/session/{}/timeouts", session_id))
RequestData::new(Method::GET, format!("/session/{}/timeouts", session_id))
}
Command::SetTimeouts(timeout_configuration) => {
RequestData::new(RequestMethod::Post, format!("/session/{}/timeouts", session_id))
RequestData::new(Method::POST, format!("/session/{}/timeouts", session_id))
.add_body(json!(timeout_configuration))
}
Command::NavigateTo(url) => {
RequestData::new(RequestMethod::Post, format!("/session/{}/url", session_id))
RequestData::new(Method::POST, format!("/session/{}/url", session_id))
.add_body(json!({ "url": url }))
}
Command::GetCurrentUrl => {
RequestData::new(RequestMethod::Get, format!("/session/{}/url", session_id))
RequestData::new(Method::GET, format!("/session/{}/url", session_id))
}
Command::Back => {
RequestData::new(RequestMethod::Post, format!("/session/{}/back", session_id))
RequestData::new(Method::POST, format!("/session/{}/back", session_id))
.add_body(json!({}))
}
Command::Forward => {
RequestData::new(RequestMethod::Post, format!("/session/{}/forward", session_id))
RequestData::new(Method::POST, format!("/session/{}/forward", session_id))
.add_body(json!({}))
}
Command::Refresh => {
RequestData::new(RequestMethod::Post, format!("/session/{}/refresh", session_id))
RequestData::new(Method::POST, format!("/session/{}/refresh", session_id))
.add_body(json!({}))
}
Command::GetTitle => {
RequestData::new(RequestMethod::Get, format!("/session/{}/title", session_id))
RequestData::new(Method::GET, format!("/session/{}/title", session_id))
}
Command::GetWindowHandle => {
RequestData::new(RequestMethod::Get, format!("/session/{}/window", session_id))
RequestData::new(Method::GET, format!("/session/{}/window", session_id))
}
Command::CloseWindow => {
RequestData::new(RequestMethod::Delete, format!("/session/{}/window", session_id))
RequestData::new(Method::DELETE, format!("/session/{}/window", session_id))
}
Command::SwitchToWindow(window_handle) => {
RequestData::new(RequestMethod::Post, format!("/session/{}/window", session_id))
RequestData::new(Method::POST, format!("/session/{}/window", session_id))
.add_body(json!({ "handle": window_handle.to_string() }))
}
Command::GetWindowHandles => RequestData::new(
RequestMethod::Get,
format!("/session/{}/window/handles", session_id),
),
Command::GetWindowHandles => {
RequestData::new(Method::GET, format!("/session/{}/window/handles", session_id))
}
Command::NewWindow => {
RequestData::new(RequestMethod::Post, format!("/session/{}/window/new", session_id))
RequestData::new(Method::POST, format!("/session/{}/window/new", session_id))
.add_body(json!({"type": "window"}))
}
Command::NewTab => {
RequestData::new(RequestMethod::Post, format!("/session/{}/window/new", session_id))
RequestData::new(Method::POST, format!("/session/{}/window/new", session_id))
.add_body(json!({"type": "tab"}))
}
Command::SwitchToFrameDefault => {
RequestData::new(RequestMethod::Post, format!("/session/{}/frame", session_id))
RequestData::new(Method::POST, format!("/session/{}/frame", session_id))
.add_body(json!({ "id": serde_json::Value::Null }))
}
Command::SwitchToFrameNumber(frame_number) => {
RequestData::new(RequestMethod::Post, format!("/session/{}/frame", session_id))
RequestData::new(Method::POST, format!("/session/{}/frame", session_id))
.add_body(json!({ "id": frame_number }))
}
Command::SwitchToFrameElement(element_id) => {
RequestData::new(RequestMethod::Post, format!("/session/{}/frame", session_id))
.add_body(json!({"id": {
RequestData::new(Method::POST, format!("/session/{}/frame", session_id)).add_body(
json!({"id": {
"ELEMENT": element_id.to_string(),
MAGIC_ELEMENTID: element_id.to_string()
}}))
}}),
)
}
Command::SwitchToParentFrame => {
RequestData::new(Method::POST, format!("/session/{}/frame/parent", session_id))
.add_body(json!({}))
}
Command::SwitchToParentFrame => RequestData::new(
RequestMethod::Post,
format!("/session/{}/frame/parent", session_id),
)
.add_body(json!({})),
Command::GetWindowRect => {
RequestData::new(RequestMethod::Get, format!("/session/{}/window/rect", session_id))
RequestData::new(Method::GET, format!("/session/{}/window/rect", session_id))
}
Command::SetWindowRect(option_rect) => {
RequestData::new(Method::POST, format!("/session/{}/window/rect", session_id))
.add_body(json!(option_rect))
}
Command::MaximizeWindow => {
RequestData::new(Method::POST, format!("/session/{}/window/maximize", session_id))
.add_body(json!({}))
}
Command::MinimizeWindow => {
RequestData::new(Method::POST, format!("/session/{}/window/minimize", session_id))
.add_body(json!({}))
}
Command::FullscreenWindow => {
RequestData::new(Method::POST, format!("/session/{}/window/fullscreen", session_id))
.add_body(json!({}))
}
Command::GetActiveElement => {
RequestData::new(Method::GET, format!("/session/{}/element/active", session_id))
}
Command::SetWindowRect(option_rect) => RequestData::new(
RequestMethod::Post,
format!("/session/{}/window/rect", session_id),
)
.add_body(json!(option_rect)),
Command::MaximizeWindow => RequestData::new(
RequestMethod::Post,
format!("/session/{}/window/maximize", session_id),
)
.add_body(json!({})),
Command::MinimizeWindow => RequestData::new(
RequestMethod::Post,
format!("/session/{}/window/minimize", session_id),
)
.add_body(json!({})),
Command::FullscreenWindow => RequestData::new(
RequestMethod::Post,
format!("/session/{}/window/fullscreen", session_id),
)
.add_body(json!({})),
Command::GetActiveElement => RequestData::new(
RequestMethod::Get,
format!("/session/{}/element/active", session_id),
),
Command::FindElement(selector) => {
RequestData::new(RequestMethod::Post, format!("/session/{}/element", session_id))
RequestData::new(Method::POST, format!("/session/{}/element", session_id))
.add_body(json!({"using": selector.name, "value": selector.query}))
}
Command::FindElements(selector) => {
RequestData::new(RequestMethod::Post, format!("/session/{}/elements", session_id))
RequestData::new(Method::POST, format!("/session/{}/elements", session_id))
.add_body(json!({"using": selector.name, "value": selector.query}))
}
Command::FindElementFromElement(element_id, selector) => RequestData::new(
RequestMethod::Post,
Method::POST,
format!("/session/{}/element/{}/element", session_id, element_id),
)
.add_body(json!({"using": selector.name, "value": selector.query})),
Command::FindElementsFromElement(element_id, selector) => RequestData::new(
RequestMethod::Post,
Method::POST,
format!("/session/{}/element/{}/elements", session_id, element_id),
)
.add_body(json!({"using": selector.name, "value": selector.query})),
Command::IsElementSelected(element_id) => RequestData::new(
RequestMethod::Get,
Method::GET,
format!("/session/{}/element/{}/selected", session_id, element_id),
),
Command::IsElementDisplayed(element_id) => RequestData::new(
RequestMethod::Get,
Method::GET,
format!("/session/{}/element/{}/displayed", session_id, element_id),
),
Command::GetElementAttribute(element_id, attribute_name) => RequestData::new(
RequestMethod::Get,
Method::GET,
format!(
"/session/{}/element/{}/attribute/{}",
session_id, element_id, attribute_name
),
),
Command::GetElementProperty(element_id, property_name) => RequestData::new(
RequestMethod::Get,
Method::GET,
format!(
"/session/{}/element/{}/property/{}",
session_id, element_id, property_name
),
),
Command::GetElementCssValue(element_id, property_name) => RequestData::new(
RequestMethod::Get,
Method::GET,
format!("/session/{}/element/{}/css/{}", session_id, element_id, property_name),
),
Command::GetElementText(element_id) => RequestData::new(
RequestMethod::Get,
Method::GET,
format!("/session/{}/element/{}/text", session_id, element_id),
),
Command::GetElementTagName(element_id) => RequestData::new(
RequestMethod::Get,
Method::GET,
format!("/session/{}/element/{}/name", session_id, element_id),
),
Command::GetElementRect(element_id) => RequestData::new(
RequestMethod::Get,
Method::GET,
format!("/session/{}/element/{}/rect", session_id, element_id),
),
Command::IsElementEnabled(element_id) => RequestData::new(
RequestMethod::Get,
Method::GET,
format!("/session/{}/element/{}/enabled", session_id, element_id),
),
Command::ElementClick(element_id) => RequestData::new(
RequestMethod::Post,
Method::POST,
format!("/session/{}/element/{}/click", session_id, element_id),
)
.add_body(json!({})),
Command::ElementClear(element_id) => RequestData::new(
RequestMethod::Post,
Method::POST,
format!("/session/{}/element/{}/clear", session_id, element_id),
)
.add_body(json!({})),
Command::ElementSendKeys(element_id, typing_data) => RequestData::new(
RequestMethod::Post,
Method::POST,
format!("/session/{}/element/{}/value", session_id, element_id),
)
.add_body(json!({"text": typing_data.to_string(), "value": typing_data.as_vec() })),
Command::GetPageSource => {
RequestData::new(RequestMethod::Get, format!("/session/{}/source", session_id))
RequestData::new(Method::GET, format!("/session/{}/source", session_id))
}
Command::ExecuteScript(script, args) => {
RequestData::new(Method::POST, format!("/session/{}/execute/sync", session_id))
.add_body(json!({"script": script, "args": args}))
}
Command::ExecuteAsyncScript(script, args) => {
RequestData::new(Method::POST, format!("/session/{}/execute/async", session_id))
.add_body(json!({"script": script, "args": args}))
}
Command::ExecuteScript(script, args) => RequestData::new(
RequestMethod::Post,
format!("/session/{}/execute/sync", session_id),
)
.add_body(json!({"script": script, "args": args})),
Command::ExecuteAsyncScript(script, args) => RequestData::new(
RequestMethod::Post,
format!("/session/{}/execute/async", session_id),
)
.add_body(json!({"script": script, "args": args})),
Command::GetAllCookies => {
RequestData::new(RequestMethod::Get, format!("/session/{}/cookie", session_id))
RequestData::new(Method::GET, format!("/session/{}/cookie", session_id))
}
Command::GetNamedCookie(cookie_name) => RequestData::new(
RequestMethod::Get,
Method::GET,
format!("/session/{}/cookie/{}", session_id, cookie_name),
),
Command::AddCookie(cookie) => {
RequestData::new(RequestMethod::Post, format!("/session/{}/cookie", session_id))
RequestData::new(Method::POST, format!("/session/{}/cookie", session_id))
.add_body(json!({ "cookie": cookie }))
}
Command::DeleteCookie(cookie_name) => RequestData::new(
RequestMethod::Delete,
Method::DELETE,
format!("/session/{}/cookie/{}", session_id, cookie_name),
),
Command::DeleteAllCookies => {
RequestData::new(RequestMethod::Delete, format!("/session/{}/cookie", session_id))
RequestData::new(Method::DELETE, format!("/session/{}/cookie", session_id))
}
Command::PerformActions(actions) => {
RequestData::new(RequestMethod::Post, format!("/session/{}/actions", session_id))
RequestData::new(Method::POST, format!("/session/{}/actions", session_id))
.add_body(json!({"actions": actions.0}))
}
Command::ReleaseActions => {
RequestData::new(RequestMethod::Delete, format!("/session/{}/actions", session_id))
RequestData::new(Method::DELETE, format!("/session/{}/actions", session_id))
}
Command::DismissAlert => {
RequestData::new(Method::POST, format!("/session/{}/alert/dismiss", session_id))
.add_body(json!({}))
}
Command::AcceptAlert => {
RequestData::new(Method::POST, format!("/session/{}/alert/accept", session_id))
.add_body(json!({}))
}
Command::DismissAlert => RequestData::new(
RequestMethod::Post,
format!("/session/{}/alert/dismiss", session_id),
)
.add_body(json!({})),
Command::AcceptAlert => RequestData::new(
RequestMethod::Post,
format!("/session/{}/alert/accept", session_id),
)
.add_body(json!({})),
Command::GetAlertText => {
RequestData::new(RequestMethod::Get, format!("/session/{}/alert/text", session_id))
RequestData::new(Method::GET, format!("/session/{}/alert/text", session_id))
}
Command::SendAlertText(typing_data) => {
RequestData::new(RequestMethod::Post, format!("/session/{}/alert/text", session_id))
RequestData::new(Method::POST, format!("/session/{}/alert/text", session_id))
.add_body(json!({
"value": typing_data.as_vec(), "text": typing_data.to_string()
}))
}
Command::TakeScreenshot => {
RequestData::new(RequestMethod::Get, format!("/session/{}/screenshot", session_id))
RequestData::new(Method::GET, format!("/session/{}/screenshot", session_id))
}
Command::TakeElementScreenshot(element_id) => RequestData::new(
RequestMethod::Get,
Method::GET,
format!("/session/{}/element/{}/screenshot", session_id, element_id),
),
Command::ExtensionCommand(command) => {
Expand Down
Loading

0 comments on commit 0c7edff

Please sign in to comment.