Ruby on rails Cucumber:从表中选择要删除或添加的元素

Ruby on rails Cucumber:从表中选择要删除或添加的元素,ruby-on-rails,cucumber,webrat,Ruby On Rails,Cucumber,Webrat,我正在使用ruby on rails开发的应用程序中有下表: 我想在cucumber中创建一个测试,从表中选择一个用户并将其删除或编辑 我不知道这个步骤的定义是什么 When /^I delete "([^"]*)"$/i do |value| page.evaluate_script('window.confirm = function() { return true; }') within(:xpath, "//tr[.//*[contains(text(), '#{value}

我正在使用ruby on rails开发的应用程序中有下表:

我想在cucumber中创建一个测试,从表中选择一个用户并将其删除或编辑

我不知道这个步骤的定义是什么

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
我希望能够做到以下几点:

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"`
有人能帮忙吗? 多谢各位

@布拉德

返回的错误为:

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



我假设是删除部分把你搞得一团糟,因为其他东西是相当标准的(设置givens和以下链接等)

那么,您使用什么作为浏览器抽象?网络鼠?水豚?似乎您有一个“删除”链接,这样做是否足够

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
我相信,像“不应该看到”这样的东西可能会在生成webrat/capybara步骤时得到开箱即用的支持


这就是你要找的吗?

经过广泛的搜索和较小的重构,我设法解决了这个问题

我使用了以下步骤:

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

我也参与了webrat的RDoc,但我发现的一切似乎都有问题。

我有一个遗留应用程序,在有用的链接ID甚至类(即class=“deleteLink”)方面都不太好。我必须在href中找到包含“delete”的链接。很明显,这很容易出错,但它目前正在工作。这是代码

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

xpath中有点混乱,但这是我最终可以使用的。我正在使用Cucumber/Rspec/Capybara/Selenium来测试一个Java应用程序。顺便说一句,我也遇到了同样的问题,通过查看“我跟随”abcd”的翻译来单击链接(),我发现有一个可选的:方法。所以我定义了这个:

When /^(?:|I )follow "([^"]*)" as delete$/ do |link|
  click_link(link, :method => :delete)
end
这就成功了。。。然后我决定让它更一般,而不仅仅是“删除”:


效果很好。我也用“我跟随“mylink”作为get”进行了尝试,效果很好,因此方法部分似乎具有适当的灵活性。

以下是Brad的回答:

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

你能将你的功能/场景添加到帖子中吗?添加了一个我想做的示例。基本上我想模拟点击Alice Hunter旁边的删除链接。这就是我想要的。我正在使用webrat。基本上,我希望能够按下上表给定行上的delete/edit/details链接。可能对同类的其他表进行抽象。公告、团体等。。。你的伪代码对我不起作用。我将发布上面的错误…对不起,内语法是水豚。。。如果您查看webrat api,它将为您提供正确的范围界定语法,不幸的是,我现在能在网上找到的只是gitrdoc,它显然已经不存在了??我会继续找,看看能不能给你一个链接