Vrtgs / thirtyfour

Selenium WebDriver client for Rust, for automated testing of websites
Other
1.06k stars 78 forks source link

Adjustable durations with ActionChain actions #241

Closed 0xlunar closed 3 weeks ago

0xlunar commented 1 month ago

Issue I'm currently writing a trait extenstion to allow more movement options for controlling the mouse, but when testing I noticed that there was a delay of ~250ms between each of my mouse actions, which I thought was strange considering text input is near instant. Upon reading through the source for ActionSource there is a hardcoded duration of 250ms

impl ActionSource<PointerAction> {
    /// Create a new Pointer action source.
    pub fn new(name: &str, action_type: PointerActionType) -> Self {
        ActionSource {
            id: name.to_owned(),
            action_type: String::from("pointer"),
            parameters: Some(PointerParameters {
                pointer_type: String::from(match action_type {
                    PointerActionType::Mouse => "mouse",
                    PointerActionType::Pen => "pen",
                    PointerActionType::Touch => "touch",
                }),
            }),
            actions: Vec::new(),
            duration: 250, // <--- Hardcoded here
        }
    }

Proposal Add action_chain_with_delay(self: &Arc<SessionHandle>, key_delay: Option<u64>, pointer_delay: Option<u64>) -> ActionChain to allow setting a custom delay for key and pointer actions Modify ActionSource<PointerAction> and ActionSource<KeyAction> to include an Option<u64> in their respective new() functions, where None is the currently applied durations and Some(u64) being the delay

// src/common/action.rs
impl ActionSource<PointerAction> {
    pub fn new(name: &str, action_type: PointerActionType, duration: Option<u64>) -> Self {
        let duration = duration.unwrap_or(250);
        ActionSource {
            id: name.to_owned(),
            action_type: String::from("pointer"),
            parameters: Some(PointerParameters {
                pointer_type: String::from(match action_type {
                    PointerActionType::Mouse => "mouse",
                    PointerActionType::Pen => "pen",
                    PointerActionType::Touch => "touch",
                }),
            }),
            actions: Vec::new(),
            duration,
        }
    }
}

impl ActionSource<KeyAction> {
    pub fn new(name: &str, duration: Option<u64>) -> Self {
        let duration = duration.unwrap_or(0);
        ActionSource {
            id: name.to_owned(),
            action_type: String::from("key"),
            parameters: None,
            actions: Vec::new(),
            duration,
        }
    }
}
// src/action_chain.rs
impl ActionChain {
    pub fn new_with_delay(handle: Arc<SessionHandle>, key_delay: Option<u64>, pointer_delay: Option<u64>) -> Self {
        ActionChain {
            handle,
            key_actions: ActionSource::<KeyAction>::new("key", key_delay),
            pointer_actions: ActionSource::<PointerAction>::new(
                "pointer",
                PointerActionType::Mouse,
                pointer_delay
            ),
        }
    }
// src/session/handle.rs
impl SessionHandle {
    pub fn action_chain_with_delay(self: &Arc<SessionHandle>, key_delay: Option<u64>, pointer_delay: Option<u64>) -> ActionChain {
        ActionChain::new_with_delay(self.clone(), key_delay, pointer_delay)
    }
}