如何在 Rails 3 中使用 Cucumber / Capybara 查找页面上的图像

发布于 2024-10-14 15:33:12 字数 1528 浏览 4 评论 0原文

我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

夏天碎花小短裙 2024-10-21 15:33:12

Nokogiri 的解决方案应该可以正常工作。唯一的问题是 Cabybara API 与 Webrat 不同,因此您必须使用 page.body,而不是 response.body

但还有一种更好的方法来使用 Cabybara 测试图像:

Then /^I should see the image "(.+)"$/ do |image|
    page.should have_xpath("//img[@src=\"/public/images/#{image}\"]")
end

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 use page.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
挖个坑埋了你 2024-10-21 15:33:12

你好,我不擅长使用 XPATH,但你可以尝试使用 CSS:

  if page.respond_to? :should
    page.should have_selector("img[src$='#{imagename}']")
  else
    assert page.has_selector?("img[src$='#{imagename}']")
  end

希望有帮助!
杰兰比

Hi I'm not good with XPATH but with CSS you can try :

  if page.respond_to? :should
    page.should have_selector("img[src$='#{imagename}']")
  else
    assert page.has_selector?("img[src$='#{imagename}']")
  end

wish helps !
jramby

吐个泡泡 2024-10-21 15:33:12

您可以通过这种方式进行测试,并且不依赖于路径:

Then /^I should see the image "(.+)"$/ do |image|
    page.should have_xpath("//img[contains(@src, \"#{image}\")]")
end

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
落日海湾 2024-10-21 15:33:12

页面.应该

现已弃用。改用

期望(页面).to

完整示例:

Then /^I should see the image "(.+)"$/ do |image|
    expect(page).to have_xpath("//img[contains(@src, \"#{image}\")]")
end

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
差↓一点笑了 2024-10-21 15:33:12

这有效吗?

page.should have_xpath('//img[@src="/public/images/foo.png"]')

Does this work?

page.should have_xpath('//img[@src="/public/images/foo.png"]')
记忆消瘦 2024-10-21 15:33:12

如果您有很多想要测试的图像,您可以创建一个 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

微暖i 2024-10-21 15:33:12

是的,但是这些 xpath 测试不会处理这样的事实:...

/public/images/foo.jpg
public/images/foo.jpg
http://mydomain.com/public/images/foo.jpg

...都是相同的图像有效链接。

Yes, but these xpath tests won't deal with the fact that...

/public/images/foo.jpg
public/images/foo.jpg
http://mydomain.com/public/images/foo.jpg

...are all the same valid link to the image.

_畞蕅 2024-10-21 15:33:12

这种语法对我有用并且更具可读性。

page.should have_css("img", :src => "/public/images/foo.png")

this syntax worked for me and is more readable.

page.should have_css("img", :src => "/public/images/foo.png")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文