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

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


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


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


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