问题
I am using protractor 5.2.2. I have a requirement of setting multiCapabilities dynamically in protractor config file.Currently i have hard coded and set multiCapabilities as given below.
multiCapabilities: [
{
browserName: 'chrome',
BatchNo:1
},
{
browserName: 'chrome',
BatchNo:2
}],
i have a dynamic parameter called threads in beforeLaunch function.So depending on the value of this parameter, i have to set multiCapabilities dynamically and the BatchNo also.In above code i have threads=2, so i have 2 objects in multiCapabilities and BatchNo set as 1 and 2 respectively.If i have threads=4 in beforeLaunch function, then i have to set 4 objects in multiCapabilities and BatchNo should set as 1,2,3 and 4 respectively(i am using chrome browser for all threads).How can i do this.Thanks in advance.
回答1:
We can use getMultiCapabilities() to customize dynamical capabilites.
/**
* If you need to resolve multiCapabilities asynchronously (i.e. wait for
* server/proxy, set firefox profile, etc), you can specify a function here
* which will return either `multiCapabilities` or a promise to
* `multiCapabilities`.
*
* If this returns a promise, it is resolved immediately after
* `beforeLaunch` is run, and before any driver is set up. If this is
* specified, both capabilities and multiCapabilities will be ignored.
*/
getMultiCapabilities?: any;
Define a function to get thread
value.
let getThreadValue = function () {
return new Promise(function (resolve, reject) {
request = new Request("sql to query thread value", function (err, rowCount, rows) {
if (err) {
reject(err);
}
else {
resolve('put thread value at here');
}
});
connection.execSql(request);
});
};
Use getMultiCapabilities
in protractor conf.js:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['./test.js'],
// If getMultiCapabilities is specified,
// both capabilities and multiCapabilities will be ignored
getMultiCapabilities: function () {
return getThreadValue().then(function (thread) {
let multiCapabilities = [];
for (index = 1; index <= thread; index++) {
multiCapabilities.push({
browserName: 'chrome',
BatchNo: index
})
}
return multiCapabilities;
});
}
};
Related code for further question about beforeLaunch
issue:
let getThreadValue = function () {
return new Promise(function (resolve, reject) {
connection.on('connect', function (err) {
if (err) {
reject(err);
}
else {
request = new Request("select * from location", function (err, rowCount, rows) {
if (err) {
reject(err);
} else {
resolve(Math.ceil(rowCount / 3));
}
});
connection.execSql(request);
}
});
});
};
beforeLaunch: function() {
return getThreadValue().then(function (thread) {
console.log('thread: ' + thread);
return new Promise(function(resolve, reject){
connection.on('connect', function (err) {
if (err) {
reject(err);
} else {
request = new Request("EXEC [usp_GetPostDetails] 1514," + thread, function (err, rowCount, rows) {
if (err) {
reject(err);
} else {
console.log("done");
resolve('done');
}
});
connection.execSql(request);
}
});
});
});
}
回答2:
multiCapabilities
should get Array<string>
. You could create a variable that will have a function that returns specific array corresponding to your condition.
For example:
firstly create a function that create your own multiCapabilities
array
function createArray(threads) {
const array = [];
for (let batch = 1; batch <= threads; batch++) {
array.push({
browserName: 'chrome',
BatchNo: batch
});
}
return array;
}
create variable that returns specific multiCapabilities
corresponding to your threads
const myMultiCapabilities = (threads) => {
return createArray(threads);
}
and finally use it for setting multiCapabilities
:
multiCapabilities: myMultiCapabilities(threads)
来源:https://stackoverflow.com/questions/52772646/how-can-i-set-multicapabilities-dynamically-in-protractor-config-file