-1

I want to add 100 names in the list. I'm using Calabash, so I have the .feature file:

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

My .rb file:

...

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

How can I implement this .rb file? The last thing I tried, I got the error:

Undefined dynamic step: "2" (Cucumber::UndefinedDynamicStep)
R. Gadeev
  • 188
  • 3
  • 12
gopp
  • 23
  • 6
  • 1
    Why do that and not instead make a step that loops 100 times filling in the item field and pressing the adicionar button? It'd be easier to do than making cucumber do something that it was never made to do. – KyleFairns May 06 '17 at 16:18
  • Hi, Kyle! Could you help me with an exemple, please? – gopp May 06 '17 at 18:41
  • You can use the [`times`](https://ruby-doc.org/core-2.4.0/Integer.html#method-i-times) method. – orde May 07 '17 at 21:57

2 Answers2

0

Below is a quick hello world example for google (I don't have your code, so can't properly do an example for your site).

The When is what we are interested in really.

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

Doing a for loop, like in the When above, could also have a maximum set by a new captured integer:

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

This would mean that you don't have to dynamically set up capturing of previous steps (which is doable, I'm sure, but would be a giant amount of effort for what you seem to be wanting to do here).

KyleFairns
  • 2,947
  • 1
  • 15
  • 35
0

You have a list of names then you can pass it using the data table and you can write your a combined step as following:

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

and then you can write the step definitions using nested steps as following in .rb file:

 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

If you simply want to fill the names with same name "3" then you can write your combined step as following:

Given I fill the item field with "3" 100 times

And then you can write the steps definition as following:

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