0

我试图重构旧的测试。下面的代码:

describe "translation result", :js => true do
  it "translations should be shown as links to translations in second way" do
    visit '/'
    fill_in('query-field', :with => 'kOŃ')
    click_button('search-button')

   sleep(7)
   page.all(:css, '.result a').size.should eq(2)
   page.find('.result a').text.should == 'horse'
  end
end

返回信息如下:

1) translation result translations should be shown as links to translations in second way
 Failure/Error: page.find('.result a').text.should == 'horse'
 Capybara::Ambiguous:
   Ambiguous match, found 2 elements matching css ".result a"
 # ./spec/integration/result_spec.rb:12:in `block (2 levels) in <top (required)>'

我试图从描述块中的“查找方法”返回的内容中获取一个元素。这意味着我尝试如下所示:

page.find('.result a').first.text.should == 'horse'

或者

page.find('.result a')[0].text.should == 'horse'

我这样做是因为我认为当我得到两个元素时,我可以得到其中一个。你觉得我的逻辑正确吗?如何解决问题。带有代码的仓库在这里:https ://github.com/mtczerwinski/dict-app

4

1 回答 1

2

如果您真的想找到多个匹配项,则可以使用all代替:find

page.all('.result a').first.text.should == 'horse'

http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders#all-instance_method

于 2013-10-14T18:27:44.657 回答