在运行时预先添加/附加到黄瓜方案名称/功能名称或标签

时间:2017-09-04 10:15:46

标签: ruby cucumber

当为不同的视口桌面/移动设备运行黄瓜套件时,我想在运行时将ENV [' VIEWPORT']值添加到功能名称或方案名称中,以便在HTML中看到报告方案失败的视口。我正在生成一个统一的HTML报告,我合并了所有视口报告。每个场景都可以基于上面的ENV标志在任何平台上运行,因此不需要按视口标记场景

1 个答案:

答案 0 :(得分:1)

您需要为自定义格式实现一些自定义格式化程序。

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

Pretty Formatter

# features/support/viewport_aware/pretty_formatter.rb

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

HTML Formatter

# 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 Formatter:

[
  {
    "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>>",