Search code examples
protractoryargs

How to call global env variable for gulp protractor


This is gulpfile:

gulp.task(
    'protractor', function () {
        var configObj = {
            configFile: config.test + 'protractor.conf.js'
        };
        configObj['args'] =[];//to be able to add multiple parameters

        if (argv.suite) {
            configObj['args'].push (
                '--suite',
                argv.suite
            );
        }

        if (argv.env) {

            if(argv.env.includes("q")){//qa
                argv.baseUrl = "http://xx.qa.yy.com:8080";
            }
            else{//prod
                if(argv.env.includes("p")){
                    argv.baseUrl = "https://xx.yy.com";
                }
                else{//local
                    argv.baseUrl = "localhost:8080";
                }
            }

            configObj['args'] .push(
                '--baseUrl',
                argv.baseUrl
            );
        }
        return gulp.src([])
            .pipe(plumber({errorHandler: handleErrors}))
            .pipe(protractor(configObj))
            .on(
                'error', function () {
                    gutil.log('E2E Tests failed');
                    process.exit(1);
                }
            );
    }
);

so, in protractor test classes, i can get baseurl with that for exaple

 this.getBaseUrl = function () {
    return browser.baseUrl;
};

,

because i set here

            configObj['args'] .push(
                '--baseUrl',
                argv.baseUrl
            );

but i cant get env. it is set here

from console command

gulp protractor --env l --suite logout

i can see in consloeoutput of that

argv.env

but i cant call from protractor it. I tried browser.env but did not work. how can i do this?

also i use yargs

var argv = require('yargs')//setting default enviroment to qa for testing
    .default({ env : 'qa' })
    .argv;

Solution

  • There is no env global variable or command line option in protractor, You have to pass it as a paramater using params. You can do something like this-

     if (argv.params.env) {
    
            if(argv.params.env.includes("q")){//qa
                argv.baseUrl = "http://xx.qa.yy.com:8080";
            }
            else{//prod
                if(argv.params.env.includes("p")){
                    argv.baseUrl = "https://xx.yy.com";
                }
                else{//local
                    argv.baseUrl = "localhost:8080";
                }
            }
    

    Now from console command you can invoke it

    gulp protractor --params.env 'test' --suite logout
    

    You can also access it calling browser.params.env

    You can always set globals in protractor in config file:

    exports.config = {
    //other config options
    
    params: {
        env: 'qa'
    }
    
    };
    

    And pass values to the global variables through command line as-

    protractor conf.js --params.env 'dev'