Protractor 如何参数化量角器配置文件的baseUrl属性
我需要在配置文件中使用不同的Protractor 如何参数化量角器配置文件的baseUrl属性,protractor,Protractor,我需要在配置文件中使用不同的baseUrls在不同的上下文中运行量角器测试。我不想为每种情况使用单独的配置文件,因为这更难维护。相反,我希望将基本url作为命令行参数传入。以下是我迄今为止所尝试的: progrator.conf.js: exports.config = { onPrepare : { ... exports.config.baseUrl = browser.params.baseUrl; ... } } 以及调用量角器: protractor
baseUrl
s在不同的上下文中运行量角器测试。我不想为每种情况使用单独的配置文件,因为这更难维护。相反,我希望将基本url作为命令行参数传入。以下是我迄今为止所尝试的:
progrator.conf.js:
exports.config = {
onPrepare : {
...
exports.config.baseUrl = browser.params.baseUrl;
...
}
}
以及调用量角器:
protractor protractor.conf.js --params.baseUrl 'http://some.server.com'
这不起作用,因为在调用onPrepare
之前,似乎已经配置了browser
实例
同样,我也尝试过:
exports.config = {
baseUrl : browser.params.baseUrl
}
但这也不起作用,因为在生成配置时,浏览器实例似乎不可用
看起来我可以使用标准nodeprocess.argv
访问所有命令行参数,但这似乎违背了量角器的精神
对于我来说,做我需要做的事情的最佳方式是什么?似乎这已经是可能的了,但在这方面的文档不完整,然而,量角器确实支持许多看似未记录的命令行参数 因此,运行类似这样的操作将起作用:
protractor --baseUrl='http://some.server.com' my.conf.js
另一个选项是使用gruntfile.js并让它调用量角器配置文件 //Grunfile.js
module.exports = function (grunt) {
grunt.registerTask("default", "", function () {
});
//Configure main project settings
grunt.initConfig({
//Basic settings and infor about our plugins
pkg: grunt.file.readJSON('package.json'),
//Name of plugin
cssmin: {
},
protractor: {
options: {
configFile: "conf.js", // Default config file
keepAlive: true, // If false, the grunt process stops when the test fails.
noColor: false, // If true, protractor will not use colors in its output.
args: {
baseUrl: grunt.option('baseUrl') || 'http://localhost:6034/'
}
},
your_target: { // Grunt requires at least one target to run so you can simply put 'all: {}' here too.
options: {
configFile: "conf.js", // Target-specific config file
args: {
baseUrl: grunt.option('baseUrl') || 'http://localhost:63634/'
}
}
},
},
//uglify
uglify: {
}
});
//Load the plugin
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-protractor-runner');
//Do the Task
grunt.registerTask('default', ['cssmin']);
};
量角器配置文件:conf.js
exports.config = {
directConnect: true,
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
args: ['--no-sandbox']
}
},
chromeOnly: true,
// Framework to use. Jasmine is recommended.
framework: 'jasmine',
// Spec patterns are relative to the current working directory when
// protractor is called.
specs: ['specs/*/*_spec.js'],
suites : {
abcIdentity : 'specs/abcIdentity/*_spec.js' //picks up all the _spec.js files
},
params: {
UserName: 'abc@test.com',
Password: '123'
},
// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000,
includeStackTrace: true
},
onPrepare: function () {
browser.driver.manage().window().maximize();
if (process.env.TEAMCITY_VERSION) {
var jasmineReporters = require('jasmine-reporters');
jasmine.getEnv().addReporter(new jasmineReporters.TeamCityReporter());
}
}
};
//使用默认url运行
//与任何其他url一起运行
grunt protractor --baseUrl:"http://dev.abc.com/"
我知道,老家伙。但如果有人仍在寻找一种基于功能定义url的方法(我必须这样做,因为Ionic 5将在浏览器中的端口8100上运行,但在应用程序中-不可更改-在端口80上没有端口声明,我使用Appium) 在功能声明中添加baseUrl参数
{
browserName: 'chrome',
baseUrl: 'http://localhost:8100' //not required but as example
}
{
...
app: 'path to app.apk',
baseUrl: 'http://localhost'
...
}
然后按如下方式配置onPrepare方法
async onPrepare() {
const config = await browser.getProcessedConfig();
if(config.capabilities.hasOwnProperty('baseUrl')) {
browser.baseUrl = config.capabilities.baseUrl;
}
}
OnPrepare为您在multiCapabilities阵列中定义的每个功能运行。getProcessedConfig返回您定义的配置,并添加当前功能。由于该方法返回一个承诺,因此我使用async/await以提高可读性
通过这种方式,您可以运行多个功能,每个功能都有不同的主机。基本url应在
config.ts
我正在使用cucumber钩子,下面的代码添加到钩子文件中,以根据环境传递所需的url
if(browser.params.baseUrl==="QA"){
console.log("Hello QA")
await browser.get("https://www.google.com");
} else {
console.log("Hi Dev")
await browser.get("https://www.gmail.com");
}
使用量角器命令运行测试
protractor --params.baseUrl 'QA' typeScript/config/config.js --cucumberOpts.tags="@CucumberScenario"
谢谢分享。如果你发现它是相关的,你应该做一个拉请求,把它们添加到文档中。是的,我会考虑的。对于任何其他人,如果你需要访问它的代码< >准备< /COD>块,如OP所要求的,可以通过下面的代码执行,并使用CONT.BaseL.感谢@ AndrewEisenberg!量角器~4.0.9的当前语法似乎是:
dragrator--baseUrlhttp://some.server.com/ my.conf.js
。我使用--baseUrl='etc'时出错。
protractor --params.baseUrl 'QA' typeScript/config/config.js --cucumberOpts.tags="@CucumberScenario"