Skip to main content

appium_client/
capabilities.rs

1//! A set of parameters used to start an Appium session.
2//!
3//! ## What are capabilities?
4//! The information in the set is used to describe what sort of "capabilities" you want your session to have,
5//! for example, a certain mobile operating system or a certain version of a device.
6//!
7//! When you start your Appium session, your Appium client will include the set of capabilities
8//! you've defined as an object in the JSON-formatted body of the request.
9//!
10//! Capabilities are represented as key-value pairs, with values allowed to be any valid JSON type, including other objects.
11//! Appium will then examine the capabilities and make sure that it can satisfy them before proceeding to start the session and
12//! return an ID representing the session to your client library.
13//!
14//! See also <https://appium.io/docs/en/2.1/guides/caps/>.
15//!
16//! ## Platform-specific capabilities
17//! You can create capabilities to pass to Appium sercer by using either [android::AndroidCapabilities] or [ios::IOSCapabilities].
18//!
19//! For example, if you wish to use UiAutomator2 as a driver for Android tests, you can write:
20//! ```rust
21//! use appium_client::capabilities::android::AndroidCapabilities;
22//! use appium_client::capabilities::{AppCapable, AppiumCapability, UdidCapable, UiAutomator2AppCompatible};
23//!
24//! let mut capabilities = AndroidCapabilities::new_uiautomator();
25//! capabilities.udid("emulator-5554");  // This capability selects the device you wish to run test on
26//! capabilities.app("/apps/sample.apk"); // This is a path to apk on your computer
27//! capabilities.app_wait_activity("com.example.AppActivity"); // This is an activity of a home screen of the app.
28//!
29//! // If you wish to specify appium capabilities manually (without predefined methods),
30//! // then use methods like set_bool, set_str, set_number
31//! capabilities.set_bool("appium:noReset", true);
32//! ```
33//!
34//! ## Blank capabilities
35//! To use blank capabilities (without any predefined methods for ease of configuration), use [empty::EmptyCapabilities].
36//! [empty::EmptyCapabilities] lets you configure Appium for any driver that was not implemented out of the box.
37//!
38//! Note that you will loose many built in features for Android and iOS, basically [empty::EmptyCapabilities] requires
39//! that you setup everything by yourself (including some Appium commands).
40
41pub mod ios;
42pub mod automation;
43pub mod android;
44pub mod empty;
45
46use std::ops::{Deref, DerefMut};
47use std::time::Duration;
48use fantoccini::wd::Capabilities;
49use serde_json::{Number, Value};
50
51/// Extensions to easily define capabilities for Appium driver. See <https://appium.io/docs/en/2.1/guides/caps/>.
52pub trait AppiumCapability
53    where Self: Deref<Target=Capabilities>,
54          Self: DerefMut<Target=Capabilities> {
55
56    /// Set the automation driver to use (the engine for tests, eg. XCuiTest for iOS).
57    /// 
58    /// Appium usually autoselects the driver based on platform, but you choose the preferred driver.
59    /// See [automation] for possible values.
60    fn automation_name(&mut self, automation_name: &str) {
61        self.set_str("appium:automationName", automation_name);
62    }
63
64    /// The version of a platform, e.g., for iOS, "16.0"
65    fn platform_version(&mut self, version: &str) {
66        self.set_str("appium:platformVersion", version);
67    }
68
69    /// The name of a particular device to automate.
70    ///
71    /// For example "iPhone 14".
72    /// Currently only actually useful for specifying iOS simulators,
73    /// since in other situations it's typically recommended to use a specific device
74    /// id via the `appium:udid` capability.
75    fn device_name(&mut self, device_name: &str) {
76        self.set_str("appium:deviceName", device_name);
77    }
78
79    /// Sets a string capability.
80    ///
81    /// For example `set_str("appium:deviceName", "iPhone 14")`.
82    fn set_str(&mut self, name: &str, value: &str) {
83        self.insert(name.to_string(), Value::String(value.to_string()));
84    }
85
86    /// Sets a number capability.
87    ///
88    /// For example `set_number("appium:newCommandTimeout", Number::from(120u64))`.
89    fn set_number(&mut self, name: &str, value: Number) {
90        self.insert(name.to_string(), Value::Number(value));
91    }
92
93    /// Sets a boolean capability.
94    ///
95    /// For example `set_bool("appium:noReset", true)`
96    fn set_bool(&mut self, name: &str, value: bool) {
97        self.insert(name.to_string(), Value::Bool(value));
98    }
99}
100
101/// Capabilities for drivers that are used to run test on a device.
102pub trait UdidCapable: AppiumCapability {
103    /// Device id.
104    ///
105    /// Android id can be retrieved by using ADB (`adb devices`).
106    /// For iOS, it's the phone's serial number.
107    fn udid(&mut self, udid: &str) {
108        self.set_str("appium:udid", udid);
109    }
110}
111
112/// Capabilities for drivers that are used to run an app.
113pub trait AppCapable: AppiumCapability {
114    /// The path to an installable application.
115    fn app(&mut self, app_path: &str) {
116        self.set_str("appium:app", app_path);
117    }
118
119    /// App or list of apps (as a JSON array) to install prior to running tests.
120    ///
121    /// Note that it will not work with `automationName` of `Espresso` and iOS real devices
122    fn other_apps(&mut self, paths: &[&str]) {
123        let paths = paths.iter()
124            .map(|p| Value::String(p.to_string()))
125            .collect();
126
127        self.insert("appium:otherApps".to_string(), Value::Array(paths));
128    }
129
130    /// Don't reset app state before this session.
131    ///
132    /// "Reset" means to delete app data (like a fresh install).
133    /// If true, instruct an Appium driver to avoid its usual reset logic during session start and cleanup (default false).
134    fn no_reset(&mut self, no_reset: bool) {
135        self.set_bool("appium:noReset", no_reset);
136    }
137
138    /// Perform a complete reset.
139    ///
140    /// "Complete reset" usually means a reinstall.
141    /// If true, instruct an Appium driver to augment its usual reset logic with additional steps to ensure maximum environmental reproducibility (default false)
142    fn full_reset(&mut self, full_reset: bool) {
143        self.set_bool("appium:fullReset", full_reset);
144    }
145
146    /// When a find operation fails, print the current page source. Defaults to false.
147    ///
148    /// When the element you're looking for is not found on screen, then this setting will print DOM
149    /// of the visible app screen.
150    /// This DOM can be further inspected to check if the locator is correct, or if the correct page is displayed.
151    fn print_page_source_on_find_failure(&mut self, value: bool) {
152        self.set_bool("appium:printPageSourceOnFindFailure", value);
153    }
154}
155
156/// Capabilities for UiAutomator2 (Android).
157pub trait UiAutomator2AppCompatible: AppiumCapability {
158    /// Activity name for the Android activity you want to launch from your package.
159    ///
160    /// This often needs to be preceded by a `.` (a dot, e.g., `.MainActivity` instead of `MainActivity`).
161    /// By default this capability is received from the package manifest.
162    fn app_activity(&mut self, activity: &str) {
163        self.set_str("appium:appActivity", activity);
164    }
165
166    /// Java package of the Android app you want to run.
167    ///
168    /// By default this capability is received from the package manifest.
169    fn app_package(&mut self, activity: &str) {
170        self.set_str("appium:appPackage", activity);
171    }
172
173    /// Activity name/names, comma separated, for the Android activity you want to wait for.
174    ///
175    /// By default the value of this capability is the same as for appActivity.
176    /// You must set it to the very first focused application activity name in case it is different
177    /// from the one which is set as appActivity if your capability has `appActivity` and `appPackage`.
178    /// You can also use wildcards (*).
179    fn app_wait_activity(&mut self, activity: &str) {
180        self.set_str("appium:appWaitActivity", activity);
181    }
182
183    /// Java package of the Android app you want to wait for.
184    ///
185    /// By default the value of this capability is the same as for appActivity.
186    fn app_wait_package(&mut self, activity: &str) {
187        self.set_str("appium:appWaitPackage", activity);
188    }
189
190    /// Timeout in milliseconds used to wait for the appWaitActivity to launch (default 20000)
191    fn app_wait_duration(&mut self, duration: Duration) {
192        self.set_number("appium:appWaitDuration", Number::from(duration.as_millis() as u64));
193    }
194
195    /// Timeout in milliseconds used to wait for an apk to install to the device. Defaults to 90000
196    fn android_install_timeout(&mut self, duration: Duration) {
197        self.set_number("appium:androidInstallTimeout", Number::from(duration.as_millis() as u64));
198    }
199
200    /// Block until app starts.
201    ///
202    /// Whether to block until the app under test returns the control to the caller after its activity
203    /// has been started by Activity Manager (true, the default value) or to continue the test without waiting for that (false)
204    fn app_wait_for_launch(&mut self, value: bool) {
205        self.set_bool("appium:appWaitForLaunch", value);
206    }
207
208    /// Always start app forcefully when testing starts.
209    ///
210    /// Set it to true if you want the application under test to be always forcefully
211    /// restarted on session startup even if appium:noReset is true,
212    /// and the app was already running. If noReset is falsy, then the app under test is going
213    /// to be restarted if either this capability set to true or appium:dontStopAppOnReset is falsy
214    /// (the default behavior). false by default. Available since driver version 2.12
215    fn force_app_launch(&mut self, value: bool) {
216        self.set_bool("appium:forceAppLaunch", value)
217    }
218
219    /// Whether to launch the application under test automatically after a test starts.
220    ///
221    /// Default: true
222    fn auto_launch(&mut self, value: bool) {
223        self.set_bool("appium:autoLaunch", value)
224    }
225
226    /// Set an optional intent category to be applied when starting the given appActivity by Activity Manager.
227    ///
228    /// Defaults to `android.intent.category.LAUNCHER`.
229    /// Please use `mobile:startActivity` in case you don't set an explicit value.
230    fn intent_category(&mut self, value: &str) {
231        self.set_str("appium:intentCategory", value);
232    }
233
234    /// Set an optional intent action to be applied when starting the given appActivity by Activity Manager.
235    ///
236    /// Defaults to `android.intent.action.MAIN`. Please use `mobile:startActivity` in case you don't set an explicit value.
237    fn intent_action(&mut self, value: &str) {
238        self.set_str("appium:intentAction", value);
239    }
240
241    /// Set an optional intent flags to be applied when starting the given appActivity by Activity Manager.
242    ///
243    /// Defaults to 0x10200000 (FLAG_ACTIVITY_NEW_TASK)
244    fn intent_flags(&mut self, value: &str) {
245        self.set_str("appium:intentFlags", value);
246    }
247
248    /// Set an optional intent arguments to be applied when starting the given appActivity by Activity Manager
249    fn optional_intent_arguments(&mut self, value: &str) {
250        self.set_str("appium:optionalIntentArguments", value);
251    }
252
253    /// Set it to true if you don't want the application to be restarted if it was already running.
254    ///
255    /// If appium:noReset is falsy, then the app under test is going to be restarted if either
256    /// this capability is falsy (the default behavior) or appium:forceAppLaunch is set to true.
257    ///
258    /// `false` by default
259    fn dont_stop_app_on_reset(&mut self, value: bool) {
260        self.set_bool("appium:dontStopAppOnReset", value);
261    }
262
263    /// Allows to set one or more comma-separated package identifiers to be uninstalled from the device before a test starts.
264    fn uninstall_other_packages(&mut self, value: &str) {
265        self.set_str("appium:uninstallOtherPackages", value);
266    }
267
268    /// Sets the maximum amount of application packages to be cached on the device under test.
269    ///
270    /// This is needed for devices that don't support streamed installs (Android 7 and below),
271    /// because ADB must push app packages to the device first in order to install them, which takes some time.
272    ///
273    /// Setting this capability to zero disables apps caching. 10 by default.
274    fn remote_apps_cache_limit(&mut self, value: u64) {
275        self.set_number("appium:remoteAppsCacheLimit", Number::from(value));
276    }
277
278    /// Use packages built with test flag.
279    ///
280    /// If set to true then it would be possible to use packages built with the test flag
281    /// for the automated testing (literally adds -t flag to the adb install command).
282    ///
283    /// false by default
284    fn allow_test_packages(&mut self, value: bool) {
285        self.set_bool("appium:allowTestPackages", value);
286    }
287
288    /// Reinstall app (even if it's a downgrade).
289    ///
290    /// If set to true then the application under test is always reinstalled even if a newer version
291    /// of it already exists on the device under test.
292    /// This capability has no effect if `appium:noReset` is set to true.
293    ///
294    /// `false` by default
295    fn enforce_app_install(&mut self, value: bool) {
296        self.set_bool("appium:enforceAppInstall", value);
297    }
298}
299
300/// Capabilities for Settings API (<https://appium.io/docs/en/2.1/guides/settings/>).
301pub trait AppiumSettingsCapable: AppiumCapability {
302    fn set_setting(&mut self, name: &str, value: Value) {
303        self.insert(format!("appium:settings[{name}]"), value);
304    }
305}
306
307/// Capabilities for XCUITest (iOS).
308pub trait XCUITestAppCompatible: AppiumCapability {
309    /// Bundle id of app. Looks like app package (`com.my.app`).
310    fn bundle_id(&mut self, id: &str) {
311        self.set_str("appium:bundleId", id);
312    }
313
314    /// Where to look for localizable strings. Default en.lproj
315    fn localizable_strings_dir(&mut self, dir: &str) {
316        self.set_str("appium:localizableStringsDir", dir);
317    }
318
319    /// Language to set for the simulator / emulator.
320    ///
321    /// You need to set this manually on physical devices.
322    fn language(&mut self, language: &str) {
323        self.set_str("appium:language", language);
324    }
325
326    /// Locale to set for the simulator / emulator.
327    ///
328    /// You need to set this manually on physical devices.
329    fn locale(&mut self, locale: &str) {
330        self.set_str("appium:locale", locale);
331    }
332
333    /// Calendar format to set for the iOS Simulator (eg. `gregorian`).
334    fn calendar_format(&mut self, value: &str) {
335        self.set_str("appium:calendarFormat", value);
336    }
337
338    /// Timeout for application upload in millisecond, on real devices
339    fn app_push_timeout(&mut self, duration: Duration) {
340        self.set_number("appium:appPushTimeout", Number::from(duration.as_millis() as u64));
341    }
342
343    /// Select application installation strategy for real devices.
344    ///
345    /// The following strategies are supported:
346    /// * `serial` (default) - pushes app files to the device in a sequential order; this is the least performant strategy, although the most reliable;
347    /// * `parallel` - pushes app files simultaneously; this is usually the the most performant strategy, but sometimes could not be very stable;
348    /// * `ios-deploy` - tells the driver to use a third-party tool ios-deploy to install the app; obviously the tool must be installed separately first and must be present in PATH before it could be used.
349    fn app_install_strategy(&mut self, value: &str) {
350        self.set_str("appium:appInstallStrategy", value);
351    }
352
353    /// Accept all iOS alerts automatically if they pop up.
354    ///
355    /// This includes privacy access permission alerts (e.g., location, contacts, photos). Default is false.
356    fn auto_accept_alerts(&mut self, value: bool) {
357        self.set_bool("appium:autoAcceptAlerts", value);
358    }
359}