Cucumbers: select an item from a table to delete or add

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

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

I do not know what this definition of step is for.

I would like 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? Thanks.

@ Brad

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 "(.*)"$/' 


+4
source share
5 answers

After a little searching and minor refactoring, I managed to solve the problem.

I 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 also can not understand what RDoc is, but everything that seems to me looks out of order.

+2
source

I have a slightly outdated application that does not apply very well to useful link identifiers or even to classes (ie class = "deleteLink"). I need to find a link that has "delete" in href. Obviously, this is a tendency to make mistakes, but now it works. Here is the code for this.

 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 

This is a bit messy in xpath, but it is something that I could finally get to work. I am using Cucumber / Rspec / Capybara / Selenium to test my Java BTW application.

+2
source

I'm going to assume that this is part of the deletion that confuses you, as the other stuff is pretty standard (setting givens and the following links, etc.)

So what do you use as an abstraction of your browser? Webrat? Capybara? It seems that you have a β€œdelete” link, is it enough 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 that something like β€œnot to be seen” is probably supported out of the box with webrat / capybara generated steps.

Is this what you are looking for?

+1
source

I had the same problem, and I looked at the translation "I follow" abcd "" on click_link (), and found an optional method there. So I defined this:

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

and it worked ... Then I decided to make it more general, and not just "how to remove":

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

It works great. I tried it with "I follow" mylink "as get" and it worked, so part of the method seems quite flexible.

+1
source

Following Brad 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 
0
source

All Articles