如何在 Rails 3 中使用 Cucumber / Capybara 查找页面上的图像
我正在将 Cucumber / Capybara 与 Rails 3 一起使用,并尝试在上传后验证图像的存在。我不知道如何检查图像的 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"
除了最后一步之外,一切都过去了。
我已经在我的步骤定义中尝试过这个,如果它是页面上的文本,则效果很好,但不适用于图像网址:
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"'
I'我假设您需要 nokogiri 步骤才能正确找到它。或者如果有更好的方法,我愿意接受建议。如何验证我刚刚上传的图像是否在页面上?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Nokogiri 的解决方案应该可以正常工作。唯一的问题是 Cabybara API 与 Webrat 不同,因此您必须使用
page.body
,而不是response.body
。但还有一种更好的方法来使用 Cabybara 测试图像:
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 :
你好,我不擅长使用 XPATH,但你可以尝试使用 CSS:
希望有帮助!
杰兰比
Hi I'm not good with XPATH but with CSS you can try :
wish helps !
jramby
您可以通过这种方式进行测试,并且不依赖于路径:
You can test it in this way, and also not depending on the path:
现已弃用。改用
完整示例:
is now deprecated. Use instead
Full example :
这有效吗?
Does this work?
如果您有很多想要测试的图像,您可以创建一个 has_image?水豚的匹配器。
这很简单,这篇文章逐步解释了它: 添加 has_image?水豚匹配器
If you have a lot of images that you would like to test you could create a has_image? matcher for Capybara.
It is easy and this post explains it step-by-step: Adding a has_image? Matcher to Capybara
是的,但是这些 xpath 测试不会处理这样的事实:...
...都是相同的图像有效链接。
Yes, but these xpath tests won't deal with the fact that...
...are all the same valid link to the image.
这种语法对我有用并且更具可读性。
this syntax worked for me and is more readable.