How do I find an image on a page with Cucumber / Capybara in Rails 3

前端 未结 8 642
后悔当初
后悔当初 2020-12-24 02:19

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.

相关标签:
8条回答
  • 2020-12-24 02:52

    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
    
    0 讨论(0)
  • 2020-12-24 02:52

    this syntax worked for me and is more readable.

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

    0 讨论(0)
  • 2020-12-24 02:54

    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
    
    0 讨论(0)
  • 2020-12-24 03:09

    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
    
    0 讨论(0)
  • 2020-12-24 03:10

    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

    0 讨论(0)
  • 2020-12-24 03:11

    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.

    0 讨论(0)
提交回复
热议问题