Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data-testid as a type of selector #256

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion thirtyfour-macros/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -458,6 +460,13 @@ impl TryFrom<Meta> 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 {
Expand Down Expand Up @@ -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),
}
}
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions thirtyfour/src/common/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub enum BySelector {
ClassName(Arc<str>),
/// Select an element by CSS.
Css(Arc<str>),
/// Select an element by data-testid.
Testid(Arc<str>),
}

/// Element Selector struct providing a convenient way to specify selectors.
Expand Down Expand Up @@ -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 {
Expand All @@ -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),
}
}
}
Expand All @@ -164,6 +174,7 @@ impl From<BySelector> 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)),
}
}
}
Expand Down