Ruby 鲁比:我怎样才能在黄瓜上重复这些步骤?
我想在名单上加100个名字。我正在使用葫芦,所以我有.feature文件:Ruby 鲁比:我怎样才能在黄瓜上重复这些步骤?,ruby,list,cucumber,calabash,calabash-android,Ruby,List,Cucumber,Calabash,Calabash Android,我想在名单上加100个名字。我正在使用葫芦,所以我有.feature文件: When I fill the item field with "3" And press the button Adicionar And I repeat the previous 2 steps 100 times My.rb文件: ... When(/^I repeat the previous (\d+) steps (\d+) times$/) do |steps, times|
When I fill the item field with "3"
And press the button Adicionar
And I repeat the previous 2 steps 100 times
My.rb文件:
...
When(/^I repeat the previous (\d+) steps (\d+) times$/) do |steps, times|
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
如何实现这个.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|
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
然后,您可以使用嵌套步骤在.rb文件中编写步骤定义,如下所示:
...
When(/^I repeat the previous (\d+) steps (\d+) times$/) do |steps, times|
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”填充名称,则可以按照以下方式编写组合步骤:
Given I add following names in the list:
|Ram|
|Abdul|
|Scot|
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
假设我用“3”填充项目字段100次
然后您可以编写步骤定义,如下所示:
Given I add following names in the list:
|Ram|
|Abdul|
|Scot|
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
为什么要这样做,而不是做一个循环100次的步骤,填写项目字段并按下adicionar按钮?这比让cucumber做一些从来没有人做过的事情要容易多了。嗨,凯尔!你能帮我举个例子吗?你可以用这个方法。