Ruby:如何在黄瓜上重复步骤



我想在列表中添加100个名称。我正在使用calabash,所以我有.feature文件:

    When I fill the item field with "3"
    And press the button Adicionar
    And I repeat the previous 2 steps 100 times

我的.rb文件:

...
When(/^I repeat the previous (d+) steps (d+) times$/) do |steps, times|

如何实现此.RB文件?我尝试的最后一件事,我发现了错误:

Undefined dynamic step: "2" (Cucumber::UndefinedDynamicStep)

以下是Google的快速Hello World示例(我没有您的代码,因此无法正确为您的网站做示例(。

When是我们真正感兴趣的。

Given(/^I navigate to "([^"]*)"$/) do |url|
  $driver.navigate.to url
end
When(/^I search for "([^"]*)"$/) do |search_term|
  # Loop through 100 times
  100.times do
    # Clear any text present
    $driver.find_element({css: 'input[name="q"]'}).clear
    # Type in the request
    $driver.find_element({css: 'input[name="q"]'}).send_keys(search_term)
    # Fire the request with the return key
    $driver.find_element({css: 'input[name="q"]'}).send_keys(:return)
    # Give time for the process to complete before a reset (This could also go first)
    sleep 1
  end
end
Then(/^I (?:should|must) see some results$/) do
  wait = Selenium::WebDriver::Wait.new(:timeout => 10)
  wait.until { $driver.find_element({css: 'h3.r'}) }
end

做一个for循环,例如上面的When中,也可以通过新的捕获整数设置:

When(/^I search for "([^"]*)" (d+) times?$/) do |search_term, times|
  # Loop through an amount of times
  times.to_i.times do
    $driver.find_element({css: 'input[name="q"]'}).clear
    $driver.find_element({css: 'input[name="q"]'}).send_keys(search_term)
    $driver.find_element({css: 'input[name="q"]'}).send_keys(:return)
    sleep 1
  end
end

这意味着您不必动态地捕获以前的步骤(我敢肯定,这是可行的,但对于您似乎想在这里做的事情来说,这是巨大的努力(。

您有一个名称列表,然后您可以使用数据表传递它,并且可以编写一个组合步骤,如以下内容:

Given I add following names in the list:
 |Ram|
 |Abdul|
 |Scot|

,然后您可以使用嵌套步骤编写步骤定义,如.rb文件中的以下内容:

 Given(/^I add following names in the list:$/) do |table|
    data = table.raw
   data.each do |row|
    steps %{
     When I fill the item field with "#{row[0]}"
     And press the button Adicionar
    }
end
end
When(/^I fill the item field with "([^"]*)"$/) do |arg1|
 pending # Write code to enter the the item
end
When(/^press the button Adicionar$/) do
  pending # Write code to click the Adicionar button
end

如果您只想填写同名名称" 3",则可以写下您的组合步骤如下:

鉴于我用" 3" 100次

填充项目字段

,然后您可以按以下方式编写步骤定义:

Given(/^I fill the item field with "([^"]*)" (d+) times$/) do |name, loop_count|
  loop_count.to_i.times do 
        steps %{
     When I fill the item field with "#{name}"
     And press the button Adicionar
    }
  end 
end
When(/^I fill the item field with "([^"]*)"$/) do |arg1|
     pending # Write code to enter the the item
end
When(/^press the button Adicionar$/) do
      pending # Write code to click the Adicionar button
end

相关内容

  • 没有找到相关文章