我们有 Protractor-Cucumber 框架作为 e2e 测试。到目前为止,我们使用conf 文件并在conf json 中静态提及环境详细信息。我想使用 protractor_conf 文件传递用户定义的参数,如下所示。
protractor protractor_conf.js -DbrowserName=chrome -DexecPlatform=(native/sauce)
并且想要在conf.js 中获取此参数并替换到功能部分下。我无法在网上获得正确的详细信息,因此任何帮助/建议将不胜感激。
最佳答案
您可以在 conf.js 文件中添加参数,然后从命令行传递 args。
这是一个例子。
// The params object will be passed directly to the Protractor instance,
// and can be accessed from your test as browser.params. It is an arbitrary
// object and can contain anything you may need in your test.
// This can be changed via the command line as:
// --params.environment.Browser "Firefox"
params: {
environment: {
browser: 'Chrome',
execPlatform: 'sauce',
password: 'default'
}
}
现在我们可以从命令行传递 arugs
protractor conf.js --parameters.environment.browser= Firefox --parameters.environment.execPlatform=sauce --parameters.environment.password=password123
接下来,您还可以在规范文件中引用这些参数。
describe('describe some test', function() {
it('describe some step', function() {
$('.password').sendKeys(browser.params.login.password);
});
});
设置参数有很多优点。
- 如果我们知道将在多个规范文件中使用相同的值(即登录电子邮件和密码),则参数非常适合消除不必要的重复。
- 能够在运行时更改参数值,可以更轻松地使用不同的数据运行相同的测试。
- 提高安全性 - 在规范文件中硬编码密码并不是一个好方法。参数使您能够将它们排除在外,并在运行时提供它们。
关于node.js - 如何使用 Protractor conf传递命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52822861/