gpt4 book ai didi

ruby-on-rails - capybara 匹配器的按钮或链接的存在

转载 作者:行者123 更新时间:2023-12-03 09:17:46 24 4
gpt4 key购买 nike

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

例如, capybara 有以下步骤:

page.should have_button('Click me')

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

最佳答案

更新的答案(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

它调用选择器 :link_or_button。此选择器在此处定义: 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

它调用此方法: 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

所以我试图检查选择器的存在,并且它起作用了:
page.should have_selector(:link_or_button, 'Click me')

关于ruby-on-rails - capybara 匹配器的按钮或链接的存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13865625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com