use serde_json::{json, Value};
use crate::error::WebDriverResult;
use crate::extensions::chrome::networkconditions::NetworkConditions;
use crate::extensions::chrome::ChromeCommand;
use crate::session::handle::SessionHandle;
#[derive(Clone)]
pub struct ChromeDevTools {
pub handle: SessionHandle,
}
impl ChromeDevTools {
pub fn new(handle: SessionHandle) -> Self {
Self {
handle,
}
}
pub async fn launch_app(&self, app_id: &str) -> WebDriverResult<()> {
self.handle.client.issue_cmd(ChromeCommand::LaunchApp(app_id.to_string())).await?;
Ok(())
}
pub async fn get_network_conditions(&self) -> WebDriverResult<NetworkConditions> {
let v = self.handle.client.issue_cmd(ChromeCommand::GetNetworkConditions).await?;
let conditions: NetworkConditions = serde_json::from_value(v)?;
Ok(conditions)
}
pub async fn set_network_conditions(
&self,
conditions: &NetworkConditions,
) -> WebDriverResult<()> {
self.handle
.client
.issue_cmd(ChromeCommand::SetNetworkConditions(conditions.clone()))
.await?;
Ok(())
}
pub async fn execute_cdp(&self, cmd: &str) -> WebDriverResult<Value> {
self.execute_cdp_with_params(cmd, json!({})).await
}
pub async fn execute_cdp_with_params(
&self,
cmd: &str,
cmd_args: Value,
) -> WebDriverResult<Value> {
let v = self
.handle
.client
.issue_cmd(ChromeCommand::ExecuteCdpCommand(cmd.to_string(), cmd_args))
.await?;
Ok(v)
}
pub async fn get_sinks(&self) -> WebDriverResult<Value> {
let v = self.handle.client.issue_cmd(ChromeCommand::GetSinks).await?;
Ok(v)
}
pub async fn get_issue_message(&self) -> WebDriverResult<Value> {
let v = self.handle.client.issue_cmd(ChromeCommand::GetIssueMessage).await?;
Ok(v)
}
pub async fn set_sink_to_use(&self, sink_name: &str) -> WebDriverResult<()> {
self.handle.client.issue_cmd(ChromeCommand::SetSinkToUse(sink_name.to_string())).await?;
Ok(())
}
pub async fn start_tab_mirroring(&self, sink_name: &str) -> WebDriverResult<()> {
self.handle
.client
.issue_cmd(ChromeCommand::StartTabMirroring(sink_name.to_string()))
.await?;
Ok(())
}
pub async fn stop_casting(&self, sink_name: &str) -> WebDriverResult<()> {
self.handle.client.issue_cmd(ChromeCommand::StopCasting(sink_name.to_string())).await?;
Ok(())
}
}