Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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')
like image 231
bumpkin Avatar asked Dec 28 '13 22:12

bumpkin


2 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.']")
like image 107
konyak Avatar answered Sep 24 '22 19:09

konyak


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

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

and works for me.

like image 28
Armando Avatar answered Sep 24 '22 19:09

Armando