diff --git a/thirtyfour-macros/src/component.rs b/thirtyfour-macros/src/component.rs
index 5172012..3b15255 100644
--- a/thirtyfour-macros/src/component.rs
+++ b/thirtyfour-macros/src/component.rs
@@ -268,6 +268,7 @@ enum ByToken {
XPath(Literal),
Name(Literal),
ClassName(Literal),
+ Testid(Literal),
Multi,
/// NotEmpty is the default but can be specified to be explicit.
NotEmpty,
@@ -294,7 +295,8 @@ impl ByToken {
| ByToken::Css(_)
| ByToken::XPath(_)
| ByToken::Name(_)
- | ByToken::ClassName(_) => "selector",
+ | ByToken::ClassName(_)
+ | ByToken::Testid(_) => "selector",
ByToken::Multi => "multi",
ByToken::NotEmpty => "not_empty",
ByToken::AllowEmpty => "allow_empty",
@@ -458,6 +460,13 @@ impl TryFrom for ByToken {
..
}),
) if k.is_ident("class") => Ok(ByToken::ClassName(v.token())),
+ (
+ k,
+ Expr::Lit(ExprLit {
+ lit: Lit::Str(v),
+ ..
+ }),
+ ) if k.is_ident("testid") => Ok(ByToken::Testid(v.token())),
(
k,
Expr::Lit(ExprLit {
@@ -525,6 +534,7 @@ impl ByTokens {
ByToken::XPath(xpath) => ret.push(quote! { By::XPath(#xpath) }),
ByToken::Name(name) => ret.push(quote! { By::Name(#name) }),
ByToken::ClassName(class_name) => ret.push(quote! { By::ClassName(#class_name) }),
+ ByToken::Testid(id) => ret.push(quote! { By::Testid(#id) }),
t => self.tokens.push(t),
}
}
@@ -679,6 +689,7 @@ impl ToTokens for DebugByTokens {
| ByToken::XPath(lit)
| ByToken::Name(lit)
| ByToken::ClassName(lit)
+ | ByToken::Testid(lit)
| ByToken::Description(lit) => lit.to_tokens(tokens),
// idents
ByToken::Multi
diff --git a/thirtyfour/src/common/command.rs b/thirtyfour/src/common/command.rs
index 6df0b29..654ecce 100644
--- a/thirtyfour/src/common/command.rs
+++ b/thirtyfour/src/common/command.rs
@@ -65,6 +65,8 @@ pub enum BySelector {
ClassName(Arc),
/// Select an element by CSS.
Css(Arc),
+ /// Select an element by data-testid.
+ Testid(Arc),
}
/// Element Selector struct providing a convenient way to specify selectors.
@@ -130,6 +132,13 @@ impl By {
selector: BySelector::Css(format!(".{}", name.into()).into()),
}
}
+
+ /// Select element by testid.
+ pub fn Testid(id: impl IntoArcStr) -> Self {
+ Self {
+ selector: BySelector::Css(format!("[data-testid=\"{}\"]", id.into()).into()),
+ }
+ }
}
impl fmt::Display for BySelector {
@@ -143,6 +152,7 @@ impl fmt::Display for BySelector {
BySelector::Tag(tag) => write!(f, "Tag({})", tag),
BySelector::ClassName(cname) => write!(f, "Class({})", cname),
BySelector::Css(css) => write!(f, "CSS({})", css),
+ BySelector::Testid(id) => write!(f, "Testid({})", id),
}
}
}
@@ -164,6 +174,7 @@ impl From for Selector {
BySelector::Tag(x) => Selector::new("css selector", x),
BySelector::ClassName(x) => Selector::new("css selector", format!(".{}", x)),
BySelector::Css(x) => Selector::new("css selector", x),
+ BySelector::Testid(x) => Selector::new("testid selector", format!("[data-testid=\"{}\"]", x)),
}
}
}