Alv*_*Lee 3 ruby cucumber capybara
have_selector在尝试匹配文本完全匹配的元素时,我很难使用Capybara .我知道这可以使用正则表达式完成,但是我阅读Capybara的博客文章让我相信我可以使用:exactarg或set Capybara.exact = true.我目前正在使用Capybara 2.2.1.这是我有的:
假设我有一个页面(称为"测试页面").在这个页面是这样的:
<div id="my_div">abcdef</div>
Run Code Online (Sandbox Code Playgroud)
我有一个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
Run Code Online (Sandbox Code Playgroud)
我的步骤定义如下所示:
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', :text => text, :exact => true)
end
Run Code Online (Sandbox Code Playgroud)
与我的期望相反,我收到以下错误:
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)
Run Code Online (Sandbox Code Playgroud)
这让我相信:exact => true在我的have_selector电话中没有做任何事情.我同样试图设置Capybara.exact = true我的测试设置配置,但这似乎也没有影响我的测试,我希望我的方式.
我错过了什么吗?或者我误解了该选项应该如何使用?我知道我总是可以使用正则表达式语法来匹配精确的文本字符串,但我认为该:exact选项专门针对这种情况.
有一个开放的功能请求,Capybara Github Issue 1256,让have_selector(和其他人)支持精确的文本匹配.
但是,目前(在Capybara 2.x中),该:exact选项不适用于该:text选项.如果我没记错的话,它是以下一段代码(来自query.rb)进行文本选项过滤.如您所见,该:exact选项没有逻辑或考虑因素.
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
Run Code Online (Sandbox Code Playgroud)
根据Capybara文档,:exact选项实际上是:
控制
is给定XPath中的表达式是完全匹配还是部分匹配
这基本上只在Capybara :: Node :: Actions中使用,其中文本(或id,name等)未指定为选项 - 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)
Run Code Online (Sandbox Code Playgroud)