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

It turns out that there was another element with the text 'Radio' in the 'navigation' DIV, so that was causing the test to fail.

For future reference, identifying the 1st element explicitly behaved as expected and fails the test:

page.first('div#navigation a').text.should == 'Radio')
page.first('div#navigation a').text.should eq('Radio')
page.first('div#navigation a').text.should match('Radio')
page.first('div#navigation a').text.should be('Radio')

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.

Tags:

Capybara