如何使用 Capybara(或 Webrat,我猜)和 Cucumber 检查某个单词是否多次出现?

发布于 2024-10-03 05:54:47 字数 2066 浏览 1 评论 0原文

我知道 /Interface \d/ 在页面上出现了 3 次。但我不知道如何用黄瓜中的水豚来测试这一点。这是我的第一次尝试:

Then /^(?:|I )should see \/([^\/]*)\/ (\d+)(?:x|X| times?)?$/ do |regexp, count|
  regexp = Regexp.new(regexp)
  count = count.to_i
  if page.respond_to? :should
    page.should have_xpath('//*', { :text => regexp, :count => count })
  else
    assert page.has_xpath?('//*', { :text => regexp, :count => count })
  end
end

但是,这对我的返回 false 然后我应该看到 /Interface \d+/ 3 次

我发现这是因为 has_xpath使用全部 。将其放入我的测试中:

puts all(:xpath, '//*', { :text => regexp}).map {|e| pp e}

结果是

#<Capybara::Element tag="html" path="/html">
#<Capybara::Element tag="body" path="/html/body">
#<Capybara::Element tag="div" path="/html/body/div">
#<Capybara::Element tag="div" path="/html/body/div/div[2]">
#<Capybara::Element tag="table" path="/html/body/div/div[2]/table">
#<Capybara::Element tag="tbody" path="/html/body/div/div[2]/table/tbody">
#<Capybara::Element tag="tr" path="/html/body/div/div[2]/table/tbody/tr[1]">
#<Capybara::Element tag="td" path="/html/body/div/div[2]/table/tbody/tr[1]/td[3]">
#<Capybara::Element tag="tr" path="/html/body/div/div[2]/table/tbody/tr[2]">
#<Capybara::Element tag="td" path="/html/body/div/div[2]/table/tbody/tr[2]/td[3]">
#<Capybara::Element tag="tr" path="/html/body/div/div[2]/table/tbody/tr[3]">
#<Capybara::Element tag="td" path="/html/body/div/div[2]/table/tbody/tr[3]/td[3]">

所以我得到了包含我的文本的元素的每一步的计数。 :-\

我想也许 has_content 会救我,但它不接受计数。

帮助!

I know that /Interface \d/ occurs three times on the page. But I don't know how to test for this with Capybara in Cucumber. Here was my first attempt:

Then /^(?:|I )should see \/([^\/]*)\/ (\d+)(?:x|X| times?)?$/ do |regexp, count|
  regexp = Regexp.new(regexp)
  count = count.to_i
  if page.respond_to? :should
    page.should have_xpath('//*', { :text => regexp, :count => count })
  else
    assert page.has_xpath?('//*', { :text => regexp, :count => count })
  end
end

However, this returns false for my Then I should see /Interface \d+/ 3 times.

I figured out that this is because has_xpath uses all. Putting this in my test:

puts all(:xpath, '//*', { :text => regexp}).map {|e| pp e}

results in

#<Capybara::Element tag="html" path="/html">
#<Capybara::Element tag="body" path="/html/body">
#<Capybara::Element tag="div" path="/html/body/div">
#<Capybara::Element tag="div" path="/html/body/div/div[2]">
#<Capybara::Element tag="table" path="/html/body/div/div[2]/table">
#<Capybara::Element tag="tbody" path="/html/body/div/div[2]/table/tbody">
#<Capybara::Element tag="tr" path="/html/body/div/div[2]/table/tbody/tr[1]">
#<Capybara::Element tag="td" path="/html/body/div/div[2]/table/tbody/tr[1]/td[3]">
#<Capybara::Element tag="tr" path="/html/body/div/div[2]/table/tbody/tr[2]">
#<Capybara::Element tag="td" path="/html/body/div/div[2]/table/tbody/tr[2]/td[3]">
#<Capybara::Element tag="tr" path="/html/body/div/div[2]/table/tbody/tr[3]">
#<Capybara::Element tag="td" path="/html/body/div/div[2]/table/tbody/tr[3]/td[3]">

So I am getting a count of every step along the way to the elements that contain my text. :-\

I thought maybe has_content would save me, but it doesn't accept a count.

Help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

能否归途做我良人 2024-10-10 05:54:47

像这样的东西应该有效:

Then /^(?:|I )should see \/([^\/]*)\/ (\d+)(?:x|X| times?)?$/ do |regexp, count|
  regexp = Regexp.new(regexp)
  count = count.to_i
  page.find(:xpath, '//body').text.split(regexp).length.should == count+1
end

Something like this should work:

Then /^(?:|I )should see \/([^\/]*)\/ (\d+)(?:x|X| times?)?$/ do |regexp, count|
  regexp = Regexp.new(regexp)
  count = count.to_i
  page.find(:xpath, '//body').text.split(regexp).length.should == count+1
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文