Bạn không thể truy cập thông tin đó trực tiếp từ trong định nghĩa bước. Nếu bạn cần thông tin, bạn sẽ phải nắm bắt nó trong một móc trước.
Dưa chuột v3 +
Sau đây trước khi móc sẽ nắm bắt được tên truyện, kịch bản tên/phác thảo và danh sách các thẻ. Lưu ý rằng giải pháp này là dành cho Cucumber v3.0 +. Đối với các phiên bản trước đó, hãy xem phần cuối của câu trả lời.
Before do |scenario|
# Feature name
@feature_name = scenario.feature.name
# Scenario name
@scenario_name = scenario.name
# Tags (as an array)
@scenario_tags = scenario.source_tag_names
end
Như một ví dụ, các tập tin tính năng:
@feature_tag
Feature: Feature description
@regular_scenario_tag
Scenario: Scenario description
Given scenario details
@outline_tag
Scenario Outline: Outline description
Given scenario details
Examples:
|num_1 | num_2 | result |
| 1 | 1 | 2 |
Với bước định nghĩa là:
Given /scenario details/ do
p @feature_name
p @scenario_name
p @scenario_tags
end
sẽ cho kết quả:
"Feature description"
"Scenario description"
["@feature_tag", "@regular_scenario_tag"]
"Feature description"
"Outline description, Examples (#1)"
["@feature_tag", "@outline_tag"]
Sau đó, bạn có thể kiểm tra @scenario_name hoặc @scenario_tags cho logic điều kiện của bạn.
Dưa chuột v2
Đối với dưa chuột v2, móc cần thiết là một phức tạp hơn:
Before do |scenario|
# Feature name
case scenario
when Cucumber::Ast::Scenario
@feature_name = scenario.feature.name
when Cucumber::Ast::OutlineTable::ExampleRow
@feature_name = scenario.scenario_outline.feature.name
end
# Scenario name
case scenario
when Cucumber::Ast::Scenario
@scenario_name = scenario.name
when Cucumber::Ast::OutlineTable::ExampleRow
@scenario_name = scenario.scenario_outline.name
end
# Tags (as an array)
@scenario_tags = scenario.source_tag_names
end
Đầu ra là hơi khác nhau:
"Feature description"
"Scenario description"
["@regular_scenario_tag", "@feature_tag"]
"Feature description"
"Outline description"
["@outline_tag", "@feature_tag"]
đẹp câu hỏi, nhờ –