我需要在不同的上下文中使用配置文件中的不同baseUrl
运行量角器测试。我不想为每种情况使用单独的配置文件,因为这更难维护。相反,我想将基本 url 作为命令行参数传入。 这是我到目前为止尝试过的:
量角器.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
}
但这也不起作用,因为在生成配置时浏览器实例似乎不可用。
看起来我可以使用标准节点process.argv
来访问所有命令行参数,但这似乎违背了量角器的精神。
我做我需要做的事情的最佳方式是什么?
似乎这已经是可能的,但这方面的文档参差不齐。 但是,查看代码,量角器确实支持许多看似未记录的命令行参数。
因此,运行这样的事情将起作用:
protractor --baseUrl='http://some.server.com' my.conf.js
另一种选择是使用gruntfile.js并让它调用量角器配置文件。
//gruntfile.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());
}
}
};
使用默认网址 http://localhost:6034 运行
grunt protractor
使用任何其他网址运行
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 针对您在多功能数组中定义的每个功能运行。getProcessingConfig 返回您定义的配置,并添加当前功能。由于该方法返回一个承诺,因此我使用 async/await 以提高可读性。
这样,您可以运行多个功能,每个功能不同的主机不同。
Base url 应该声明为 baseUrl:",config.ts
我正在使用黄瓜钩子,下面的代码被添加到钩子文件中,以根据环境传递所需的网址
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"