#241 ✓wontfix
Guillaume Godet-Bar

Patch for handling ajax requests to controller in Webrat Rails sessions

Reported by Guillaume Godet-Bar | May 8th, 2009 @ 12:35 PM

Whereas Webrat/Selenium is meant for testing javascripting, Ajax requests are still handy for tapping the controller while filling forms and avoiding page reloads. Typical examples include modifying the rails session or the cookies.

This patch adds an "ajax" method to Webrat. It can be used as follows:


ajax(http_method, url, data, headers=nil)

Example:


ajax :get, "/controller/model/id", {}

When the "ajax" method is used, the response.body variable holds the latest valid, non-empty body received by Webrat (i.e., provided by a classic http call). Therefore, submit links may be followed and the result of the form submission may be tested.

Comments and changes to this ticket

  • gaffo

    gaffo May 15th, 2009 @ 08:47 PM

    • State changed from “new” to “wontfix”

    Guillaume,
    I'm not sure what the benefit of this is. Could you provide a test code example for the scenario you are trying to accomplish? Currently for this type of test you would use Selenium mode to make sure this works with the ajax post, and webrat mode to make sure that your request worked with non-js. I'm going to mark this as invalid at the moment, but will gladly re-open it with a good example.

    Thanks for the input and don't take the "wontfix" as a putoff. We are completely open to discussion and re-opening this, it's just that 80% of people forget about tickets and it helps keep the Lighthouse clean to "close" things.

  • Guillaume Godet-Bar

    Guillaume Godet-Bar May 22nd, 2009 @ 03:36 AM

    You're right gaffo, I should have added a more comprehensive example.

    In fact, we needed this functionality for an application with some dense javascript parts, which could not be conveniently "non-js'ed". Most of these scripts send at some point ajax updates and requests, which end up modifying session variables, which are in turn used for updating or creating model instances.

    For instance, one of these modules is for creating sorts of html image maps, while on the edit page of a model (from a classic CRUD application). Whenever the user validates the map s/he added, an ajax request is sent to rails for storing the coordinates of the map in a session variable. The coordinates are appended until the user clicks create/update.

    One of our (simplified) cucumber examples runs as such:


    Scenario: Add an image map to the process
    Given the following post:

    {mkd-extraction-1e4abb1229005ec264765c232228b69e}

    And I add an image whatever.png associated to the post with name a test post And I am on the edit page of the post with name a test post When I add a map with coordinates 10, 40, 100, 200 And I add a map with coordinates 40, 60, 400, 800 And I press Update Then I should see Post was successfully updated. And I should see two image maps

    ... and the step for adding the map is defined as such:

    When /^I add a map with the coordinates "([^\"]*)" to the post "([^\"]*)"$/ do |coords, post_name|
      the_coords = coords.split(",").collect{|a_coord| a_coord.to_i}
      post = Post.find_by_name(post_name)
      ajax(:get, "/posts/save_map/#{post.id}", {:x => the_coords[0], :y => the_coords[1], :w => the_coords[2], :h => the_coords[3]}  )
    end
    

    Of course, this mechanism works because the scripts do not apply massive modifications to the DOM of the edit page (i.e., the "update" button is still there).

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

Attachments

Pages