conf.js 在插件配置中使用 Protractor 参数值

Using Protractor parameter value in plugin config in conf.js

我希望我的 protractor-screenshoter-plugin 以规范名称作为目录名称创建报告目录。当 运行ning Protractor:
时,规范名称将作为参数传递 量角器 --specs my_spec.js conf.js

调用上述命令后,我希望我的测试是 运行 并在目录 my_spec.js(或 my_spec)中创建报告。
插件的配置包含在 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
}]

有什么办法吗?如何在 conf.js?

中访问量角器的“--specs”参数值

您可以使用 process.argv 访问触发 Protractor 时传递的所有 CLI 参数,这将为您提供一个包含所有参数的数组

请参阅 process.argv

上 Nodejs 文档的以下摘录

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.

当你执行protractor conf.js --spec demo2.js

conf.js中的语句console.log(process.argv)将输出如下内容

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

然后构建您的逻辑以提取您需要的值。在这种特定情况下,要获取 specs 值(不带扩展名,因此不会发生文件名冲突),以下函数将有助于

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
}]