Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How to use Capybara to test a link's href-value without caring about the text?

Tags:

capybara

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

like image 649
Drew Verlee Avatar asked Jul 13 '15 15:07

Drew Verlee


People also ask

What is Capybara testing?

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.

What is Capybara selenium?

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.


1 Answers

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') 
like image 74
Thomas Walpole Avatar answered Sep 18 '22 09:09

Thomas Walpole