2013-08-21 27 views
6

Nếu tôi có một kịch bản mà bắt đầu như thế này:kịch bản Kéo phác thảo (hoặc đọc thẻ) từ bên trong bước dưa chuột

@my-tag 

    Scenario Outline: 
    Admin user changes email 

    Given I register a random email address 

...

là nó có thể đọc hoặc là văn bản kịch bản phác thảo hoặc @my-tag trong định nghĩa từng bước? Ví dụ: trong bước I register a random email address, tôi muốn in thông tin gỡ lỗi nếu nó đang chạy trong một trường hợp nhất định hoặc giá trị thẻ.

+0

đẹp câu hỏi, nhờ –

Trả lời

20

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"] 
+1

chỉ để add: scenario.feature.source_tag_names sẽ chỉ trả lại ["@feature_tag"] –

+0

bạn đã tiết kiệm thời gian, cảm ơn bạn –

+0

'uninitialized constant Cucumber :: Ast'. Ast đã không được chấp nhận, gặp khó khăn khi theo dõi cách thay thế nó. –

Các vấn đề liên quan