How to test if an img tag points to an existing image?
I'm using Cucumber and Capybara for BDD on a RoR application. I have the following feature
Feature: List profiles In order to know that the people behind the profiles are real people As a lurker I want to be able to find who is registed and see their profile picture Background: Profiles have been added to the database, some with picture attached Given some profiles exist And I am on the home page Scenario: Search by city, avatars should be there When I search for a city with profiles Then I should see some result profiles with avatar
The underlying Capybara steps file contains:
Then /^I should see some result profiles with avatar$/ do page.should have_css("#profile_search_results .profile img.avatar", :count => some_profiles) end
This step checks that the page contains
<div id="#profile_search_results> <img class="avatar" src="" />
BUT... I also want to check if the image exists (it is not a broken image).
How can I do this in my Capybara steps?
The way to check, without having to use Selenium or any other drivers, was to extract the src attribute from the img element (Thanks MrDanA), and check the status code after visiting that URL:
page.should have_css("#profile_search_results .profile img.avatar", :count => some_profiles) img = page.first(:css, "#profile_search_results .profile img.avatar") visit img[:src] page.status_code.should be 200