1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! Find API for locating elements on screen
//!
//! This API can be used to find elements on screen or in a known parent.
//!
//! ## Basic usage
//! ```no_run
//!# use appium_client::capabilities::android::AndroidCapabilities;
//!# use appium_client::capabilities::{AppCapable, UdidCapable, UiAutomator2AppCompatible};
//!# use appium_client::ClientBuilder;
//!# use appium_client::find::{AppiumFind, By};
//!#
//!# #[tokio::main]
//!# async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // create capabilities & client
//! let mut capabilities = AndroidCapabilities::new_uiautomator();
//!# capabilities.udid("emulator-5554");
//!# capabilities.app("/apps/sample.apk");
//!# capabilities.app_wait_activity("com.example.AppActivity");
//!
//! let client = ClientBuilder::native(capabilities)
//!     .connect("http://localhost:4723/wd/hub/")
//!     .await?;
//!
//! // locate an element (find it)
//! let element = client
//!     .find_by(By::accessibility_id("Click this"))
//!     .await?;
//!
//! // locate all matching elements by given xpath
//! let elements = client
//!     .find_all_by(By::xpath("//*[contains(@resource-id, 'test')]"))
//!     .await?;
//! # Ok(())
//! # }
//! ```
//!
//! Notice that if you wish to get only one element (the first match), you can use [AppiumFind::find_by].
//! If you want all matches on a screen, you use [AppiumFind::find_all_by].
//!
//! ## Custom locator strategy
//!
//! If none of the options available in [By] work with your driver, then you might use [By::custom_kind] to specify custom location strategy (and search query).
//!
//! Using [By::custom_kind] is basically the same as using any other [By] variant, but you need to write more and the compiler won't tell you that you made a typo.
//!
//! Some Appium docs on the matter of locators (selectors): <https://appium.github.io/appium.io/docs/en/writing-running-appium/finding-elements/>
//!
//! Example:
//! ```no_run
//!# use appium_client::capabilities::android::AndroidCapabilities;
//!# use appium_client::capabilities::{AppCapable, UdidCapable, UiAutomator2AppCompatible};
//!# use appium_client::ClientBuilder;
//!# use appium_client::find::{AppiumFind, By};
//!#
//!# #[tokio::main]
//!# async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!# let mut capabilities = AndroidCapabilities::new_uiautomator();
//!# capabilities.udid("emulator-5554");
//!# capabilities.app("/apps/sample.apk");
//!# capabilities.app_wait_activity("com.example.AppActivity");
//!#
//!# let client = ClientBuilder::native(capabilities)
//!#     .connect("http://localhost:4723/wd/hub/")
//!#     .await?;
//!#
//! // locate an element (find it)
//! let element = client
//!     .find_by(By::accessibility_id("Find me"))
//!     .await?;
//!
//! // do the same, but more explicitly
//! let element = client
//!     .find_by(By::custom_kind("accessibility id", "Find me"))
//!     .await?;
//! # Ok(())
//! # }
//! ```
//!
use std::collections::HashMap;
use fantoccini::elements::{Element, ElementRef};
use fantoccini::Client;
use fantoccini::error::CmdError;
use serde::Serializer;
use serde_derive::Serialize;
use crate::commands::AppiumCommand;
use async_trait::async_trait;

/// Locators supported by Appium
///
/// If you wish to use your very own locator (e.g. something I didn't implement in this enum),
/// just use [By::CustomKind].
#[derive(Debug, PartialEq, Clone)]
pub enum By {
    Id(String),
    Name(String),
    Xpath(String),
    UiAutomator(String),
    AndroidDataMatcher(String),
    AndroidViewMatcher(String),
    AndroidViewTag(String),
    IosClassChain(String),
    IosNsPredicate(String),
    AccessibilityId(String),
    ClassName(String),
    Image(String),
    Custom(String),
    CustomKind(String, String)
}

#[derive(Debug, PartialEq, Serialize, Clone)]
pub struct LocatorParameters {
    pub using: String,
    pub value: String,
}

impl By {
    /// Native element identifier. resource-id for android; name for iOS.
    pub fn id(id: &str) -> By {
        By::Id(id.to_string())
    }

    /// Name of element.
    pub fn name(name: &str) -> By {
        By::Name(name.to_string())
    }

    /// Search the app XML source using xpath (not recommended, has performance issues).
    pub fn xpath(query: &str) -> By {
        By::Xpath(query.to_string())
    }

    /// Use the UI Automator API, in particular the UiSelector class to locate elements. (UiAutomator2 only).
    ///
    /// In Appium you send the Java code, as a string, to the server, which executes it in the application’s environment,
    /// returning the element or elements.
    /// 
    /// See <https://developer.android.com/reference/androidx/test/uiautomator/UiSelector>
    pub fn uiautomator(query: &str) -> By {
        By::UiAutomator(query.to_string())
    }

    /// Locate an element using Espresso [DataMatcher](https://developer.android.com/reference/android/support/test/espresso/Espresso#ondata). (Espresso only)
    pub fn android_data_matcher(query: &str) -> By {
        By::AndroidDataMatcher(query.to_string())
    }

    /// Locate an element using Espresso [ViewMatcher](https://developer.android.com/reference/android/support/test/espresso/matcher/ViewMatchers). (Espresso only)
    pub fn android_view_matcher(query: &str) -> By {
        By::AndroidViewMatcher(query.to_string())
    }

    /// Locate an element by its [view tag](https://developer.android.com/reference/android/support/test/espresso/matcher/ViewMatchers.html#withTagValue%28org.hamcrest.Matcher%3Cjava.lang.Object%3E). (Espresso only)
    pub fn android_view_tag(query: &str) -> By {
        By::AndroidViewTag(query.to_string())
    }

    /// Locate an element by a [class chain](https://pavankovurru.github.io/Appium_Mobile_Automation_Framework/documents/README_IOS.html#ios-class-chain-strategy) - a faster, but less powerful alternative to XPath on iOS.
    pub fn ios_class_chain(query: &str) -> By {
        By::IosClassChain(query.to_string())
    }

    /// A string corresponding to a recursive element search using the [iOS Predicate](https://github.com/appium/appium-xcuitest-driver/blob/master/docs/ios/ios-predicate.md). (iOS 10.0 and above)
    pub fn ios_ns_predicate(query: &str) -> By {
        By::IosNsPredicate(query.to_string())
    }

    /// Read a unique identifier for a UI element.
    ///
    /// For XCUITest it is the element's accessibility-id attribute. For Android it is the element's content-desc attribute.
    pub fn accessibility_id(id: &str) -> By {
        By::AccessibilityId(id.to_string())
    }

    /// Locate element by its class name.
    ///
    /// For IOS it is the full name of the XCUI element and begins with XCUIElementType.
    /// For Android it is the full name of the UIAutomator2 class (e.g.: android.widget.TextView)
    pub fn class_name(class_name: &str) -> By {
        By::ClassName(class_name.to_string())
    }

    /// Locate an element by matching it with a base 64 encoded image file
    pub fn image(base64_template: &str) -> By {
        By::Image(base64_template.to_string())
    }

    /// Custom locator for use with plugins registered via the customFindModules capability.
    pub fn custom(query: &str) -> By {
        By::Custom(query.to_string())
    }

    /// A locator for non-standard locators
    ///
    /// You can define what type of locator to use, so you're free to use anything here.
    pub fn custom_kind(using: &str, value: &str) -> By {
        By::CustomKind(using.to_string(), value.to_string())
    }
}

impl From<By> for LocatorParameters {
    fn from(val: By) -> Self {
        let (using, value) = match val {
            By::Id(value) => ("id".to_string(), value),
            By::Name(value) => ("name".to_string(), value),
            By::Xpath(value) => ("xpath".to_string(), value),
            By::UiAutomator(value) => ("-android uiautomator".to_string(), value),
            By::AndroidDataMatcher(value) => ("-android datamatcher".to_string(), value),
            By::AndroidViewMatcher(value) => ("-android viewmatcher".to_string(), value),
            By::AndroidViewTag(value) => ("-android viewtag".to_string(), value),
            By::IosClassChain(value) => ("-ios class chain".to_string(), value),
            By::IosNsPredicate(value) => ("-ios predicate string".to_string(), value),
            By::AccessibilityId(value) => ("accessibility id".to_string(), value),
            By::Image(value) => ("-image".to_string(), value),
            By::ClassName(value) => ("class name".to_string(), value),
            By::Custom(value) => ("-custom".to_string(), value),
            By::CustomKind(kind, value) => (kind, value)
        };

        LocatorParameters {
            using,
            value
        }
    }
}

impl serde::Serialize for By {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
        let locator_params: LocatorParameters = self.clone().into();
        locator_params.serialize(serializer)
    }
}

#[async_trait]
pub trait AppiumFind {
    /// Locates an element by given strategy.
    async fn find_by(&self, search: By) -> Result<Element, CmdError>;

    /// Locates all elements matching criteria.
    async fn find_all_by(&self, search: By) -> Result<Vec<Element>, CmdError>;
}

#[async_trait]
impl AppiumFind for Client {
    async fn find_by(&self, search: By) -> Result<Element, CmdError> {
        let value = self.issue_cmd(AppiumCommand::FindElement(search)).await?;
        let map: HashMap<String, String> = serde_json::from_value(value.clone())?;

        map.get("ELEMENT")
            .ok_or_else(|| CmdError::NotW3C(value))
            .map(|element| Element::from_element_id(
                self.clone(),
                ElementRef::from(element.clone())
            ))
    }

    async fn find_all_by(&self, search: By) -> Result<Vec<Element>, CmdError> {
        let value = self.issue_cmd(AppiumCommand::FindElements(search)).await?;
        let result: Vec<HashMap<String, String>> = serde_json::from_value(value)?;

        let elements = result.into_iter()
            .filter_map(|map| map.get("ELEMENT").cloned())
            .map(|element| Element::from_element_id(
                self.clone(),
                ElementRef::from(element)
            ))
            .collect();

        Ok(elements)
    }
}

#[async_trait]
impl AppiumFind for Element {
    async fn find_by(&self, search: By) -> Result<Element, CmdError> {
        let client = self.clone().client();
        let element_ref = self.element_id();
        let value = client.issue_cmd(AppiumCommand::FindElementWithContext(search, element_ref.to_string())).await?;
        let map: HashMap<String, String> = serde_json::from_value(value.clone())?;

        map.get("ELEMENT")
            .ok_or_else(|| CmdError::NotW3C(value))
            .map(|element| Element::from_element_id(
                client,
                ElementRef::from(element.clone())
            ))
    }

    async fn find_all_by(&self, search: By) -> Result<Vec<Element>, CmdError> {
        let client = self.clone().client();
        let element_ref = self.element_id();
        let value = client.issue_cmd(AppiumCommand::FindElementsWithContext(search, element_ref.to_string())).await?;
        let result: Vec<HashMap<String, String>> = serde_json::from_value(value)?;

        let elements = result.into_iter()
            .filter_map(|map| map.get("ELEMENT").cloned())
            .map(|element| Element::from_element_id(
                client.clone(),
                ElementRef::from(element)
            ))
            .collect();

        Ok(elements)
    }
}