在Angular Protractor测试中,可以使用spawn
方法来运行包含需要传入参数的外部脚本。以下是一个示例解决方法:
首先,确保安装了child_process
模块:
npm install child_process --save-dev
创建一个外部脚本文件,例如externalScript.js
,该脚本接受一个参数并输出结果:
// externalScript.js
const arg = process.argv[2];
console.log(`Received argument: ${arg}`);
console.log('Script executed successfully!');
在Protractor测试中,使用spawn
方法来运行外部脚本并传入参数:
// spec.js
const { spawn } = require('child_process');
describe('Protractor Test', function() {
it('should run external script with parameter', function() {
const argValue = 'parameter';
const scriptPath = 'path/to/externalScript.js';
const externalScriptProcess = spawn('node', [scriptPath, argValue]);
externalScriptProcess.stdout.on('data', function(data) {
console.log('External script output:', data.toString());
});
externalScriptProcess.stderr.on('data', function(data) {
console.error('External script error:', data.toString());
});
externalScriptProcess.on('close', function(code) {
console.log('External script exited with code:', code);
});
});
});
在上面的示例中,我们使用spawn
方法以node
命令运行外部脚本,并传递参数argValue
。然后,我们监听外部脚本的输出和错误,并在脚本退出时输出退出代码。
注意:确保将path/to/externalScript.js
替换为实际的外部脚本路径。