水豚匹配器的按钮或链接的存在 [英] Capybara matcher for presence of button or link

查看:82
本文介绍了水豚匹配器的按钮或链接的存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网页上的用户无法区分按钮和样式为按钮的链接。
有没有一种方法可以检查页面上是否存在按钮或链接?

Users on web page don't distinguish between "button" and "link styled as button". Is there a way to add check whether a "button or link" is present on page?

例如Capybara可以执行以下操作:

For example Capybara has step:

page.should have_button('Click me')

找不到样式为按钮的链接。

which does not find links styled as buttons.

推荐答案

更新了答案(RSpec中不建议使用匹配器3.0 +):

Updated answer (should matcher is deprecated in RSpec 3.0+):

expect(page).to have_selector(:link_or_button, 'Click me')

之前:

page.should have_selector(:link_or_button, 'Click me')

接着是 click_link_or_button 在此处定义: https://github.com/jnicklas/capybara/blob/master/lib/capybara/node/actions.rb#L12

def click_link_or_button(locator)
  find(:link_or_button, locator).click
end
alias_method :click_on, :click_link_or_button

它调用选择器:链接或按钮。此选择器在此处定义: https:// github。 com / jnicklas / capybara / blob / master / lib / capybara / selector.rb#L143

It calls a selector :link_or_button. This selector is defined here: https://github.com/jnicklas/capybara/blob/master/lib/capybara/selector.rb#L143

Capybara.add_selector(:link_or_button) do
  label "link or button"
  xpath { |locator| XPath::HTML.link_or_button(locator) }
end

它调用此方法:< a href = http://rdoc.info/github/jnicklas/xpath/XPath/HTML#link_or_button-instance_method rel = nofollow noreferrer> http://rdoc.info/github/jnicklas/xpath/XPath/HTML #link_or_button-instance_method

# File 'lib/xpath/html.rb', line 33

def link_or_button(locator)
  link(locator) + button(locator)
end

所以我试图检查选择器的存在,并且它起作用了:

So i tried to check the presence of the selector and it worked:

page.should have_selector(:link_or_button, 'Click me')

这篇关于水豚匹配器的按钮或链接的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆