代码之家  ›  专栏  ›  技术社区  ›  Devleena

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

  •  0
  • Devleena  · 技术社区  · 6 年前

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

    我在beforeLaunch函数中有一个动态参数threads。因此,根据这个参数的值,我必须动态设置multiCapabilities和BatchNo。在上面的代码中,我有threads=2,所以multiCapabilities和BatchNo中的2个对象分别设为1和2。如果我在beforeLaunch函数中有threads=4,然后我必须在multiCapabilities中设置4个对象,BatchNo应该分别设置为1、2、3和4(我对所有线程都使用chrome浏览器)。我该怎么做呢?提前谢谢。

    2 回复  |  直到 6 年前
        1
  •  0
  •   yong    6 年前

    我们可以用 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);
                    }
                });
            });
        });
    }
    
        2
  •  0
  •   Oleksii    6 年前

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

    首先创建一个函数,创建自己的函数 多容量 数组

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

    多容量 对应于你的线程

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

    多容量

    multiCapabilities: myMultiCapabilities(threads)