Matching meta tags with Capybara 2

less than 1 minute read

As I updated my bundle, some capybara have_selector(…) matches started to fail. Here was the message :

expected to find xpath "//meta[@http-equiv='refresh']" but there were no matches. Also found "", which matched the selector but not all filters. (Capybara::ExpectationNotMet)

After some searching, I eventually understood that it was a modification in the behaviour of Capybara 2 that only matches elements in the html body, and not in the head anymore. If trying to match the title, stackoverflow suggests to use :

expect(response).to have_title('My page')

To match meta tags, I had to resort to the following :

meta_refresh_tags = Nokogiri::HTML(page.source).xpath("//meta[@http-equiv='refresh']")
expect(meta_refresh_tags).not_to(be_empty, 'could not find a meta refresh tag')

I could do with something nicer, but it’s ok for the moment.

I usually write about 15 minutes worth of reading per month. I won't transfer your email. No Spam, unsubscribe whenever you want.

As a gift for subscribing, you'll receive an illustrated mini-ebook "How to start a team coding dojo"!

Leave a comment