加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 在Capybara中,确实:或者Capybara.exact适用于have_sele

发布时间:2020-12-17 01:33:16 所属栏目:百科 来源:网络整理
导读:在尝试匹配文本完全匹配的元素时,我很难使用Capybara的have_selector.我知道这可以使用正则表达式来完成,但是我对 Capybara’s blog post的阅读使我相信我可以使用:exact arg或set Capybara.exact = true.我目前正在使用Capybara 2.2.1.这是我有的: 假设我
在尝试匹配文本完全匹配的元素时,我很难使用Capybara的have_selector.我知道这可以使用正则表达式来完成,但是我对 Capybara’s blog post的阅读使我相信我可以使用:exact arg或set Capybara.exact = true.我目前正在使用Capybara 2.2.1.这是我有的:

假设我有一个页面(称为“测试页面”).在这个页面是这样的:

<div id="my_div">abcdef</div>

我有一个Cucumber功能测试,看起来像这样:

Feature: Test for exact text matches in have_selector
Scenario:
  Given I am on the test page
  Then I should get a partial text match "bcde" in my div
  And I should get a partial text match "abcdef" in my div
  And I should get an exact text match using regexp "abcdef" in my div
  And I should not get an exact text match using regexp "bcde" in my div
  And I should get an exact text match using arg "abcdef" in my div
  And I should not get an exact text match using arg "bcde" in my div

我的步骤定义如下所示:

Then /^I should get a partial text match "([^"]*)" in my div$/ do |text|
  page.should have_selector('div#my_div',:text => text)
end

Then /^I should get an exact text match using regexp "([^"]*)" in my div$/ do |text|
  page.should have_selector('div#my_div',:text => /^#{text}$/)
end

Then /^I should not get an exact text match using regexp "([^"]*)" in my div$/ do |text|
  page.should_not have_selector('div#my_div',:text => /^#{text}$/)
end

Then /^I should get an exact text match using arg "([^"]*)" in my div$/ do |text|
  page.should have_selector('div#my_div',:text => text,:exact => true)
end

Then /^I should not get an exact text match using arg "([^"]*)" in my div$/ do |text|
  page.should_not have_selector('div#my_div',:exact => true)
end

与我的期望相反,我收到以下错误:

And I should not get an exact text match using arg "bcde" in my div
expected not to find css "div#my_div" with text "bcde",found 1 match: "abcdef" (Capybara::ExpectationNotMet)

这让我相信:exact => true在我的have_selector调用中没有做任何事情.我有同样的尝试在我的测试设置配置中设置Capybara.exact = true,但这似乎也没有影响我的测试,我希望我的方式.

我错过了什么吗?或者我误解了该选项应该如何使用?我知道我总是可以使用正则表达式语法来匹配精确的文本字符串,但我认为:exact选项是专门针对这种情况的.

解决方法

有一个开放的功能请求 Capybara Github Issue 1256,让has_selector(和其他)支持精确的文本匹配.

但是,就目前而言(在Capybara 2.x中),:exact选项不适用于:text选项.如果我没记错的话,那就是下面的一段代码(来自query.rb)进行文本选项过滤.如您所见,没有逻辑或考虑:确切选项.

def matches_filters?(node)
  if options[:text]
    regexp = options[:text].is_a?(Regexp) ? options[:text] : Regexp.escape(options[:text].to_s)
    return false if not node.text(visible).match(regexp)
  end

根据Capybara docs,:exact选项实际上是:

Control whether is expressions in the given XPath match exactly or
partially

这基本上只在Capybara::Node::Actions中使用,其中文本(或id,名称等)未指定为选项 – ex:

# Will match links that exactly match 'Password'
click_link('Password',:exact => true)

# Will match links that contain the word 'Password' - ex 'Password Confirmation'
click_link('Password',:exact => false)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读