Capybara: how to check the TEXT value of an element with xpath and css

capybara

Trying to make this test to fail without success.

I've got this HTML:

<div id="navigation">
 <ul>
  <li>
   <a href="/url">TV</a>

And then I was trying to identify the text of the A element and make it fail for now.

I've used all the following expressions with Xpath but all of them keep passing even though I'm using a different text for the comparison :S

page.should have_xpath("//div[@id='navigation']//a", :content => 'Radio')
page.should have_xpath("//div[@id='navigation']//a", :text => 'Radio')
page.should have_xpath("//div[@id='navigation']//a[contains(string(),'Radio')]")
page.should have_xpath("//div[@id='navigation']//a[contains(text(),'Radio')]")
page.should have_xpath("//div[@id='navigation']//a[contains(.,'Radio')]")

Any idea how I could identify the text of an specific HTML element with capybara?
and… is it possible to achieve the same with CSS?

Cheers!

Best Answer

As of RSpec 2.11 (July 2012), the preferred way is to use the expect syntax:

expect(page).to have_css("#navigation a", text: "Radio")

You can configure RSpec to only accept this new syntax to keep your codebase consistent.

Related Topic