【发布时间】:2011-01-27 04:02:38
【问题描述】:
我正在使用带有 Rails 3 的 Cucumber / Capybara 并尝试在上传后验证图像的存在。我不确定如何检查图像的 url 来验证它。
我有以下场景:
Scenario: Create new listing
Given I am on the new listing page
When I fill in "listing_name" with "Amy Johnson Photography"
And I attach the file "features/support/test_image.jpg" to "listing_images_attributes_0_photo"
And I press "Create"
Then I should see "Amy Johnson Photography"
And I should see the image "test_image.jpg"
除了最后一步,一切都通过了。
我已经为我的步骤定义尝试了此方法,如果它是页面上的文本,则效果很好,但不适用于图像 url:
Then /^I should see the image "(.+)"$/ do |image|
if page.respond_to? :should
page.should have_content(image)
else
assert page.has_content?(image)
end
end
然后我也尝试了类似这个步骤定义的东西:
Then /^I should see the image "(.+)"$/ do |image|
html = Nokogiri::HTML(response.body)
tags = html.xpath('//img[@src="/public/images/foo.png"]')
# tags.length.should eql(num_of_images)
end
导致以下错误:
And I should see the image "test_image.jpg" # features/step_definitions/listing_steps.rb:41
undefined method `body' for nil:NilClass (NoMethodError)
./features/step_definitions/listing_steps.rb:42:in `/^I should see the image "(.+)"$/'
features/manage_listings.feature:24:in `And I should see the image "test_image.jpg"'
我假设您需要一个 nokogiri 步骤才能正确找到它。或者如果有更好的方法,我愿意接受建议。如何验证我刚刚上传的图片是否在页面上?谢谢。
【问题讨论】:
-
在您的步骤定义中,您遇到了一个常见的水豚错误。如果我没记错的话,尝试使用“page.body”而不是“response.body”。
-
我不知道您是否可以在 xpath 中使用正则表达式,但如果您可以访问您的 Listing 实例,您可以只使用字符串插值 "//img[@src='/public /images/#{@listing_id}/foo.png']"
-
此页面包含一个很好的 CSS3 选择器参考,它将帮助您:reference.sitepoint.com/css/css3attributeselectors
标签: ruby-on-rails cucumber paperclip