2017-09-04 14 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