黄瓜:从表中选择一个元素进行删除或添加

时间:2010-11-22 18:30:32

标签: ruby-on-rails cucumber webrat

我在使用ruby on rails开发的应用程序中有以下表格: alt text

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

我不知道该步骤的定义是什么。

我希望能够做到这样的事情:

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"`

有人可以帮忙吗? 谢谢。

@brad

返回错误:

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


5 个答案:

答案 0 :(得分:2)

经过一番广泛的搜索和轻微的重构,我设法解决了这个问题。

我使用了以下步骤:

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感兴趣,但我发现的一切似乎都没有问题。

答案 1 :(得分:2)

我有一些遗留应用程序,对于有用的链接ID甚至类(即class =“deleteLink”)都不是很好。我必须在href中找到“删除”的链接。显然这很容易出错,但它现在正在发挥作用。这是代码。

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应用程序BTW。

答案 2 :(得分:1)

我将假设它是删除部分,搞乱你,因为其他东西是相当标准的(设置赠品和以下链接等...)

那么,您使用什么作为浏览器抽象? Webrat?水豚?看起来好像你有一个'删除'链接,这样就可以了吗?

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步骤。

这是你要找的吗?

答案 3 :(得分:1)

我遇到了同样的问题,在查看'我关注'abcd“'到click_link()的翻译时,我发现有一个可选的方法。所以我定义了这个:

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

然后工作......然后我决定让它更通用,而不仅仅是“删除”:

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

效果很好。我尝试了'我跟随'mylink“也得到了',这样做有效,所以方法部分似乎是适当灵活的。

答案 4 :(得分:0)

继布拉德的答案之后,我选择了:

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