use fantoccini::Locator;
use std::fmt;
use std::fmt::Debug;
#[derive(Debug, Clone)]
pub enum BySelector {
Id(String),
LinkText(String),
Css(String),
XPath(String),
}
#[derive(Debug, Clone)]
pub struct By {
selector: BySelector,
}
impl fmt::Display for By {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.selector {
BySelector::Id(id) => write!(f, "Id({})", id),
BySelector::XPath(xpath) => write!(f, "XPath({})", xpath),
BySelector::LinkText(text) => write!(f, "Link Text({})", text),
BySelector::Css(css) => write!(f, "CSS({})", css),
}
}
}
impl By {
#[allow(non_snake_case)]
pub fn Id(id: &str) -> Self {
Self {
selector: BySelector::Id(id.to_string()),
}
}
#[allow(non_snake_case)]
pub fn LinkText(text: &str) -> Self {
Self {
selector: BySelector::LinkText(text.to_string()),
}
}
#[allow(non_snake_case)]
pub fn Css(css: &str) -> Self {
Self {
selector: BySelector::Css(css.to_string()),
}
}
#[allow(non_snake_case)]
pub fn XPath(x: &str) -> Self {
Self {
selector: BySelector::XPath(x.to_string()),
}
}
#[allow(non_snake_case)]
pub fn Name(name: &str) -> Self {
Self {
selector: BySelector::Css(format!(r#"[name="{}"]"#, name)),
}
}
#[allow(non_snake_case)]
pub fn Tag(tag: &str) -> Self {
Self {
selector: BySelector::Css(tag.to_string()),
}
}
#[allow(non_snake_case)]
pub fn ClassName(name: &str) -> Self {
Self {
selector: BySelector::Css(format!(".{}", name)),
}
}
pub fn locator(&self) -> Locator {
match &self.selector {
BySelector::Id(id) => Locator::Id(id),
BySelector::LinkText(text) => Locator::LinkText(text),
BySelector::Css(css) => Locator::Css(css),
BySelector::XPath(x) => Locator::XPath(x),
}
}
}