Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 水豚,在css元素中查找_Ruby On Rails_Testing_Cucumber_Capybara - Fatal编程技术网

Ruby on rails 水豚,在css元素中查找

Ruby on rails 水豚,在css元素中查找,ruby-on-rails,testing,cucumber,capybara,Ruby On Rails,Testing,Cucumber,Capybara,我正在与RubyonRails3、Cucumber和Capybara合作 我已经搜索了很长一段时间,但我不知道如何在css标签中找到特定的页面元素。在我的例子中,我需要确保在表中找到名称,而不是在“Welcome[name]”中 我试过这样的方法: within('table') do page.body.index("[Name]") end 我有一个带有id='table'的表 但我想知道如何对任何css元素执行此操作,例如: within('h2') do page.body.s

我正在与RubyonRails3、Cucumber和Capybara合作

我已经搜索了很长一段时间,但我不知道如何在css标签中找到特定的页面元素。在我的例子中,我需要确保在表中找到名称,而不是在“Welcome[name]”中

我试过这样的方法:

within('table') do
  page.body.index("[Name]")
end
我有一个带有
id='table'
的表

但我想知道如何对任何css元素执行此操作,例如:

within('h2') do
  page.body.should have_content ('stuff')
end
我认为我的问题与page.body有关,但我不确定如何将其包含到特定的css标记中


提前谢谢

Capybara的
内的
匹配器只匹配第一个结果,因此如果您有多个
h2
标记,它将只查找第一个

相反,使用
:text
选项尝试
使用css

page.should have_css("#table", :text => "[Name]")
page.should have_css('h2', :text => 'stuff')

要查找特定元素,请执行以下操作:

page.find('#table').should have_text('stuff')
我做过这样的事情:
page.all(:css,'button.btn primary.days.active')。size.should==1

检查是否有任何元素包含一组特定的类。
不过,我不需要寻找元素的特定文本值。
我只是想确保元素的存在,以及元素的数量。

我想所有答案都应该有效,但现在水豚不再使用should,而是使用expect

expect(page).to have_css("#table", :text => "[Name]")
expect(page).to have_css('h2', :text => 'stuff')

这是可行的,有没有一个好的方法来获取找到的元素的索引?因此,如果我想比较表中两个元素的顺序,我认为最好的方法是找到每个元素的索引,然后比较它们。have_text不再受支持。使用page.find(“#table”)。应该改为使用_内容('stuff')。