[英]How do I find an image on a page with Cucumber / Capybara in Rails 3
I am using Cucumber / Capybara with Rails 3 and am trying to validate the existence of an image after upload. 我正在使用Cucumber / Capybara和Rails 3,我试图在上传后验证图像是否存在。 I'm not sure how to check the url of the image to validate it.
我不确定如何检查图像的URL来验证它。
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. 我假设您需要一个nokogiri步骤来正确找到它。 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. Nokogiri的解决方案应该可以正常工作。 The only problem is that the Cabybara API is different to Webrat, so instead of
response.body
, you must use page.body
. 唯一的问题是Cabybara API与Webrat不同,因此您必须使用
page.body
而不是response.body
。
But theres an even better way to test for the image with Cabybara : 但是有一种更好的方法来测试Cabybara的图像:
Then /^I should see the image "(.+)"$/ do |image|
page.should have_xpath("//img[@src=\"/public/images/#{image}\"]")
end
Hi I'm not good with XPATH but with CSS you can try : 嗨,我不熟悉XPATH,但使用CSS,你可以尝试:
if page.respond_to? :should page.should have_selector("img[src$='#{imagename}']") else assert page.has_selector?("img[src$='#{imagename}']") end
wish helps ! 希望有所帮助 jramby
jramby
You can test it in this way, and also not depending on the path: 您可以通过这种方式测试它,也不依赖于路径:
Then /^I should see the image "(.+)"$/ do |image|
page.should have_xpath("//img[contains(@src, \"#{image}\")]")
end
page.should
page.should
is now deprecated. 现已弃用。 Use instead
请改用
expect(page).to
期待(页)。为了
Full example : 完整示例:
Then /^I should see the image "(.+)"$/ do |image|
expect(page).to have_xpath("//img[contains(@src, \"#{image}\")]")
end
这有用吗?
page.should have_xpath('//img[@src="/public/images/foo.png"]')
If you have a lot of images that you would like to test you could create a has_image? 如果你想测试很多图像,你可以创建一个has_image吗? matcher for Capybara.
Capybara的匹配器。
It is easy and this post explains it step-by-step: Adding a has_image? 这很简单,这篇文章一步一步解释: 添加一个has_image? Matcher to Capybara
匹配给Capybara
Yes, but these xpath tests won't deal with the fact that... 是的,但这些xpath测试不会处理......
/public/images/foo.jpg
public/images/foo.jpg
http://mydomain.com/public/images/foo.jpg
...are all the same valid link to the image. ...都是与图像相同的有效链接。
this syntax worked for me and is more readable. 这个语法对我有用,而且更具可读性。
page.should have_css("img", :src => "/public/images/foo.png")
page.should have_css(“img”,:src =>“/ public / images / foo.png”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.