appium_client/commands/
contexts.rs1use async_trait::async_trait;
3use fantoccini::error::CmdError;
4use http::Method;
5use serde_json::json;
6use crate::{AndroidClient, AppiumClientTrait, IOSClient};
7use crate::commands::AppiumCommand;
8
9#[async_trait]
11pub trait SupportsContextSwitching: AppiumClientTrait {
12 async fn set_context(&self, context: &str) -> Result<(), CmdError> {
13 self.issue_cmd(AppiumCommand::Custom(
14 Method::POST,
15 "context".to_string(),
16 Some(json!({"name": context})),
17 )).await?;
18 Ok(())
19 }
20
21 async fn current_context(&self) -> Result<Option<String>, CmdError> {
22 let value = self.issue_cmd(AppiumCommand::Custom(
23 Method::GET,
24 "context".to_string(),
25 None
26 )).await?;
27
28 let value: Option<String> = serde_json::from_value(value)?;
29 Ok(value.filter(|v| v != "null"))
30 }
31
32 async fn available_contexts(&self) -> Result<Vec<String>, CmdError> {
33 let value = self.issue_cmd(AppiumCommand::Custom(
34 Method::GET,
35 "contexts".to_string(),
36 None
37 )).await?;
38
39 let value: Vec<String> = serde_json::from_value(value)?;
40 Ok(value)
41 }
42}
43
44#[async_trait]
45impl SupportsContextSwitching for AndroidClient {}
46
47#[async_trait]
48impl SupportsContextSwitching for IOSClient {}