use crate::common::connection_common::convert_json;
use crate::error::WebDriverResult;
use crate::extensions::chrome::NetworkConditions;
use crate::WebDriverSession;
use serde_json::{json, Value};
use thirtyfour::extensions::chrome::ChromeCommand;
#[derive(Debug, Clone)]
pub struct ChromeDevTools<'a> {
pub session: &'a WebDriverSession,
}
impl<'a> ChromeDevTools<'a> {
pub fn new(session: &'a WebDriverSession) -> Self {
Self {
session,
}
}
fn cmd(&self, command: ChromeCommand) -> WebDriverResult<serde_json::Value> {
self.session.execute(Box::new(command))
}
pub fn launch_app(&self, app_id: &str) -> WebDriverResult<()> {
self.cmd(ChromeCommand::LaunchApp(app_id.to_string()))?;
Ok(())
}
pub fn get_network_conditions(&self) -> WebDriverResult<NetworkConditions> {
let v = self.cmd(ChromeCommand::GetNetworkConditions)?;
convert_json(&v["value"])
}
pub fn set_network_conditions(&self, conditions: &NetworkConditions) -> WebDriverResult<()> {
self.cmd(ChromeCommand::SetNetworkConditions(conditions.clone()))?;
Ok(())
}
pub fn execute_cdp(&self, cmd: &str) -> WebDriverResult<Value> {
self.execute_cdp_with_params(cmd, json!({}))
}
pub fn execute_cdp_with_params(&self, cmd: &str, cmd_args: Value) -> WebDriverResult<Value> {
let v = self.cmd(ChromeCommand::ExecuteCdpCommand(cmd.to_string(), cmd_args))?;
Ok(v["value"].clone())
}
pub fn get_sinks(&self) -> WebDriverResult<Value> {
let v = self.cmd(ChromeCommand::GetSinks)?;
Ok(v["value"].clone())
}
pub fn get_issue_message(&self) -> WebDriverResult<Value> {
let v = self.cmd(ChromeCommand::GetIssueMessage)?;
Ok(v["value"].clone())
}
pub fn set_sink_to_use(&self, sink_name: &str) -> WebDriverResult<()> {
self.cmd(ChromeCommand::SetSinkToUse(sink_name.to_string()))?;
Ok(())
}
pub fn start_tab_mirroring(&self, sink_name: &str) -> WebDriverResult<()> {
self.cmd(ChromeCommand::StartTabMirroring(sink_name.to_string()))?;
Ok(())
}
pub fn stop_casting(&self, sink_name: &str) -> WebDriverResult<()> {
self.cmd(ChromeCommand::StopCasting(sink_name.to_string()))?;
Ok(())
}
}