Jamis Charles
Jamis Charles

Reputation: 6039

How do I do an xpath regex search in my Cucumber / Capybara step definition (Rails 3)?

I have a Scenario that validates that the image I just uploaded exists on the next page.

This below works great, except that I have a variable folder structure based on the listing_id. How can I use regex here to match image to only the filename instead of the entire url?

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

Upvotes: 9

Views: 4335

Answers (2)

Ryan
Ryan

Reputation: 185

If you have the option then using a css selector is more concise in this case:

page.should have_selector( :css, "a[href$='#{image}']")

Note the '$' on then end of 'href$' to denote matching against the end of the string.

Upvotes: 0

Dylan Markow
Dylan Markow

Reputation: 124419

Try page.should have_xpath("//img[contains(@src, \"#{image}\")]"), which will match any img whose src contains your filename.

Upvotes: 15

Related Questions