Read more

Capybara will not find links without an href attribute

Arne Hartherz
September 23, 2014Software engineer at makandra GmbH

Capybara will fail to find <a> tags that are missing an href attribute. This will probably happen to you every now and then on JavaScript-heavy applications.

Illustration money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
Read more Show archive.org snapshot

An example would be an AngularJS application where the following HTML actually works. [1]

<a ng-click="hello()">Hello</a>

Capybara will fail to find that link, even though looking it up via the DOM shows it:

>> find_link("Hello")
Capybara::ElementNotFound: Unable to find link "Hello"

>> find("a").text
=> "Hello"

To make find_link and click_link work, simply add href="#" to your element:

    <a href="#" ng-click="hello()">Hello</a>
>> find_link("Hello")
=> #<Capybara::Element tag="a">

[1] Note that while it does work in your application (links are usually styled correctly, and Angular will take care of the click event), the HTML spec Show archive.org snapshot defines hyperlinks as anchor tags (<a>) that have an href attribute. Without an href they are just link placeholders. So Capybara at least adheres to the HTML spec Show archive.org snapshot . :)

Posted by Arne Hartherz to makandra dev (2014-09-23 11:08)