如何使用Ruby检查页面上是否存在数据测试?

发布于 2025-02-05 10:14:31 字数 267 浏览 1 评论 0原文

如何使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘落散花 2025-02-12 10:14:31

您正在执行的操作对于CSS属性选择器有效,但是您不应该使用与RSPECS期望语法使用。另外,由于have_no_selector可以从CSS的正常默认值更改为XPATH(或任何其他支持的选择器类型),因此我建议使用HAS_CSShave_no_css代码>使用CSS时。

expect(page).not_to have_css("[data-testid='prepay-enroll-now-button']")

或者

expect(page).to have_no_css("[data-testid='prepay-enroll-now-button']")

,如果您广泛使用data-Testid属性,则可能需要设置capybara.test_id ='data-testid',然后将您使用data-Testid作为其他Capybara方法中的定位器。例如,如果“ prepay-enroll-now-button'是实际链接或按钮(而不是仅带有行为的div),则可以做诸如

click_on('prepay-enroll-now-button')

或之类的事情

expect(page).not_to have_selector(:link_or_button, 'prepay-enroll-now-button')

What you are doing is valid for a CSS attribute selector, however you shouldn't be using should with RSpecs expect syntax. Also, since have_no_selector can be changed from the normal default of CSS to XPath (or any other supported selector type) I'd recommend using have_css or have_no_css when you are using CSS.

expect(page).not_to have_css("[data-testid='prepay-enroll-now-button']")

or

expect(page).to have_no_css("[data-testid='prepay-enroll-now-button']")

Also, if you are widely using data-testid attributes you may want set Capybara.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 like

click_on('prepay-enroll-now-button')

or

expect(page).not_to have_selector(:link_or_button, 'prepay-enroll-now-button')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文