Ruby-on-rails Capybara 匹配器,用于显示按钮或链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13865625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:10:56  来源:igfitidea点击:

Capybara matcher for presence of button or link

ruby-on-railscapybara

提问by mirelon

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?

网页上的用户不区分“按钮”和“链接样式为按钮”。有没有办法添加检查页面上是否存在“按钮或链接”?

For example Capybara has step:

例如水豚有步骤:

page.should have_button('Click me')

which does not find links styled as buttons.

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

回答by mirelon

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

更新的答案(如果匹配器在 RSpec 3.0+ 中被弃用):

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

Before:

前:

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

Followed from click_link_or_buttonwhich is defined here: https://github.com/jnicklas/capybara/blob/master/lib/capybara/node/actions.rb#L12

其次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

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

它调用一个选择器: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

It calls this method: http://rdoc.info/github/jnicklas/xpath/XPath/HTML#link_or_button-instance_method

它调用这个方法: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')

回答by ardochhigh

Using the expectsyntax

使用期望语法

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

This works without needing to define a custom matcher.

这无需定义自定义匹配器即可工作。

回答by Dave Oh

Personally I would give your button or link an id and look for that using

我个人会给你的按钮或链接一个 id 并使用

page.should have_css('#foo')

page.should have_css('#foo')

This way you can refer to the link or button without worrying about its implementation.

通过这种方式,您可以引用链接或按钮而无需担心其实现。

I always find this useful: https://gist.github.com/428105

我总是觉得这很有用:https: //gist.github.com/428105

回答by idrinkpabst

You can also use a custom matcher

您还可以使用自定义匹配器

RSpec::Matchers::define :have_link_or_button do |text|
  match do |page|
    Capybara.string(page.body).has_selector?(:link_or_button, text: text)
  end
end

Then do

然后做

expect(page).to have_link_or_button('Login')

回答by Jon Kern

I had an odd case where some smoke tests marched across various customer-centric login pages that had slight variations on doing the login submit button... Driven by a Cucumber table of user, org, etc.

我有一个奇怪的例子,一些冒烟测试在各种以客户为中心的登录页面上进行,这些页面在登录提交按钮上有细微的变化......由用户、组织等的 Cucumber 表驱动。

# A bit of a hack, org_name is normally a subdomain, but sometimes it is the complete domain
def login(user, org_name)
  # Use the below to automatically hit each user's org's server
  if org_name.include? '.com'
    Capybara.app_host = "http://#{org_name}"
  else
    Capybara.app_host = "http://#{org_name}.mydomain.com"
  end

  visit '/'
  fill_in 'username', :with => user
  fill_in 'userpwd', :with => '***'
  begin
    page.find(:link_or_button, 'submit')
    click_on 'submit'
  rescue Capybara::ElementNotFound
    page.find(:link_or_button, 'Log In')
    click_on 'Log In'
    rescue Capybara::ElementNotFound
      pending "Need to determine how to invoke the Login button for #{org_name} near Line ##{__LINE__} of #{__method__} in #{__FILE__} "
  end

  # -----------------------
  # Work-around for modal popup saying SSL is mismatched if you are using actual production URLs
  # The rescue is for cases that do not exhibit the modal pop-up
  page.driver.browser.switch_to.alert.accept rescue Selenium::WebDriver::Error::NoAlertPresentError

  # Ensure that login was successful
  page.should_not have_content 'Login failed'
end

回答by simonmorley

I think you can use the find button instance method:

我认为您可以使用查找按钮实例方法:

(Capybara::Element) find_button(locator)

Using id, name, value.

使用 id、名称、值。

Or if you want a link

或者如果你想要一个链接

(Capybara::Element) find_link(locator)

From: http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders#find_button-instance_method

来自:http: //rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders#find_button-instance_method

回答by Denis repon

if html:

如果是 html:

<a class="top-menu__item">text123
    <span class="label">
        <span class="label">86</span>
    </span>
</a>

not work:

不行:

  assert page.has_selector?(:link_or_button, text: 'text123')
  assert page.should have_selector(:link_or_button, text: 'text123')