如何在量角器配置文件中动态设置 multiCapabilities [英] How can i set multiCapabilities dynamically in protractor config file

查看:51
本文介绍了如何在量角器配置文件中动态设置 multiCapabilities的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
}],

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

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.

推荐答案

我们可以使用 getMultiCapabilities() 自定义动态能力.

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;

定义一个函数来获取thread值.

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

在量角器 conf.js 中使用 getMultiCapabilities:

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

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

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

这篇关于如何在量角器配置文件中动态设置 multiCapabilities的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆