Capybara - check for a data attribute by reference

I have a link with a specific attribute on my page. Using Rspec + Capybara, how can I check for 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') 
+7
source share
4 answers

I have the same query, but the quotes are different.

 find("a[data-method='delete']").click 

and works for me.

+12
source

It could be a Capybara problem that finds too many results. Perhaps change “find” to “everything” and see if you have an array of results that you can select. Good luck.

+1
source

This worked for me when searching for the title link (tooltip) attribute:

 Then(/^I should see tooltip "(.*?)" on link "(.*?)"$/) do |tip, link| within('#results') do link = find(:link, link) expect(link['title']).to eql(tip) end end 

NOTE. within is optional and useful to reduce the likelihood of ambiguity.

HTML Source Code:

 <a title="Owner: lfco Fire Department" href="/water_supplies/597df82566e76cfcae000018">Hydrant LFCO One</a> 
0
source

Here are a few 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 the page, but have access to view,

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

All Articles