Skip to main content

scroll/
scroll.rs

1use std::time::Duration;
2use fantoccini::actions::{InputSource, MOUSE_BUTTON_LEFT, PointerAction, TouchActions};
3use appium_client::ClientBuilder;
4use appium_client::capabilities::*;
5use appium_client::capabilities::android::AndroidCapabilities;
6use appium_client::find::{AppiumFind, By};
7
8#[tokio::main]
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let mut capabilities = AndroidCapabilities::new_uiautomator();
11    capabilities.app("https://github.com/appium/android-apidemos/releases/download/v3.1.0/ApiDemos-debug.apk");
12
13    let client = ClientBuilder::native(capabilities)
14        .connect("http://localhost:4723/")
15        .await?;
16
17    // Go to a screen with a long list
18    client.find_by(By::accessibility_id("Views"))
19        .await?
20        .click()
21        .await?;
22
23    // Let's calculate some things first
24    let (width, height) = client.get_window_size().await?;
25
26    // This is the horizontal center, it will be our x for swipe.
27    let horizontal_center = (width / 2) as f64;
28
29    // The swipe will start at 80% of screen height, and end at 20% of screen height.
30    // So we will swipe UP through most of the screen.
31    let almost_top = height as f64 * 0.2;
32    let almost_bottom = height as f64 * 0.8;
33
34    let swipe_down = TouchActions::new("finger".to_string())
35        // position the finger first
36        .then(PointerAction::MoveTo {
37            duration: Some(Duration::from_millis(0)),
38            x: horizontal_center,
39            y: almost_bottom,
40        })
41        // THEN touch the screen
42        .then(PointerAction::Down {
43            button: MOUSE_BUTTON_LEFT // believe me, it is not a mouse, but a simple touch
44        })
45        // THEN move the finger through the screen
46        .then(PointerAction::MoveTo {
47            duration: Some(Duration::from_millis(500)),
48            x: horizontal_center,
49            y: almost_top,
50        });
51
52    client.perform_actions(swipe_down)
53        .await?;
54
55    Ok(())
56}