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;
#[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 {
pub fn id(id: &str) -> By {
By::Id(id.to_string())
}
pub fn name(name: &str) -> By {
By::Name(name.to_string())
}
pub fn xpath(query: &str) -> By {
By::Xpath(query.to_string())
}
pub fn uiautomator(query: &str) -> By {
By::UiAutomator(query.to_string())
}
pub fn android_data_matcher(query: &str) -> By {
By::AndroidDataMatcher(query.to_string())
}
pub fn android_view_matcher(query: &str) -> By {
By::AndroidViewMatcher(query.to_string())
}
pub fn android_view_tag(query: &str) -> By {
By::AndroidViewTag(query.to_string())
}
pub fn ios_class_chain(query: &str) -> By {
By::IosClassChain(query.to_string())
}
pub fn ios_ns_predicate(query: &str) -> By {
By::IosNsPredicate(query.to_string())
}
pub fn accessibility_id(id: &str) -> By {
By::AccessibilityId(id.to_string())
}
pub fn class_name(class_name: &str) -> By {
By::ClassName(class_name.to_string())
}
pub fn image(base64_template: &str) -> By {
By::Image(base64_template.to_string())
}
pub fn custom(query: &str) -> By {
By::Custom(query.to_string())
}
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 {
async fn find_by(&self, search: By) -> Result<Element, CmdError>;
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)
}
}