testhook.feature
Feature: Test how to use hooks
In order to learn how to use hooks
as a learner
I want to do some practice
@hotel
Scenario:search a hotel
Given I visit to hotel launch page
Scenario:search a flight
Given I visit to flight launch page复制代码 在该feature文件中定义了两个scenario,第一个scenario有一个tag: @hotel(为了说明tagged hooks的用法),另一个没加tag。两个scenario都只有一个步骤,在这我们就不纠结这些了,只为演示hooks的的用法。 testhook.rbGiven /^I visit to hotel launch page$/ do
puts "The current page is the hotel launch page."
end
Given /^I visit to flight launch page$/ do
puts "The current page is the flight launch page."
end复制代码该文件是对feature文件的具体实现,在这两个Given中我都只是简单的打印出了一句话。
hooks.rbBefore do
#Before("@hotel") do
#tagged hooks
puts "This is before hook."
end
After do
#After("@hotel") do #tagged hooks
puts "This is after hook."
end复制代码
在该hooks文件中,我定义了before和after钩子,也都只是简单的输出一句话。注:第2,7行分别为tagged钩子,当执行它们时,只有跟它们匹配的tag的scenario前/后会执行此hook。