2017-09-04 15 views
0

當爲不同視口桌面/移動設備運行黃瓜套件時,我希望在運行時將ENV ['VIEWPORT']值預先添加到功能名稱或場景名稱所以我可以在HTML報告中看到場景失敗的視口。我正在生成統一的HTML報告,我合併了所有視口報告。每個場景都可以在任何基於上述ENV標誌的平臺上運行,因此無需通過視口標記場景在運行時預先添加/附加到黃瓜場景名稱/功能名稱或標籤

回答

0

您需要爲自定義格式實現一些自定義格式器。

https://github.com/cucumber/cucumber/wiki/Custom-Formatters

然後,類似於https://github.com/moredip/timestamped-scenarios,假設黃瓜2.4.0版本和打捆被使用:

格式化
# features/support/viewport_aware/adds_viewport.rb 
require 'rubygems' 

module ViewportAware 
    module AddsViewport 
    def self.formatter_with_viewport(formatter_class) 
     Class.new(formatter_class){ include AddsViewport } 
    end 

    def scenario_name(keyword, name, file_colon_line, source_indent) 
     super(keyword, with_viewport(name), file_colon_line, source_indent) 
    end 

    def feature_name(keyword, name) 
     super(with_viewport(keyword), name) 
    end 

    # for json formatter 
    def on_finished_testing(event) 
     @feature_hashes.each do |it| 
     it[:name] = with_viewport(it[:name]) 
     (it[:elements] || []).each do |el| 
      el[:name] = with_viewport(el[:name]) 
     end 
     end 
     super 
    end 

    private 

    def with_viewport(str) 
     "#{str} <<#{ENV['VIEWPORT']}>>" 
    end 
    end 
end 

漂亮格式化

# features/support/viewport_aware/pretty_formatter.rb 

require 'cucumber/formatter/pretty' 
module ViewportAware 
    PrettyFormatter = AddsViewport.formatter_with_viewport(Cucumber::Formatter::Pretty) 
end 

HTML格式化

# features/support/viewport_aware/html_formatter.rb 

require 'cucumber/formatter/html' 
module ViewportAware 
    HtmlFormatter = AddsViewport.formatter_with_viewport(Cucumber::Formatter::Html) 
end 

JSON格式化

# features/support/viewport_aware/json_formatter.rb 

require 'cucumber/formatter/json' 
module ViewportAware 
    JsonFormatter = AddsViewport.formatter_with_viewport(Cucumber::Formatter::Json) 
end 

然後運行:

VIEWPORT=mobile bundle exec cucumber -f ViewportAware::PrettyFormatter 

VIEWPORT=mobile bundle exec cucumber -f ViewportAware::HtmlFormatter 

VIEWPORT=mobile bundle exec cucumber -f ViewportAware::JsonFormatter 

漂亮格式的結果:

Feature <<mobile>>: Create a boat 
    In order to avoid mistakes when finding my boat 
    As a sailor of my boat 
    I want to be told the details of my boat 

    Scenario: Creating a new boat <<mobile>>        
    <skimmed> 

或JSON格式化:

[ 
    { 
    "uri": "features/add.feature", 
    "id": "create-a-boat", 
    "keyword": "Feature", 
    "name": "Create a boat <<mobile>>", 
    "description": " In order to avoid mistakes when finding my boat\n As a sailor of my boat\n I want to be told the details of my boat", 
    "line": 1, 
    "elements": [ 
     { 
     "id": "create-a-boat;creating-a-new-boat", 
     "keyword": "Scenario", 
     "name": "Creating a new boat <<mobile>>", 
+0

感謝@eugene。太精彩了。我測試了這一點,它適用於HTML和漂亮,但不適用於JSON。任何方式我可以得到這與JSON格式化工作?我試着添加一個像上面這樣的常量,但看起來像json格式化程序有點不同 – Rahul

+0

我已經擴展了答案。 請注意刪除的「初始化」。 這個補丁變得混亂(更像是一個監管猴子補丁),但它仍然有效。 你也可以重構它。 –

+0

非常感謝@eugene。按預期工作。 – Rahul