Checking the contents of multiple html attributes with Cucumber and Capybara

by Michiel on February 22, 2011 · 0 comments

in Rails Development

Today I came across a test scenario where I wanted to check wether the style attribute on a div with id=”banner” would be filled with something.

In this application, we want to check for this because the user can manage a banner image in the admin and set the background of the div with it, but if the image is not set in the admin it should fall-back to an img tag.

For this we used capybara’s have_xpath to check for the div and the style attribute:

Then /^I should see the image just uploaded as an infographic$/ do
  visit root_path
  page.should have_xpath("//div[@id = 'banner' and contains(@style, 'background-image')]")
end

Do you have any other way of checking the attributes of html tags in a Cucumber test or are there other good ways to do this? What is your opinion? Otherwise I hope this will help you :)

Leave a Comment

Previous post:

Next post: