2017-04-17 6 views
0

私はテストオートメーションにprotractor-cucumber-frameworkを使用しています。私は複数の機能ファイルを持っています。各フィーチャーファイルには複数のシナリオがあります。私は "cucumber-html-reporter"を使ってテスト実行のHTMLレポートを取得しています。このHTMLレポートは、機能の総数と実行されたシナリオの総数に関する詳細情報を提供します。だから、テスト実行後、私は実行された「フィーチャーファイルの総数」と「シナリオの総数」を知るようになります。JavaScriptテスト自動化プロジェクトからの機能とシナリオの合計数を取得するには

機能

  • 各機能ファイル内のシナリオの総数私のJavaScriptのテスト自動化で
    • 合計数を取得することが可能な任意のコマンドやプラグインはありますか?

    +0

    これをご覧ください - http://stackoverflow.com/questions/18004326/how-can-i-get-a-quick-count-of-the-number-of-scenarios-and-steps-大規模なキューブ – Grasshopper

    答えて

    1

    そのための多目的JSスクリプトは、タグなどをASTにガーキンを使用して機能ファイルを解析し、機能をカウントし、シナリオが含ま..:

    例:

    const glob = require('glob') 
    const Gherkin = require('gherkin') 
    const parser = new Gherkin.Parser() 
    
    const AST = glob 
         .sync('./specifications/**/*.feature') 
         .map(path => parser.parse(fs.readFileSync(path).toString())) 
    

    そこからASTオブジェクトをトラバースし、フィーチャ/シナリオの数や必要なその他の情報を抽出できます。

    1

    これは、プラグインなしで実現するのは非常に簡単です。

    フィーチャー名をキーに、シナリオ数を値として使用してオブジェクトを作成し、console.log()のいずれか、または後で表示するファイルに保存するのはなぜでしょうか?

    私は両方の方法(2.x構文と1.x構文を示しています。

    CucumberJS 2.xの構文

    let {defineSupportCode} = require('cucumber'), 
        counter = {}; 
    
    defineSupportCode(({registerHandler, Before}) => { 
    
        registerHandler('BeforeFeature', function (feature, callback) { 
         global.featureName = function() { 
          return feature.name; 
         }; 
         callback(); 
        }); 
    
        Before(function (scenario, callback){ 
         counter[featureName()] !== undefined ? counter[featureName()] += 1 : counter[featureName()] = 1; 
         callback(); 
        }); 
    
        registerHandler('AfterFeatures', function (feature, callback) { 
         console.log(JSON.stringify(counter)); 
         callback(); 
        }); 
    }); 
    

    CucumberJS 1.xの構文

    var counter = {}; 
    
    module.exports = function() { 
    
        this.BeforeFeature(function (feature, callback) { 
         global.featureName = function() { 
          return feature.name; 
         }; 
         callback(); 
        }); 
    
        this.Before(function (scenario, callback){ 
         counter[featureName()] !== undefined ? counter[featureName()] += 1 : counter[featureName()] = 1; 
         callback(); 
        }); 
    
        this.AfterFeatures(function (feature, callback) { 
         console.log(JSON.stringify(counter)); 
         callback(); 
        }); 
    }; 
    

    エクストラ

    この情報をファイルに保存したい場合は、そう後の段階でそれを見ることができるので、fs-eを使うことをお勧めしますxtraライブラリ。 console.log()の代わりに、これを使用する:

    fs = require('fs-extra'); 
    fs.writeFileSync("path/to/file.js","let suite = " + JSON.stringify(counter)); 
    

    注意してください、ファイルは、テストを実行した場所から作成されます。

    Given I am running from "frameworks/cucumberjs" 
    When I generate a file from "frameworks/cucumberjs/hooks/counter.js" with the fs library at "./file.js" 
    Then the file "frameworks/cucumberjs/file.js" should exist 
    
    Given I am running from "frameworks/cucumberjs" 
    When I generate a file from "frameworks/cucumberjs/features/support/hooks/counter.js" with the fs library at "./hello/file.js" 
    Then the file "frameworks/cucumberjs/hello/file.js" should exist 
    

    正しいディレクトリから実行していることを確認してください。特長の

    総数

    あなたにも機能の総数をしたい場合:

    console.log()の代わりに

    console.log(JSON.stringify(counter) + "\nFeature Count: " + Object.keys(counter).length) 
    

    そして、WriteFile関数の代わりに:

    fs.writeFileSync("path/to/file.js","let suite = " + JSON.stringify(counter) + ", featureCount = " + Object.keys(counter).length); 
    

    それぞれのフィーチャ名でソートされたシナリオ数を取得したので、作成したオブジェクト内のキーの量を指定すると、フィーチャの数がカウントされます。その構造から

    +0

    Faims - 詳細な説明をありがとうございました –

    関連する問題