如何使用 Capybara(或 Webrat,我猜)和 Cucumber 检查某个单词是否多次出现?
我知道 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西应该有效:
Something like this should work: