Cucumber学习笔记3

上一篇 / 下一篇  2013-08-25 12:12:18

Data Tables:
Sometimes steps in a scenario need to describe data that doesn’t easily fit on
a single line of Given, When, or Then. Gherkin allows us to place these details in
a table right underneath a step. Data tables give you a way to extend a Gherkin
step beyond a single line to include a larger piece of data.

Given a User "Michael Jackson" born on August 29, 1958
And a User "Elvis" born on January 8, 1935
And a User "John Lennon" born on October 9, 1940

--->
Given these Users:
| name | date of birth |
| Michael Jackson | August 29, 1958 |
| Elvis | January 8, 1935 |
| John Lennon | October 9, 1940 |

Given /^these Users:$/ do |table|
@table= table.raw  
#[["name", "date of birth"], ["Michael Jackson", "August 29, 1958"], ["Elvis", "January 8, 1935"], ["John Lennon", "October 9, 1940"]]

@table_hash = table.hashes
#[{"name"=>"Michael Jackson","date of birth"=>"August 29, 1958"},{"name"=>"Elvis","date of birth"=>"August 29, 1958"},{"name"=>"John Lennon","date of birth"=>"October 9, 1940"}]

user1name=@table[1][0]
user2name=@table[2][0]
user1birth=@table[1][1]
user2name=@table[2][1]
……

end

Scenario Outline

Scenario Outline: Withdraw fixed amount
Given I have <Balance> in my account
When I choose to withdraw the fixed amount of <Withdrawal>
Then I should <Outcome>
And the balance of my account should be <Remaining>

Examples:
| Balance | Withdrawal | Remaining | Outcome |
| $500 | $50 | $450 | receive $50 cash |
| $500 | $100 | $400 | receive $100 cash |
| $500 | $200 | $300 | receive $200 cash |
| $100 | $200 | $100 | see an error message |

Multiple Tables of Examples

Scenario Outline: Withdraw fixed amount
Given I have <Balance> in my account
When I choose to withdraw the fixed amount of <Withdrawal>
Then I should <Outcome>
And the balance of my account should be <Remaining>

Examples: Successful withdrawal
| Balance | Withdrawal | Outcome | Remaining |
| $500 | $50 | receive $50 cash | $450 |
| $500 | $100 | receive $100 cash | $400 |

Examples: Attempt to withdraw too much
| Balance | Withdrawal | Outcome | Remaining |
| $100 | $200 | see an error message | $100 |
| $0 | $50 | see an error message | $0 |


Nesting Steps

Feature:login
Scenario:success login
Given I am on main page
When I input right user and pass
And I press login
Then I login successful

Given /I am on main page/ do
 print "go to main page" 
end
When /I input right user and pass/ do
print "input user and pass"
end
And /I press login/ do
print "press login"
end
Then /I login successful/ do
end

Feature:clazz management
Scenario:view clazz page
Given I have already login
When I press clazz button
Then I will go to clazz page

In 2rd Scenario,we can nesting the login step :
Given /I have already login/ do
steps %{
And I am on main page
And I input right user and pass
And I press login
}
end
When /I press clazz button/ do
#pending
end
Then /I will go to clazz page/ do
#pending
end

 When Cucumber runs this high-level step, it will delegate to each of the lower-level steps.

The %{ ... } construct is just a way to tell Ruby that you have a string going
across multiple lines.
1 step to nesting:

…… do
steps "And I am on main page"
end

Arguments and Nested Steps:

If the low-level steps you are nesting within a new high-level step take arguments,you’ll  want to be able to capture those arguments in the high-level step and pass them to the low-level steps. Since the steps method just takes a string, you can use Ruby’s string interpolation to pass in the arguments:

Given /^an activated customer (\w+) exists$/ do |name|
steps %{
Given I create a customer with login #{name}
And I register the customer with login #{name}
And I activate the customer with login #{name}
}
end

But,passing a data table won't work!


Tags

If you want to tag all the scenarios in a feature at once, just tag the Feature
element at the top, and all the scenarios will inherit the tag. You can still tag
individual scenarios as well.

@nightly @slow
Feature: Nightly Reports
@widgets
Scenario: Generate overnight widgets report

to run a Scenario with tag:
$ cucumber --tags @tag1,@tag2



TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-05-15  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 32038
  • 日志数: 22
  • 建立时间: 2013-08-19
  • 更新时间: 2014-04-01

RSS订阅

Open Toolbar