Paul Biggar
Paul Biggar

Reputation: 28739

How do I match a full link in capybara?

In Capybara+Rspec, I can check that a link is missing:

response.body.should_not have_link("link_content")

This is fine, but unfortunately the test fails for when "link_content" partially matches a link, such as "this_is_a_long_link_content". How can I change the test to make it pass in this case?

(That is, the matcher should not partially match, it should only fully match).

Upvotes: 1

Views: 1029

Answers (2)

Marek Příhoda
Marek Příhoda

Reputation: 11198

From the docs:

If all else fails, you can also use the page.html method to test against the raw HTML:

This works for me:

page.html.should match('>\s*Log in\s*</a>')
page.html.should_not match('>\s*link_content\s*</a>')

Note that the argument to match can a regular expression. That means that you can make the solution whitespace-agnostic by simply adding \s*.

Upvotes: 4

Niklas B.
Niklas B.

Reputation: 95298

You can also use the following workaround:

response.body.should_not have_xpath("//a[normalize-space(text())='link_content']")

This is whitespace-agnostic and therefore a little more flexible than the raw HTML approach.

Upvotes: 4

Related Questions