无法找到css“ul#items li:first”(Capybara :: ElementNotFound)

时间:2011-06-08 06:10:46

标签: ruby-on-rails cucumber

我继承了Rails应用程序,但我不熟悉这个特定的测试环境。当黄瓜测试运行时,我们得到:

Scenario: Add only an image to a profile by url                     # features/add_an_image_by_file_url.feature:11
    Given I am logged in as a user "admin"                            # features/step_definitions/user_steps.rb:19
    And I create a profile for "Joe Blogs"                       # features/step_definitions/profile_steps.rb:1
    And I have a fake image url "http://fake.com/images/profile.jpg" # features/step_definitions/photo_steps.rb:1
    When I follow "New photo"                                         # features/step_definitions/web_steps.rb:32
    And I fill in "Url" with "http://fake.com/images/profile.jpg"    # features/step_definitions/web_steps.rb:38
    And I press "Create"                                              # features/step_definitions/web_steps.rb:26
    Then I should see "Photo was successfully uploaded."              # features/step_definitions/web_steps.rb:99
    And I should see "profile.jpg" within first profile item          # features/step_definitions/item_steps.rb:18
      Unable to find css "ul#items li:first" (Capybara::ElementNotFound)
      (eval):2:in `find'
      ./features/step_definitions/web_steps.rb:13:in `with_scope'
      ./features/step_definitions/web_steps.rb:100:in `/^I should see "([^\"]*)"(?: within "([^\"]*)")?$/'
      features/add_an_image_by_file_url.feature:19:in `And I should see "profile.jpg" within first profile item'

这来自item_steps:

Then /^I should see "([^\"]*)" within (.*?) profile item$/ do |string, filter|
  Then %Q{I should see "#{string}" within "ul#items li:#{filter}"}
end

和网络步骤有关:

Then /^I should see "([^\"]*)"(?: within "([^\"]*)")?$/ do |text, selector|
  with_scope(selector) do
    if defined?(Spec::Rails::Matchers)
      page.should have_content(text)
    else
      assert page.has_content?(text)
    end
  end
end

有问题的HTML如下所示:

    <ul id='items'>
      <li class='note clearfix'>
        <div class='content'>
          <img src="profile.jpg"/>
        </div>
      </li>
    </ul>

这里到底发生了什么?

1 个答案:

答案 0 :(得分:3)

嗯,“这里发生了什么”非常简单:

  • 黄瓜使用匹配的步骤定义
  • 执行场景的每个步骤
  • 一切顺利,直到
  • 它执行“我应该看到”profile.jpg“在第一个个人资料项目中”
  • 在此步骤中,黄瓜进入item_steps
  • 中匹配的步骤定义
  • item_steps中的定义从web_steps
  • 调用另一个步骤定义
  • 最后一个检查页面是否确实具有给定范围内的给定内容
  • 并且检查失败......

我想,有趣的问题是“为什么它不起作用”。当我在我的黄瓜/水豚情景中使用它时,非常相似的css选择器对我来说很好。

您确定HTML代码是否真的显示在浏览器中?模板或控制器中没有条件可以阻止它显示?当你将'ul#items li:first'更改为更简单的东西,比如'ul#items'时会发生什么?

更新:问题非常简单:profile.jpg不是文本内容 - 它是一个“看不见的”HTML代码。你唯一的错误就是场景本身。