4

I have the following table in an application I am developing using ruby on rails: alt text

I want to create a test in cucumber where I select a user from the table and delete it or edit it.

I don't know what is the step definition for that.

I'd like to be able to do something like:

Feature: User Manegement
         In order to manage users
         As an admin 
         I want to see a users list and change user properties

Background:
Given the following activated users exists
  | name         | email                    | 
  | Alice Hunter | alice.hunter@example.com |
  | Bob Hunter   | bob.hunter@example.com   |
And the following user records
  | name     | email                    | 
  | Jonh Doe | jonh.doe@example.com     |

    Scenario: I delete a user from the table
      Given I am logged in as admin
      When I follow "Administration"
      And I follow "User Management"
      And I delete "Alice Hunter"
      Then I should not see "Alice Hunter"`

Can anyone help? Thank you.

@brad

The error returned:

  wrong number of arguments (2 for 1) (ArgumentError)
  ./features/step_definitions/table_steps.rb:26:in `within'
  ./features/step_definitions/table_steps.rb:26:in `/^I delete "(.*)"$/'


Confusion
  • 16,256
  • 8
  • 46
  • 71
Tiago Veloso
  • 8,513
  • 17
  • 64
  • 85

5 Answers5

2

After some extensive searching and minor refactoring, I managed to solve the problem.

I have used the following step:

When /^as admin I (press|follow|check|uncheck|choose) "([^\"]*)" for (.*) whose (.*) is "([^\"]*)"$/ do |action, whatyouclick, class_name, var_name, value|
  unless var_name == "id" then
    id = eval("\"#{class_name}\".classify.constantize.find_by_#{var_name}(\"#{value}\").id.to_s")
  else
    id = value
  end
  within("tr[id=as_admin__#{class_name}-list-#{id}-row]") do
    case action
      when "press"
        click_button(whatyouclick)
      when "follow"
        click_link(whatyouclick)
      when "check"
        check(whatyouclick)
      when "uncheck"
        uncheck(whatyouclick)
      when "choose"
        uncheck(whatyouclick)
    end
  end
end

I am also insterested in webrat's RDoc, but everything I find seems out of order.

Tiago Veloso
  • 8,513
  • 17
  • 64
  • 85
2

I have a bit of a legacy app that isn't being nice with regard to useful link ids or even classes (i.e. class="deleteLink"). I have to find the link that has 'delete' in the href. Obviously this is error prone but it's working for now. Here's the code for that.

When /^I delete "([^"]*)"$/i do |value|
  page.evaluate_script('window.confirm = function() { return true; }') 
  within(:xpath, "//tr[.//*[contains(text(), '#{value}')]]") do
    find(:xpath, ".//a[contains(@href,'delete')]").click
  end
end

It's a little messy in the xpath, but it's what i could finally get to work. I'm using Cucumber/Rspec/Capybara/Selenium to test a Java app BTW.

Trever Shick
  • 1,734
  • 16
  • 17
1

I'm going to assume it's the deleting part that is messing you up as the other stuff is fairly standard (setting up givens and following links etc...)

So, what are you using as your browser abstraction? Webrat? Capybara? It appears as if you have a 'delete' link, is it sufficient to do something like this?

And /I delete "(.*)"/ do |person|
  # Use webrat or capybara to find row based on 'person' text... then find 'delete' link in row and click it
  # example (untested, pseudo code)
  within(:xpath, "//table/tr[contains(#{person})") do
    find('.deleteLink').click
  end
end

And I believe something like "should not see" is probably supported out of the box with generated webrat/capybara steps.

Is this what you're looking for?

brad
  • 31,987
  • 28
  • 102
  • 155
  • That is some what I am looking for. I am using webrat. Basically I want to be able to press the delete/edit/details link on a given line of the above table. Possibly abstractable for other tables of the same sort. Announcement, group, etc... Your pseudo code did not work for me. I will post the error above... – Tiago Veloso Nov 22 '10 at 19:29
  • sorry, the within syntax is for capybara... if you check out the webrat api, it will give you the proper syntax for scoping, unfortunately all i can find online right now is for gitrdoc which apparently doesn't exist anymore?? I'll keep looking and see if I can get you a link – brad Nov 22 '10 at 22:01
1

I had the same problem, and looking into the translation of 'I follow "abcd"' to click_link(), I found there's an optional :method. So I defined this:

When /^(?:|I )follow "([^"]*)" as delete$/ do |link|
  click_link(link, :method => :delete)
end

and that worked... Then I decided to make it more general, not just "as delete":

When /^(?:|I )follow "([^"]*)" as ([a-z]+)$/ do |link,method|
  click_link(link, :method => method.to_sym)
end

Works great. I tried it with 'I follow "mylink" as get' too, and that worked, so the method part seems to be suitably flexible.

Andy
  • 11
  • 1
0

Following on Brad's answer below I went with:

When /^I follow "([^"]*)" for "([^"]*)"$/ do |link, person|
  # Use capybara to find row based on 'person' text... no need for the additional 'find'
  # the '.,' sets the scope to the current node, that is the tr in question
  within(:xpath, "//table/tr[contains(.,'#{person}')]") do
    click_link(link)
  end
end
mongo296
  • 181
  • 2
  • 4