use serde::Serialize;
use crate::cdp::Cdp;
use crate::cdp::command::{CdpCommand, Empty};
use crate::common::protocol::string_enum;
use crate::error::WebDriverResult;
string_enum! {
pub enum MediaType {
Screen = "screen",
Print = "print",
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetDeviceMetricsOverride {
pub width: u32,
pub height: u32,
pub device_scale_factor: f64,
pub mobile: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub scale: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub screen_orientation: Option<serde_json::Value>,
}
impl CdpCommand for SetDeviceMetricsOverride {
const METHOD: &'static str = "Emulation.setDeviceMetricsOverride";
type Returns = Empty;
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ClearDeviceMetricsOverride;
impl CdpCommand for ClearDeviceMetricsOverride {
const METHOD: &'static str = "Emulation.clearDeviceMetricsOverride";
type Returns = Empty;
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetUserAgentOverride {
pub user_agent: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub accept_language: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub platform: Option<String>,
}
impl CdpCommand for SetUserAgentOverride {
const METHOD: &'static str = "Emulation.setUserAgentOverride";
type Returns = Empty;
}
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetGeolocationOverride {
#[serde(skip_serializing_if = "Option::is_none")]
pub latitude: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub longitude: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub accuracy: Option<f64>,
}
impl CdpCommand for SetGeolocationOverride {
const METHOD: &'static str = "Emulation.setGeolocationOverride";
type Returns = Empty;
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct SetLocaleOverride {
#[serde(skip_serializing_if = "Option::is_none")]
pub locale: Option<String>,
}
impl CdpCommand for SetLocaleOverride {
const METHOD: &'static str = "Emulation.setLocaleOverride";
type Returns = Empty;
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetTimezoneOverride {
pub timezone_id: String,
}
impl CdpCommand for SetTimezoneOverride {
const METHOD: &'static str = "Emulation.setTimezoneOverride";
type Returns = Empty;
}
#[derive(Debug, Clone, Serialize)]
pub struct MediaFeature {
pub name: String,
pub value: String,
}
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetEmulatedMedia {
#[serde(skip_serializing_if = "Option::is_none")]
pub media: Option<MediaType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub features: Option<Vec<MediaFeature>>,
}
impl CdpCommand for SetEmulatedMedia {
const METHOD: &'static str = "Emulation.setEmulatedMedia";
type Returns = Empty;
}
#[derive(Debug)]
pub struct EmulationDomain<'a> {
cdp: &'a Cdp,
}
impl<'a> EmulationDomain<'a> {
pub(crate) fn new(cdp: &'a Cdp) -> Self {
Self {
cdp,
}
}
pub async fn set_device_metrics_override(
&self,
width: u32,
height: u32,
device_scale_factor: f64,
mobile: bool,
) -> WebDriverResult<()> {
self.cdp
.send(SetDeviceMetricsOverride {
width,
height,
device_scale_factor,
mobile,
scale: None,
screen_orientation: None,
})
.await?;
Ok(())
}
pub async fn clear_device_metrics_override(&self) -> WebDriverResult<()> {
self.cdp.send(ClearDeviceMetricsOverride).await?;
Ok(())
}
pub async fn set_user_agent_override(
&self,
user_agent: impl Into<String>,
) -> WebDriverResult<()> {
self.cdp
.send(SetUserAgentOverride {
user_agent: user_agent.into(),
accept_language: None,
platform: None,
})
.await?;
Ok(())
}
pub async fn set_geolocation_override(
&self,
latitude: f64,
longitude: f64,
accuracy: f64,
) -> WebDriverResult<()> {
self.cdp
.send(SetGeolocationOverride {
latitude: Some(latitude),
longitude: Some(longitude),
accuracy: Some(accuracy),
})
.await?;
Ok(())
}
pub async fn clear_geolocation_override(&self) -> WebDriverResult<()> {
self.cdp.send(SetGeolocationOverride::default()).await?;
Ok(())
}
pub async fn set_locale_override(&self, locale: impl Into<String>) -> WebDriverResult<()> {
self.cdp
.send(SetLocaleOverride {
locale: Some(locale.into()),
})
.await?;
Ok(())
}
pub async fn set_timezone_override(
&self,
timezone_id: impl Into<String>,
) -> WebDriverResult<()> {
self.cdp
.send(SetTimezoneOverride {
timezone_id: timezone_id.into(),
})
.await?;
Ok(())
}
}