Skip to main content

appium_client/commands/
location.rs

1//! Geolocation
2use async_trait::async_trait;
3use fantoccini::error::CmdError;
4use http::Method;
5use serde_derive::{Deserialize, Serialize};
6use serde_json::json;
7use crate::{AndroidClient, AppiumClientTrait, IOSClient};
8use crate::commands::AppiumCommand;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct Location {
12    pub latitude: f64,
13    pub longitude: f64,
14    pub altitude: f64,
15}
16
17impl Location {
18    pub fn new(latitude: f64, longitude: f64, altitude: f64) -> Location {
19        Location {
20            latitude, longitude, altitude
21        }
22    }
23}
24
25/// Retrieve current geolocation (or set it on emulator)
26#[async_trait]
27pub trait SupportsLocation : AppiumClientTrait {
28    async fn location(&self) -> Result<Location, CmdError> {
29        let value = self.issue_cmd(AppiumCommand::Custom(
30            Method::GET,
31            "location".to_string(),
32            None
33        )).await?;
34
35        Ok(serde_json::from_value(value)?)
36    }
37
38    /// Tries to set location if the driver/device supports it. Returns location of device after the attempt.
39    ///
40    /// Due to configuration or limitation of device, the location change may fail silently.
41    /// The returned [Location] is the location that the device currently uses (or where is actually is).
42    async fn set_location(&self, location: Location) -> Result<Location, CmdError> {
43        let value = self.issue_cmd(AppiumCommand::Custom(
44            Method::POST,
45            "location".to_string(),
46            Some(json!({
47                "location": location
48            }))
49        )).await?;
50
51        Ok(serde_json::from_value(value)?)
52    }
53}
54
55#[async_trait]
56impl SupportsLocation for AndroidClient {}
57
58#[async_trait]
59impl SupportsLocation for IOSClient {}
60
61#[derive(Clone, Debug, Serialize)]
62pub struct AndroidGeoLocation {
63    pub latitude: f64,
64    pub longitude: f64,
65    pub altitude: f64,
66    pub satellites: u32,
67    pub speed: f64,
68}
69
70impl AndroidGeoLocation {
71    pub fn new(location: Location, satellites: u32, speed: f64) -> AndroidGeoLocation {
72        AndroidGeoLocation {
73            latitude: location.latitude,
74            longitude: location.longitude,
75            altitude: location.altitude,
76            satellites,
77            speed
78        }
79    }
80}
81
82/// Set android geolocation (with extended options)
83#[async_trait]
84pub trait SupportsAndroidLocation : AppiumClientTrait {
85    async fn set_android_location(&self, location: AndroidGeoLocation) -> Result<Location, CmdError> {
86        let value = self.issue_cmd(AppiumCommand::Custom(
87            Method::POST,
88            "location".to_string(),
89            Some(json!({
90                "location": location
91            }))
92        )).await?;
93
94        Ok(serde_json::from_value(value)?)
95    }
96
97}
98
99#[async_trait]
100impl SupportsAndroidLocation for AndroidClient {}