Search code examples
ruby-on-railscucumberwebrat

cucumber webrat click on second delete link


I've got a scenario which I want to select the 'Delete' link on the page for the second item in the table. I've been looking at click_link_within or using within but it is not working correctly.

  Scenario: User deletes a staff member from the listing
    Given a staff exists with name: "Michael"
    And a staff exists with name: "Joe"
    And I am on the listing staff index page
    When I follow "Delete" for row containing "Joe"
    Then I should be on the listing staff index page
    And I should not see "Joe"
    And I should see "Michael"

Here is the step I'm trying to use:

When /^I follow "([^"]*)" for row containing "([^"]*)"$/ do |button, text|
  click_link_within "//*[.//td[contains(.,#{text})]]", button
end

And here is the listing code in haml:

%h1 Staff Listing 

%table
    %th Username
    %th Surname
    %th First Name

    - @staff.each do |staff|
        %tr
            %td= staff.username
            %td= staff.surname
            %td= staff.firstname
            %td= link_to "Edit", edit_staff_path(staff)
            %td= link_to "Delete", staff, :method => :delete

Solution

  • For generating correct delete links rails uses javascript. So you get a DELETE request instead of GET which a normal link would produce. Since webrat doesn't handle javascript there is no way this can work. Check out Capybara as alternative to webrat or webrat with selenium.