use super::click::{KeyboardModifier, Position};
#[derive(Debug, Clone, Default)]
pub struct FillOptions {
pub force: Option<bool>,
pub timeout: Option<f64>,
}
impl FillOptions {
pub fn builder() -> FillOptionsBuilder {
FillOptionsBuilder::default()
}
pub(crate) fn to_json(&self) -> serde_json::Value {
let mut json = serde_json::json!({});
if let Some(force) = self.force {
json["force"] = serde_json::json!(force);
}
if let Some(timeout) = self.timeout {
json["timeout"] = serde_json::json!(timeout);
} else {
json["timeout"] = serde_json::json!(crate::DEFAULT_TIMEOUT_MS);
}
json
}
}
#[derive(Debug, Clone, Default)]
pub struct FillOptionsBuilder {
force: Option<bool>,
timeout: Option<f64>,
}
impl FillOptionsBuilder {
pub fn force(mut self, force: bool) -> Self {
self.force = Some(force);
self
}
pub fn timeout(mut self, timeout: f64) -> Self {
self.timeout = Some(timeout);
self
}
pub fn build(self) -> FillOptions {
FillOptions {
force: self.force,
timeout: self.timeout,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct PressOptions {
pub delay: Option<f64>,
pub timeout: Option<f64>,
}
impl PressOptions {
pub fn builder() -> PressOptionsBuilder {
PressOptionsBuilder::default()
}
pub(crate) fn to_json(&self) -> serde_json::Value {
let mut json = serde_json::json!({});
if let Some(delay) = self.delay {
json["delay"] = serde_json::json!(delay);
}
if let Some(timeout) = self.timeout {
json["timeout"] = serde_json::json!(timeout);
} else {
json["timeout"] = serde_json::json!(crate::DEFAULT_TIMEOUT_MS);
}
json
}
}
#[derive(Debug, Clone, Default)]
pub struct PressOptionsBuilder {
delay: Option<f64>,
timeout: Option<f64>,
}
impl PressOptionsBuilder {
pub fn delay(mut self, delay: f64) -> Self {
self.delay = Some(delay);
self
}
pub fn timeout(mut self, timeout: f64) -> Self {
self.timeout = Some(timeout);
self
}
pub fn build(self) -> PressOptions {
PressOptions {
delay: self.delay,
timeout: self.timeout,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct CheckOptions {
pub force: Option<bool>,
pub position: Option<Position>,
pub timeout: Option<f64>,
pub trial: Option<bool>,
}
impl CheckOptions {
pub fn builder() -> CheckOptionsBuilder {
CheckOptionsBuilder::default()
}
pub(crate) fn to_json(&self) -> serde_json::Value {
let mut json = serde_json::json!({});
if let Some(force) = self.force {
json["force"] = serde_json::json!(force);
}
if let Some(position) = &self.position {
json["position"] = serde_json::to_value(position).unwrap();
}
if let Some(timeout) = self.timeout {
json["timeout"] = serde_json::json!(timeout);
} else {
json["timeout"] = serde_json::json!(crate::DEFAULT_TIMEOUT_MS);
}
if let Some(trial) = self.trial {
json["trial"] = serde_json::json!(trial);
}
json
}
}
#[derive(Debug, Clone, Default)]
pub struct CheckOptionsBuilder {
force: Option<bool>,
position: Option<Position>,
timeout: Option<f64>,
trial: Option<bool>,
}
impl CheckOptionsBuilder {
pub fn force(mut self, force: bool) -> Self {
self.force = Some(force);
self
}
pub fn position(mut self, position: Position) -> Self {
self.position = Some(position);
self
}
pub fn timeout(mut self, timeout: f64) -> Self {
self.timeout = Some(timeout);
self
}
pub fn trial(mut self, trial: bool) -> Self {
self.trial = Some(trial);
self
}
pub fn build(self) -> CheckOptions {
CheckOptions {
force: self.force,
position: self.position,
timeout: self.timeout,
trial: self.trial,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct HoverOptions {
pub force: Option<bool>,
pub modifiers: Option<Vec<KeyboardModifier>>,
pub position: Option<Position>,
pub timeout: Option<f64>,
pub trial: Option<bool>,
}
impl HoverOptions {
pub fn builder() -> HoverOptionsBuilder {
HoverOptionsBuilder::default()
}
pub(crate) fn to_json(&self) -> serde_json::Value {
let mut json = serde_json::json!({});
if let Some(force) = self.force {
json["force"] = serde_json::json!(force);
}
if let Some(modifiers) = &self.modifiers {
json["modifiers"] = serde_json::to_value(modifiers).unwrap();
}
if let Some(position) = &self.position {
json["position"] = serde_json::to_value(position).unwrap();
}
if let Some(timeout) = self.timeout {
json["timeout"] = serde_json::json!(timeout);
} else {
json["timeout"] = serde_json::json!(crate::DEFAULT_TIMEOUT_MS);
}
if let Some(trial) = self.trial {
json["trial"] = serde_json::json!(trial);
}
json
}
}
#[derive(Debug, Clone, Default)]
pub struct HoverOptionsBuilder {
force: Option<bool>,
modifiers: Option<Vec<KeyboardModifier>>,
position: Option<Position>,
timeout: Option<f64>,
trial: Option<bool>,
}
impl HoverOptionsBuilder {
pub fn force(mut self, force: bool) -> Self {
self.force = Some(force);
self
}
pub fn modifiers(mut self, modifiers: Vec<KeyboardModifier>) -> Self {
self.modifiers = Some(modifiers);
self
}
pub fn position(mut self, position: Position) -> Self {
self.position = Some(position);
self
}
pub fn timeout(mut self, timeout: f64) -> Self {
self.timeout = Some(timeout);
self
}
pub fn trial(mut self, trial: bool) -> Self {
self.trial = Some(trial);
self
}
pub fn build(self) -> HoverOptions {
HoverOptions {
force: self.force,
modifiers: self.modifiers,
position: self.position,
timeout: self.timeout,
trial: self.trial,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct PressSequentiallyOptions {
pub delay: Option<f64>,
}
impl PressSequentiallyOptions {
pub fn builder() -> PressSequentiallyOptionsBuilder {
PressSequentiallyOptionsBuilder::default()
}
pub(crate) fn to_json(&self) -> serde_json::Value {
let mut json = serde_json::json!({});
if let Some(delay) = self.delay {
json["delay"] = serde_json::json!(delay);
}
json["timeout"] = serde_json::json!(crate::DEFAULT_TIMEOUT_MS);
json
}
}
#[derive(Debug, Clone, Default)]
pub struct PressSequentiallyOptionsBuilder {
delay: Option<f64>,
}
impl PressSequentiallyOptionsBuilder {
pub fn delay(mut self, delay: f64) -> Self {
self.delay = Some(delay);
self
}
pub fn build(self) -> PressSequentiallyOptions {
PressSequentiallyOptions { delay: self.delay }
}
}
#[derive(Debug, Clone, Default)]
pub struct SelectOptions {
pub force: Option<bool>,
pub timeout: Option<f64>,
}
impl SelectOptions {
pub fn builder() -> SelectOptionsBuilder {
SelectOptionsBuilder::default()
}
pub(crate) fn to_json(&self) -> serde_json::Value {
let mut json = serde_json::json!({});
if let Some(force) = self.force {
json["force"] = serde_json::json!(force);
}
if let Some(timeout) = self.timeout {
json["timeout"] = serde_json::json!(timeout);
} else {
json["timeout"] = serde_json::json!(crate::DEFAULT_TIMEOUT_MS);
}
json
}
}
#[derive(Debug, Clone, Default)]
pub struct SelectOptionsBuilder {
force: Option<bool>,
timeout: Option<f64>,
}
impl SelectOptionsBuilder {
pub fn force(mut self, force: bool) -> Self {
self.force = Some(force);
self
}
pub fn timeout(mut self, timeout: f64) -> Self {
self.timeout = Some(timeout);
self
}
pub fn build(self) -> SelectOptions {
SelectOptions {
force: self.force,
timeout: self.timeout,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct KeyboardOptions {
pub delay: Option<f64>,
}
impl KeyboardOptions {
pub fn builder() -> KeyboardOptionsBuilder {
KeyboardOptionsBuilder::default()
}
pub(crate) fn to_json(&self) -> serde_json::Value {
let mut json = serde_json::json!({});
if let Some(delay) = self.delay {
json["delay"] = serde_json::json!(delay);
}
json
}
}
#[derive(Debug, Clone, Default)]
pub struct KeyboardOptionsBuilder {
delay: Option<f64>,
}
impl KeyboardOptionsBuilder {
pub fn delay(mut self, delay: f64) -> Self {
self.delay = Some(delay);
self
}
pub fn build(self) -> KeyboardOptions {
KeyboardOptions { delay: self.delay }
}
}
#[derive(Debug, Clone, Default)]
pub struct MouseOptions {
pub button: Option<super::click::MouseButton>,
pub click_count: Option<u32>,
pub delay: Option<f64>,
pub steps: Option<u32>,
}
impl MouseOptions {
pub fn builder() -> MouseOptionsBuilder {
MouseOptionsBuilder::default()
}
pub(crate) fn to_json(&self) -> serde_json::Value {
let mut json = serde_json::json!({});
if let Some(button) = &self.button {
json["button"] = serde_json::to_value(button).unwrap();
}
if let Some(click_count) = self.click_count {
json["clickCount"] = serde_json::json!(click_count);
}
if let Some(delay) = self.delay {
json["delay"] = serde_json::json!(delay);
}
if let Some(steps) = self.steps {
json["steps"] = serde_json::json!(steps);
}
json
}
}
#[derive(Debug, Clone, Default)]
pub struct MouseOptionsBuilder {
button: Option<super::click::MouseButton>,
click_count: Option<u32>,
delay: Option<f64>,
steps: Option<u32>,
}
impl MouseOptionsBuilder {
pub fn button(mut self, button: super::click::MouseButton) -> Self {
self.button = Some(button);
self
}
pub fn click_count(mut self, click_count: u32) -> Self {
self.click_count = Some(click_count);
self
}
pub fn delay(mut self, delay: f64) -> Self {
self.delay = Some(delay);
self
}
pub fn steps(mut self, steps: u32) -> Self {
self.steps = Some(steps);
self
}
pub fn build(self) -> MouseOptions {
MouseOptions {
button: self.button,
click_count: self.click_count,
delay: self.delay,
steps: self.steps,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::protocol::click::MouseButton;
#[test]
fn test_fill_options_builder() {
let options = FillOptions::builder().force(true).timeout(5000.0).build();
let json = options.to_json();
assert_eq!(json["force"], true);
assert_eq!(json["timeout"], 5000.0);
}
#[test]
fn test_press_options_builder() {
let options = PressOptions::builder().delay(100.0).timeout(3000.0).build();
let json = options.to_json();
assert_eq!(json["delay"], 100.0);
assert_eq!(json["timeout"], 3000.0);
}
#[test]
fn test_check_options_builder() {
let options = CheckOptions::builder()
.force(true)
.position(Position { x: 5.0, y: 10.0 })
.timeout(2000.0)
.trial(true)
.build();
let json = options.to_json();
assert_eq!(json["force"], true);
assert_eq!(json["position"]["x"], 5.0);
assert_eq!(json["position"]["y"], 10.0);
assert_eq!(json["timeout"], 2000.0);
assert_eq!(json["trial"], true);
}
#[test]
fn test_hover_options_builder() {
let options = HoverOptions::builder()
.force(true)
.modifiers(vec![KeyboardModifier::Shift])
.position(Position { x: 10.0, y: 20.0 })
.timeout(4000.0)
.trial(false)
.build();
let json = options.to_json();
assert_eq!(json["force"], true);
assert_eq!(json["modifiers"], serde_json::json!(["Shift"]));
assert_eq!(json["position"]["x"], 10.0);
assert_eq!(json["position"]["y"], 20.0);
assert_eq!(json["timeout"], 4000.0);
assert_eq!(json["trial"], false);
}
#[test]
fn test_select_options_builder() {
let options = SelectOptions::builder().force(true).timeout(6000.0).build();
let json = options.to_json();
assert_eq!(json["force"], true);
assert_eq!(json["timeout"], 6000.0);
}
#[test]
fn test_keyboard_options_builder() {
let options = KeyboardOptions::builder().delay(50.0).build();
let json = options.to_json();
assert_eq!(json["delay"], 50.0);
}
#[test]
fn test_mouse_options_builder() {
let options = MouseOptions::builder()
.button(MouseButton::Right)
.click_count(2)
.delay(100.0)
.steps(10)
.build();
let json = options.to_json();
assert_eq!(json["button"], "right");
assert_eq!(json["clickCount"], 2);
assert_eq!(json["delay"], 100.0);
assert_eq!(json["steps"], 10);
}
}