find_by/find_by.rs
1use std::time::Duration;
2use appium_client::ClientBuilder;
3use appium_client::capabilities::*;
4use appium_client::capabilities::android::AndroidCapabilities;
5use appium_client::commands::rotation::SupportsRotation;
6use appium_client::find::{AppiumFind, By};
7use appium_client::wait::AppiumWait;
8
9#[tokio::main]
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11 // Capabilities describe the automation environment,
12 // for example: what app are we testing
13 let mut capabilities = AndroidCapabilities::new();
14 capabilities.app("https://github.com/appium/android-apidemos/releases/download/v3.1.0/ApiDemos-debug.apk");
15
16 // To add custom capability that is not supported by this library just use "insert".
17 // Alternatively, there are helpful functions like "set_bool", "set_str".
18 // You can read more about capabilities on Appium website - https://appium.io/docs/en/2.1/guides/caps/.
19 capabilities.set_str("appium:automationName", "uiautomator2");
20 capabilities.set_bool("appium:fullReset", true);
21
22 // To start automation, you need to build a client.
23 let client = ClientBuilder::native(capabilities)
24 .connect("http://localhost:4723/")
25 .await?;
26
27 // The app should automatically start, let's print the DOM of current app screen.
28 let value = client.source().await?;
29 println!("{value}");
30
31 // Screen orientation is another Appium perk
32 let orientation = client.orientation().await?;
33 println!("Screen orientation: {orientation}");
34
35 // Now we try to locate a button using UiAutomator API.
36 // Notice that the program will wait until the button appears on screen (but maximum of 30 seconds).
37 let views_button = client
38 .appium_wait()
39 .for_element(By::uiautomator("new UiSelector().text(\"Views\");"))
40 .await?;
41
42 views_button.click().await?;
43
44 // Search for a vec of elements, because we know that there will be more than one result.
45 // Notice that this time we don't wait, just find everything that's on screen as is.
46 let menu_elements = client
47 .find_all_by(By::uiautomator("new UiSelector().className(\"android.widget.TextView\");"))
48 .await?;
49
50 menu_elements.get(1)
51 .unwrap()
52 .click()
53 .await?;
54
55 // To add a timeout for wait, use "at_most".
56 // "check_every" limits how often Appium has to perform the search during wait.
57 //
58 // Sometimes it's better to use one or both of those methods, because:
59 // 1) We know that something should appear sooner, and if it doesn't, we don't want to wait full 30 seconds.
60 // 2) We don't want to slow down Appium server by checking again too often.
61 let element = client
62 .appium_wait()
63 .at_most(Duration::from_secs(20))
64 .check_every(Duration::from_millis(500))
65 .for_element(By::class_name("android.widget.ListView"))
66 .await?;
67
68 // This is a simple search for one element, without waiting for it to appear. And then we click on it.
69 // Notice that we are searching for an element inside "element" (which is a ListView).
70 element
71 .find_by(By::accessibility_id("3D Transition"))
72 .await?
73 .click()
74 .await?;
75
76 Ok(())
77}