Search code examples
protractorcucumberjs

How can i set multiCapabilities dynamically in protractor config file


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.


Solution

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