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!
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With