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

我在使用ruby on rails开发的应用程序中有下表: 替代文字

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

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

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

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


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

我使用了以下步骤:

 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中找到“删除”的链接。 显然这很容易出错,但它现在正在发挥作用。 这是代码。

 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。

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

那么,你使用什么作为浏览器抽象? 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步骤。

这是你在找什么?

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

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

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

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

效果很好。 我尝试了’我跟随’mylink“as 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