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

How can i set multiCapabilities dynamically in protractor config file

我正在使用量角器 5.2.2。我需要在量角器配置中动态设置 multiCapabilities file.Currently 我已经硬编码并设置 multiCapabilities 如下所示。

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

我在 beforeLaunch function.So 中有一个名为 threads 的动态参数,具体取决于此参数的值,我必须动态设置 multiCapabilities 并且 BatchNo also.In 上面的代码我有 threads=2,所以我在 multiCapabilities 中有 2 个对象,BatchNo 设置为 1 和 2 respectively.If 我在 beforeLaunch 函数中有 threads=4,然后我必须在 multiCapabilities 中设置 4 个对象,BatchNo 应该分别设置为 1、2、3 和 4(我正在为所有线程使用 chrome 浏览器)。我怎样才能提前 this.Thanks。

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

首先创建一个函数来创建你自己的multiCapabilities数组

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

创建 returns 特定 multiCapabilities 对应于您的线程

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

最后用它来设置multiCapabilities:

multiCapabilities: myMultiCapabilities(threads)

我们可以使用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);   
    });
};

在量角器中使用getMultiCapabilities 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;
        });
    }
};

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