如何使用Ruby检查页面上是否存在数据测试?
如何使用Ruby检查页面上是否存在数据测试? 我之所以问这个,是因为我看到有一些搜索选择器的方法,但是我不知道这是否是正确的方法。 我想这样做
expect(page).should have_no_selector("[data-testid='prepay-enroll-now-button']");
expect(page).should have_selector("[data-testid='prepay-enroll-now-button']");
How can I check whether or not a data-testid exists on my page using Ruby ?
I ask this because I see that there are some ways to search for selectors, but I don't know exactly if this is the correct way or not.
I'm trying to do it like this
expect(page).should have_no_selector("[data-testid='prepay-enroll-now-button']");
expect(page).should have_selector("[data-testid='prepay-enroll-now-button']");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在执行的操作对于CSS属性选择器有效,但是您不应该使用与RSPECS期望语法使用。另外,由于
have_no_selector
可以从CSS的正常默认值更改为XPATH(或任何其他支持的选择器类型),因此我建议使用HAS_CSS
或have_no_css
代码>使用CSS时。或者
,如果您广泛使用
data-Testid
属性,则可能需要设置capybara.test_id ='data-testid'
,然后将您使用data-Testid作为其他Capybara方法中的定位器。例如,如果“ prepay-enroll-now-button'是实际链接或按钮(而不是仅带有行为的div),则可以做诸如或之类的事情
What you are doing is valid for a CSS attribute selector, however you shouldn't be using
should
with RSpecs expect syntax. Also, sincehave_no_selector
can be changed from the normal default of CSS to XPath (or any other supported selector type) I'd recommend usinghave_css
orhave_no_css
when you are using CSS.or
Also, if you are widely using
data-testid
attributes you may want setCapybara.test_id = 'data-testid'
which will then let you use the data-testid as the locator in other Capybara methods. For instance if 'prepay-enroll-now-button' is an actual link or button (rather than just a div with behaviors attached) you could do things likeor