如何在Rails 3中的Cucumber / Capybara的页面上找到图片 [英] How do I find an image on a page with Cucumber / Capybara in Rails 3
问题描述
我使用Cucumber / Capybara与Rails 3,我试图验证上传后的图像的存在。我不知道如何检查图像的URL来验证它。
我有以下情况:
假设我在新的列表页面
当我用Amy Johnson Photography填写listing_name时
我将features / support / test_image.jpg文件附加到listing_images_attributes_0_photo
然后我按创建
然后我应该看到Amy Johnson Photography
我应该看到图像test_image.jpg
除了最后一步之外的一切都通过。
我的步骤定义,如果它的页面上的文本工作伟大,但不是一个图像url:
然后/ ^我应该看到图像(。+)$ / do | image |
如果page.respond_to? :should
page.should have_content(image)
else
assert page.has_content?(image)
end
end
然后我也尝试过这样的步骤定义:
然后/ ^我应该看到图像(。+)$ / do | image |
html = Nokogiri :: HTML(response.body)
tags = html.xpath('// img [@src =/ public / images / foo.png]')
#tags.length.should eql(num_of_images)
end
我会看到图片test_image.jpg#features / step_definitions / listing_steps.rb:41
undefined方法`body'for nil:NilClass(NoMethodError)
./features/step_definitions/listing_steps.rb:42:in`/ ^我应该看到图像(。+)$ /'
/manage_listings.feature:24:in`我应该看到图像test_image.jpg'
我假设你需要一个nokogiri步骤找到这个正确。或者如果有一个更好的方法,我接受建议。如何验证我刚上传的图片是否在网页上?感谢。
解决方案Nokogiri的解决方案应该可以正常工作。唯一的问题是Cabybara API不同于Webrat,所以代替
response.body
,你必须使用page.body
。
但是这是一个更好的方法来测试与Cabybara的图像:
然后/ ^我应该看到图像(。+)$ / do | image |
page.should have_xpath(// img [@ src = \/ public / images /#{image} \])
end
I am using Cucumber / Capybara with Rails 3 and am trying to validate the existence of an image after upload. I'm not sure how to check the url of the image to validate it.
I have the following scenario:
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"
Everything passes except the last step.
I've tried this for my step definition, which works great if it's text on the page, but doesn't for an image 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've also tried something like this step definition instead:
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
which causes the following error:
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"'
I'm assuming you need a nokogiri step to find this properly. Or if there's a better way, I'm open to suggestions. How can I validate that the image I just uploaded is on the page? Thanks.
解决方案The solution with Nokogiri should work fine. The only problem is that the Cabybara API is different to Webrat, so instead of
response.body
, you must usepage.body
.But theres an even better way to test for the image with Cabybara :
Then /^I should see the image "(.+)"$/ do |image| page.should have_xpath("//img[@src=\"/public/images/#{image}\"]") end
这篇关于如何在Rails 3中的Cucumber / Capybara的页面上找到图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!