Feature: User login
Background: There is a user with the following login detail:
| email | password|
| my@example.com| test |
Scenario: Login succeed
Given the user login with the following detail:
| email | password|
| my@example.com| test |
Then the user should login succeed
Scenario: Login failed
Given the user login with the following detail:
| email | password |
| my@example.com| wrongpassword |
Then the user should login failed 实际上,上面的这段代码就是使用cucumber的DSL描述的测试场景,几乎就是遵循了一定格式的英语,即使看不懂代码的产品经理、业务分析师也能够通过此文档和开发人员顺畅地交流。用Cucumber把一个需求的不同场景描述出来,也是从不同角度阐述了这个需求的业务价值。__Cucumber的目标就是书写可执行的,能够表述业务价值文档。__ 与之类似的框架还有Concordian,JBehave等。
Given /^the user login with the following detail:$/ do |detail|
#omitting code…
end 这个step definition会匹配下面这个step:
Given the user login with the following detail:
| email | password|
| my@example.com| test |
class PageWithLogin
def url
#omitting code…
end
def login email, password
#omitting code…
end
end
class PageWithLoginResult
def login_succeed?
#omitting code…
end
end
Step定义
Given /^the user login with the following detail:$/ do |detail|
on_page_with :login do |page|
visit page.url
page.login(detail["email"], detail["password"])
end
end
Given /^the user should login succeed$/ do |detail|
on_page_with :login_result do |page|
page.login_succeed?.should == true
end
end