如何在量角器配置文件中动态设置multiCapabilities

时间:2018-10-12 05:10:32

标签: protractor cucumberjs

我正在使用量角器5.2.2。我需要在量角器配置文件中动态设置multiCapabilities。目前,我已进行了硬编码并设置了multiCapabilities,如下所示。

multiCapabilities: [
{
browserName: 'chrome',
BatchNo:1
},
{
browserName: 'chrome',
BatchNo:2
}],

我在beforeLaunch函数中有一个称为线程的动态参数。因此,根据此参数的值,我必须动态设置multiCapabilities,而BatchNo也要设置。在上面的代码中,我有thread = 2,所以我在multiCapabilities中有2个对象和BatchNo分别设置为1和2。如果我在beforeLaunch函数中线程数为4,则必须在multiCapabilities中设置4个对象,并且BatchNo应该分别设置为1,2,3和4(我正在对所有线程使用chrome浏览器)。我该怎么做。谢谢您。

2 个答案:

答案 0 :(得分:0)

multiCapabilities应该得到Array<string>。您可以创建一个变量,该变量将具有一个函数,该函数返回与您的条件相对应的特定数组。 例如:

首先创建一个函数,该函数创建自己的multiCapabilities数组

function createArray(threads) {
  const array = [];
  for (let batch = 1; batch <= threads; batch++) {
    array.push({
      browserName: 'chrome',
      BatchNo: batch
    });
  }
  return array;
}

创建变量,该变量返回与您的线程相对应的特定multiCapabilities

const myMultiCapabilities = (threads) => {
  return createArray(threads);
}

最后将其用于设置multiCapabilities

multiCapabilities: myMultiCapabilities(threads)

答案 1 :(得分:0)

我们可以使用getMultiCapabilities()来定制动态功能。

 /**
   * 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;

定义一个函数以获得thread值。

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);   
    });
};

在量角器conf.js中使用getMultiCapabilities

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;
        });
    }
};

有关beforeLaunch问题的进一步问题的相关代码:

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);
                }
            });
        });
    });
}