Cucumber 黄瓜+;水豚检查文本是否多次出现

Cucumber 黄瓜+;水豚检查文本是否多次出现,cucumber,capybara,Cucumber,Capybara,我想检查给定字符串在某些场景中是否多次出现 我在别处发现了这个: Then /^I should see "([^\"]*)" twice$/ do |text| regexp = Regexp.new(text + "(.+)" + text) response.body.should contain(regexp) end 这是为webrat写的。我试着用水豚来表达它: Then /^I should see "([^"]*)" twice$/ do |text| regexp

我想检查给定字符串在某些场景中是否多次出现

我在别处发现了这个:

Then /^I should see "([^\"]*)" twice$/ do |text|
  regexp = Regexp.new(text + "(.+)" + text)
  response.body.should contain(regexp)
end
这是为webrat写的。我试着用水豚来表达它:

Then /^I should see "([^"]*)" twice$/ do |text|
  regexp = Regexp.new(text + "(.+)" + text)
  if page.respond_to? :should
    page.should have_xpath('//*', :text => regexp)
  else
    assert page.has_xpath?('//*', :text => regexp)
  end
end
这给了我期望的#has_xpath(“/*”)返回true,got false


我还尝试了上述regexp的多行变体。

在弄乱了regexp之后,我最终使用的解决方案。我推测,“([^/])”将某物作为正则表达式,而“([^”])”将某物作为纯文本:

Then /^I should see "([^\/]*)" "(.+)" times$/ do |regexp, times|
  str = regexp
  for i in times
    str = str + "(.+)" + regexp
  end
  regexp = Regexp.new(str)
  if page.respond_to? :should
    page.should have_xpath('//*', :text => regexp)
  else
    assert page.has_xpath?('//*', :text => regexp)
  end
end