#79 ✓resolved
Josh Nichols

edge click_link fails (when used from cucumber)

Reported by Josh Nichols | December 1st, 2008 @ 10:52 PM

I started a new rails app to play around with cucumber and webrat, following the instructions at:

At the suggestion of a friend, I setup webrat as a plugin.

The project is on github at http://github.com/technicalpickl...

When I run something that uses click_link, it isn't found, even though it is definitely in the response. Here's the code snippet:


click_url 'Destroy'

The response body (including a few Destory links):


"<h1>Listing froobles</h1>\n\n<table>\n  <tr>\n    <th>Name</th>\n    <th>Color</th>\n    <th>Description</th>\n  </tr>\n\n\n  <tr>\n    <td>Frooble 0</td>\n    <td></td>\n    <td></td>\n    <td><a href="/froobles/1">Show</a></td>\n    <td><a href="/froobles/1/edit">Edit</a></td>\n    <td><a href="/froobles/1" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Destroy</a></td>\n  </tr>\n\n  <tr>\n    <td>Frooble 1</td>\n    <td></td>\n    <td></td>\n    <td><a href="/froobles/2">Show</a></td>\n    <td><a href="/froobles/2/edit">Edit</a></td>\n    <td><a href="/froobles/2" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Destroy</a></td>\n  </tr>\n\n  <tr>\n    <td>Frooble 2</td>\n    <td></td>\n    <td></td>\n    <td><a href="/froobles/3">Show</a></td>\n    <td><a href="/froobles/3/edit">Edit</a></td>\n    <td><a href="/froobles/3" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Destroy</a></td>\n  </tr>\n\n  <tr>\n    <td>Frooble 3</td>\n    <td></td>\n    <td></td>\n    <td><a href="/froobles/4">Show</a></td>\n    <td><a href="/froobles/4/edit">Edit</a></td>\n    <td><a href="/froobles/4" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Destroy</a></td>\n  </tr>\n\n</table>\n\n<br />\n\n<a href="/froobles/new">New frooble</a>\n"

The error looks like:


      Could not find link with text or title or id /Destroy/ (Webrat::NotFoundError)
      /Users/nichoj/Projects/cucumbler/vendor/plugins/webrat/lib/webrat/core/locators/locator.rb:14:in `locate!'
      /Users/nichoj/Projects/cucumbler/vendor/plugins/webrat/lib/webrat/core/locators/link_locator.rb:62:in `find_link'
      /Users/nichoj/Projects/cucumbler/vendor/plugins/webrat/lib/webrat/core/scope.rb:242:in `click_link'
      (eval):2:in `click_link'
      ./features/step_definitions/frooble_steps.rb:16:in ` /I delete the first frooble/'
      features/manage_froobles.feature:23:in `/I delete the first frooble/'

I'm told this happens using the GitHub gem as well. Reverting to using the rubyforge gem, it works fine.

Comments and changes to this ticket

  • Jeff Rafter

    Jeff Rafter December 1st, 2008 @ 11:24 PM

    I think that this has to do with the fact that on Rails 2.1.x response.body is returning ....

    but on Rails 2.2.2 it isn't. response.body is the contents of the (not even the body tag itself!). The parsers are happily letting this go by unnoticed (ah for draconian parse errors!). A quick patch is to modify the rails.rb response_body method
    def response_body
      "<body>" << response.body << "</body>"
    end
    
    

    This should work for both versions given that the XPath searches are not rooted but use ".//". Also, if your page wrapped everything in some kind of main

    in the body you wouldn't see this error (but the default scaffolding does not do this).
  • Bryan Helmkamp

    Bryan Helmkamp December 2nd, 2008 @ 12:08 AM

    Josh -- Thanks. I'll look into this a bit more and post what I find.

    I'm not aware of any change to Rails that would prevent it from include the full response body (including body and HTML tags) in the response.body. That would be very strange.

    Jeff -- Is this behavior you've observed somewhere?

  • Bryan Helmkamp

    Bryan Helmkamp December 2nd, 2008 @ 01:19 AM

    • State changed from “new” to “resolved”

    I figured this out. Rails' response.body is doing what it should, returning the whole response body, but the problem was the missing html tags and the lack of a single root node in that source.

    Previously, Webrat used the existence of an HTML tag to determine whether or not to parse the response as HTML or XML, and if it was parsed as XML, only the first node (in this case, the H1) would be left in the doc.

    I resolved this by having Webrat check the response Content-Type header when running in Rails mode. If it's got "xml" in it, it will parse as XML, otherwise it will default to parsing as HTML. This ensures the doc is wrapped in an HTML tag and then Webrat will "see" your link and form elements, etc.

    I verified by running this against your app.

    Cheers,

    -Bryan

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Ruby Acceptance Testing for Web applications.

Shared Ticket Bins

People watching this ticket

Tags

Pages