Search code examples
javascriptnode.jsprotractorreport

Using Protractor parameter value in plugin config in conf.js


I would like my protractor-screenshoter-plugin to create report directory with spec name as directory name. Spec name is to be passed as parameter when running Protractor:
protractor --specs my_spec.js conf.js

After calling the above command I would like my test to be run and report to be created in directory my_spec.js (or my_spec).
Plugin's configuration is included in conf.js:

plugins: [{
    package: 'protractor-screenshoter-plugin',
    screenshotOnExpect: 'failure+success',
    screenshotOnSpec: 'failure',
    withLogs: false,
    htmlReport: true,
    screenshotPath: '',//I would like to put the --specs parameter value here
    writeReportFreq: 'end',
    clearFoldersBeforeTest: true
}]

Any ideas how to do it? How to access Protractor's '--specs' parameter value in conf.js?


Solution

  • You can access all the CLI arguments passed when triggering Protractor using process.argv which will give you an array containing all the arguments

    See the below extract from the Nodejs documentation on process.argv

    The process.argv property returns an array containing the command line arguments passed when the Node.js process was launched. The first element will be process.execPath. See process.argv0 if access to the original value of argv[0] is needed. The second element will be the path to the JavaScript file being executed. The remaining elements will be any additional command line arguments.

    When you execute protractor conf.js --spec demo2.js

    The statement console.log(process.argv) in conf.js will output something like this below

    [ 'C:\\Program Files\\nodejs\\node.exe',
      'C:\\Users\\aditya\\AppData\\Roaming\\npm\\node_modules\\protractor\\bin\\protractor',
      'conf.js',
      '--specs',
      'demo2.js' ]
    

    Then build your logic to extract the value you need. In this specific case, to get the specs value (without extension, so that filename conflict does not occur), the below function would help

    function getSpecsFromCLIArg() {
        for (i = 0; i < process.argv.length; i++) {
            if (process.argv[i] === '--specs') {
                var specFile = process.argv[i + 1];
                return specFile.substr(0, specFile.indexOf('.'));
            }
        }
    }
    console.log(getSpecsFromCLIArg())
    
    plugins: [{
        package: 'protractor-screenshoter-plugin',
        screenshotOnExpect: 'failure+success',
        screenshotOnSpec: 'failure',
        withLogs: false,
        htmlReport: true,
        screenshotPath: getSpecsFromCLIArg(),
        writeReportFreq: 'end',
        clearFoldersBeforeTest: true
    }]