Clicking a link

posted: February 27th, 2007

There have been times when integration testing a rails site that I’ve wanted to click a link on a page without caring about it’s route or url.

So let’s say the following exists on a page in my app:

1 <html>
2   <a href="http://meresheep.com/users/4;resend_activation_email">
3       resend your activation email
4   </a>
5 </html>

In my integration test, I can now do things like this:

1 def test_non_activated_user
2   s = new_session_as :duff
3   s.click_link("resend your activation email")
4   s.assert_template("invitation_sent")
5 end

To get this functionality, I added the following method to my integration testing DSL:

1 module IntegrationDsl
2 
3   def click_link(link_text)
4     assert_select("a", link_text, "Trying to click a link named '#{link_text}' that did not exist") do | links |
5       get_via_redirect links.first.attributes['href']
6     end
7   end
8 
9 end

The test fails if the link doesn’t exist on the current page.