Skip to content

Commit

Permalink
Implement wheel support.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaboatman committed Apr 27, 2023
1 parent 446a1c3 commit b31b6ce
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ native-tls = ["hyper-tls"]
rustls-tls = ["hyper-rustls"]

[dependencies]
webdriver = { version = "0.46", default-features = false }
webdriver = { version = "0.48", default-features = false }
url = "2.2.2"
serde = { version = "1.0.103", features = ["derive"] }
serde_json = "1.0.25"
Expand Down
119 changes: 113 additions & 6 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,17 @@ impl PointerAction {
WDActions::PointerAction::Move(WDActions::PointerMoveAction {
duration: duration.map(|x| x.as_millis() as u64),
origin: WDActions::PointerOrigin::Pointer,
x: Some(x),
y: Some(y),
x,
y,
..Default::default()
}),
),
PointerAction::MoveTo { duration, x, y } => WDActions::PointerActionItem::Pointer(
WDActions::PointerAction::Move(WDActions::PointerMoveAction {
duration: duration.map(|x| x.as_millis() as u64),
origin: WDActions::PointerOrigin::Viewport,
x: Some(x),
y: Some(y),
x,
y,
..Default::default()
}),
),
Expand All @@ -213,8 +213,8 @@ impl PointerAction {
WDActions::PointerMoveAction {
duration: duration.map(|x| x.as_millis() as u64),
origin: WDActions::PointerOrigin::Element(element.element),
x: Some(x),
y: Some(y),
x,
y,
..Default::default()
},
)),
Expand Down Expand Up @@ -404,6 +404,100 @@ impl From<TouchActions> for ActionSequence {
}
}

/// A sequence containing [`Wheel` actions](WheelAction) for a wheel device.
#[derive(Debug, Clone)]

Check warning on line 408 in src/actions.rs

View check run for this annotation

Codecov / codecov/patch

src/actions.rs#L408

Added line #L408 was not covered by tests
pub struct WheelActions {
/// A unique identifier to distinguish this input source from others.
///
/// Choose a meaningful string as it may be useful for debugging.
id: String,
/// The list of actions for this sequence.
actions: Vec<WheelAction>,
}

impl WheelActions {
/// Create a new `WheelActions` sequence.
///
/// The id can be any string but must uniquely identify this input source.
pub fn new(id: String) -> Self {
Self {
id,
actions: Vec::new(),
}
}

Check warning on line 427 in src/actions.rs

View check run for this annotation

Codecov / codecov/patch

src/actions.rs#L422-L427

Added lines #L422 - L427 were not covered by tests

/// Pushes a new action.
pub fn push(&mut self, action: WheelAction) {
self.actions.push(action);
}

Check warning on line 432 in src/actions.rs

View check run for this annotation

Codecov / codecov/patch

src/actions.rs#L430-L432

Added lines #L430 - L432 were not covered by tests
}

impl From<WheelActions> for ActionSequence {
fn from(wa: WheelActions) -> Self {
ActionSequence(WDActions::ActionSequence {
id: wa.id,
actions: WDActions::ActionsType::Wheel {
actions: wa.actions.into_iter().map(|x| x.into_item()).collect(),
},
})
}

Check warning on line 443 in src/actions.rs

View check run for this annotation

Codecov / codecov/patch

src/actions.rs#L436-L443

Added lines #L436 - L443 were not covered by tests
}

/// An action performed with a wheel device.
///
/// See [15.4.4 Wheel Actions](https://www.w3.org/TR/webdriver/#wheel-actions) of the
/// WebDriver standard.
#[derive(Debug, Clone)]

Check warning on line 450 in src/actions.rs

View check run for this annotation

Codecov / codecov/patch

src/actions.rs#L450

Added line #L450 was not covered by tests
pub enum WheelAction {
/// Pause action.
/// Useful for adding pauses between other key actions.
Pause {
/// The pause duration, given in milliseconds.
duration: Duration,
},
/// Wheel scroll event.
Scroll {
/// The scroll duration.
duration: Option<Duration>,
/// `x` offset of the scroll origin, in pixels.
x: i64,
/// `y` offset of the scroll origin, in pixels.
y: i64,
/// The change of the number of pixels to be scrolled on the `x`-axis.
delta_x: i64,
/// The change of the number of pixels to be scrolled on the `y`-axis.
delta_y: i64,
},
}

impl WheelAction {
fn into_item(self) -> WDActions::WheelActionItem {
match self {
WheelAction::Pause { duration } => WDActions::WheelActionItem::General(
WDActions::GeneralAction::Pause(WDActions::PauseAction {
duration: Some(duration.as_millis() as u64),
}),
),

Check warning on line 480 in src/actions.rs

View check run for this annotation

Codecov / codecov/patch

src/actions.rs#L474-L480

Added lines #L474 - L480 were not covered by tests
WheelAction::Scroll {
x,
y,
delta_x,
delta_y,
duration,
} => WDActions::WheelActionItem::Wheel(WDActions::WheelAction::Scroll(
WDActions::WheelScrollAction {
duration: duration.map(|d| d.as_millis() as u64),
origin: WDActions::PointerOrigin::Viewport,
x: Some(x),
y: Some(y),
deltaX: Some(delta_x),
deltaY: Some(delta_y),
},
)),

Check warning on line 496 in src/actions.rs

View check run for this annotation

Codecov / codecov/patch

src/actions.rs#L482-L496

Added lines #L482 - L496 were not covered by tests
}
}

Check warning on line 498 in src/actions.rs

View check run for this annotation

Codecov / codecov/patch

src/actions.rs#L498

Added line #L498 was not covered by tests
}

/// A sequence of actions to be performed.
///
/// See the documentation for [`Actions`] for more details.
Expand Down Expand Up @@ -495,6 +589,19 @@ impl InputSource for TouchActions {
}
}

impl InputSource for WheelActions {
type Action = WheelAction;

fn pause(self, duration: Duration) -> Self {
self.then(WheelAction::Pause { duration })
}

Check warning on line 597 in src/actions.rs

View check run for this annotation

Codecov / codecov/patch

src/actions.rs#L595-L597

Added lines #L595 - L597 were not covered by tests

fn then(mut self, action: Self::Action) -> Self {
self.actions.push(action);
self
}

Check warning on line 602 in src/actions.rs

View check run for this annotation

Codecov / codecov/patch

src/actions.rs#L599-L602

Added lines #L599 - L602 were not covered by tests
}

/// A list of action sequences to be performed via [`Client::perform_actions()`]
///
/// An [`ActionSequence`] is a sequence of actions of a specific type.
Expand Down

0 comments on commit b31b6ce

Please sign in to comment.