Capybara - check for the existence of a data attribute on a link

Viewed 10890

I have a link with a specific attribute on my page. Using Rspec + Capybara how can I check for the existence of this link?

<a href="#" id="text" data-content="This is a discrete bar chart.">Foo</a>

Does not work:

page.find(:css, 'a[data-content="This is a discrete bar chart."]')

Works:

page.find(:css, 'a#test')
4 Answers

Here are some different ways:

expect(page).to have_selector("a[href='#'][data-content='This is a discrete bar chart.']")

page.has_selector?("a[href='#'][data-content='This is a discrete bar chart.']")

page.find("a[href='#'][data-content='This is a discrete bar chart.']")  # returns the node found

If you don't have access to page but have access to rendered, then

expect(Capybara.string(rendered)).to have_selector("a[href='#'][data-content='This is a discrete bar chart.']")
Related