appium_client/commands/
apps.rs1use std::time::Duration;
3use async_trait::async_trait;
4use fantoccini::error::CmdError;
5use http::Method;
6use serde_json::json;
7use serde::{Serialize, Deserialize};
8use crate::{AndroidClient, AppiumClientTrait, IOSClient};
9use crate::commands::AppiumCommand;
10
11#[async_trait]
13pub trait InteractsWithApps: AppiumClientTrait {
14 async fn install_app(&self, path: &str) -> Result<(), CmdError> {
15 self.issue_cmd(AppiumCommand::Custom(
16 Method::POST,
17 "appium/device/install_app".to_string(),
18 Some(json!({
19 "appPath": path
20 })),
21 )).await?;
22 Ok(())
23 }
24
25 async fn is_app_installed(&self, bundle_id: &str) -> Result<bool, CmdError> {
26 let value = self.issue_cmd(AppiumCommand::Custom(
27 Method::POST,
28 "appium/device/app_installed".to_string(),
29 Some(json!({
30 "bundleId": bundle_id
31 })),
32 )).await?;
33
34 Ok(serde_json::from_value(value)?)
35 }
36
37 async fn run_app_in_background(&self, duration: Duration) -> Result<(), CmdError> {
38 self.issue_cmd(AppiumCommand::Custom(
39 Method::POST,
40 "appium/app/background".to_string(),
41 Some(json!({
42 "seconds": duration.as_secs()
43 })),
44 )).await?;
45
46 Ok(())
47 }
48
49 async fn remove_app(&self, bundle_id: &str) -> Result<(), CmdError> {
50 self.issue_cmd(AppiumCommand::Custom(
51 Method::POST,
52 "appium/device/remove_app".to_string(),
53 Some(json!({
54 "bundleId": bundle_id
55 })),
56 )).await?;
57
58 Ok(())
59 }
60
61 async fn activate_app(&self, bundle_id: &str) -> Result<(), CmdError> {
62 self.issue_cmd(AppiumCommand::Custom(
63 Method::POST,
64 "appium/device/activate_app".to_string(),
65 Some(json!({
66 "bundleId": bundle_id
67 })),
68 )).await?;
69
70 Ok(())
71 }
72
73 async fn app_state(&self, bundle_id: &str) -> Result<AppState, CmdError> {
74 let value = self.issue_cmd(AppiumCommand::Custom(
75 Method::POST,
76 "appium/device/app_state".to_string(),
77 Some(json!({
78 "bundleId": bundle_id
79 })),
80 )).await?;
81
82 Ok(serde_json::from_value(value)?)
83 }
84
85 async fn terminate_app(&self, bundle_id: &str) -> Result<(), CmdError> {
86 self.issue_cmd(AppiumCommand::Custom(
87 Method::POST,
88 "appium/device/terminate_app".to_string(),
89 Some(json!({
90 "bundleId": bundle_id
91 })),
92 )).await?;
93
94 Ok(())
95 }
96}
97
98bitflags::bitflags! {
99 #[repr(transparent)]
100 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
101 pub struct AppState: i8 {
102 const NOT_INSTALLED = 0;
103 const NOT_RUNNING = 1;
104 const RUNNING_IN_BACKGROUND_SUSPENDED = 2;
105 const RUNNING_IN_BACKGROUND = 3;
106 const RUNNING_IN_FOREGROUND = 4;
107 }
108}
109
110#[async_trait]
111impl InteractsWithApps for AndroidClient {}
112
113#[async_trait]
114impl InteractsWithApps for IOSClient {}