我继承了一个 Rails 应用程序,但不熟悉这个特定的测试环境。当 cucumber 测试运行时,我们得到:
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>
这里到底发生了什么?
最佳答案
嗯,“这里发生了什么”非常简单:
- cucumber 使用匹配的步骤定义执行场景的每个步骤
- 一切顺利,直到
- 它执行“我应该在第一个个人资料项目中看到“profile.jpg””
- 在此步骤中,cucumber 会转到 item_steps 中匹配的步骤定义
- item_steps 中的定义调用 web_steps 中的另一个步骤定义
- 最后一个检查页面是否确实在给定范围内包含给定内容
- 检查失败...
我想,有趣的问题是“为什么它不起作用”。当我在 cucumber / capybara 场景中使用它时,非常相似的 CSS 选择器对我来说工作得很好。
您确定 HTML 代码确实显示在浏览器中吗?模板或 Controller 中没有可以阻止其显示的条件吗?当您将“ul#items li:first”更改为更简单的内容(例如“ul#items”)时会发生什么?
更新:问题确实非常简单:profile.jpg 不是文本内容 - 它是“不可见”的 html 代码。您遇到的唯一错误是场景本身。
关于ruby-on-rails - 无法找到 CSS "ul#items li:first"(Capybara::ElementNotFound),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6274840/