1

我基本上是在尝试获取“has_content?”的功能?或选项 :count 为 has_css 提供的“should have_content”匹配器?匹配器。

4

1 回答 1

2

你能试试这样的东西吗all('a').length

这里有几个例子:

describe 'my awesome page' do
  before :each do
    visit root_path
  end

  # css just for querying DOM
  it 'number of titles should be huge' do
    page.all(:css,'h2.awesome_header').length.should have_at_least(3).items
  end
  it 'has exactly 4 div elements' do
    page.all(:css,'div.awesome_class').length.should == 4
  end

  # For querying content you can try :xpath instead :css. 
  it 'has paragraps with text' do
    page.all(:xpath, '//div[contains(., "t")]').length.should == 9
  end
end

更新:

我找到了更优雅的方法来做到这一点。在此处查看文档:http ://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders: 这里都是工作示例

page.all(:css, 'p', :text => /.*wellcome.*/).length.should == 2
于 2012-07-13T19:52:30.800 回答