Capybara provides a useful method to check a link:
have_link
which as far as I can tell can be used like:
have_link("link_text", :href => "actual link")
However, I don't care about the link_text, rather I just want to check href
(as linking the test to the text is more brittle). If I only want to check the href
this would be a view test.
How do I use Capybara to check the href
without needing to check the text? Maybe i can use regex?
[edit] changed wording based on answer below
Capybara is a web-based test automation software that simulates scenarios for user stories and automates web application testing for behavior-driven software development. It is written in the Ruby programming language.
Capybara helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and comes with Rack::Test and Selenium support built in. WebKit is supported through an external gem.
To find a link based on just its href using capybara you could do
link = page.find(:css, 'a[href="actual link"]')
or if you're looking to assert that the element exists
page.assert_selector(:css, 'a[href="actual link"]')
or - if using RSpec
expect(page).to have_selector(:css, 'a[href="actual link"]')
Since have link by default searches for substrings in the link text you can also do
expect(page).to have_link(nil, href: 'actual link')
or
page.assert_selector(:link, nil, href: 'actual link')
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