mark
mark

Reputation: 737

Cant set baseurl permanent in protractor

I run protractor like this

protractor  --params.env=q

q means QA. This protractor conf

  var CommonPageObject = require('./e2e/commonPageObject');


    suites: {
        login: './e2e/account/login/*.js'
},
 //for console params. example console command: protractor  --params.env=q
    params: {
            env: 'qa',//can be local, prod or qa default is qa for environment
    },
     onPrepare: function () {
            var commonPageObject = new CommonPageObject();
            commonPageObject.prepareVariables();


       }

this is common page

    this.prepareVariables = function () {console.log("xx",browser.params.env);
        var env = browser.params.env;
        this.setEnvironment(env);
        this.setBaseUrl(env);
    };
  //can be local, prod or qa. local = 2, qa = 0, prod=1
    this.setEnvironment = function (env) {
        if(env.includes("l")){
            environment = 2;
        }
        else{
            if(env.includes("p")){
                environment = 1;
            }
            else{// qa
                environment = 0;
            }
        }console.log("environment",environment);
    };

    //to get enviroment qa=0, prod=1, local=2 default=qa
    this.getEnvironment = function () {
        return environment;
    };

    this.setBaseUrl = function (env) {
        if(env.includes("q")){
            baseUrl = "http://xxxx.qa.xxx.com:8080";
        }
        else{
            if(env.includes("p")){
                baseUrl = "https://xxxxx.com";
            }
            else{
                baseUrl = "localhost:8080";
            }
        }console.log("baseUrl",baseUrl);
    };

    this.getBaseUrl = function () {console.log("getBaseUrl",baseUrl);
        return baseUrl;
    };

Output is thhat protractor --params.env=qwhen i run

  xx q
environment 0
baseUrl http://xxx.qa.xxxx.com:8080
Started
getBaseUrl undefined
FgetBaseUrl undefined
F

it becomes undefined when tests starts. Whhy is that? Why commonpage object page variable cant be kept in stack? SO,, should i call those methods for each test inbefore each? Or am i doing something wrong?

Upvotes: 1

Views: 248

Answers (1)

craig
craig

Reputation: 5016

Your scope of the commonPageObject exists only within the context of the onPrepare method. This does not transfer to the rest of the spec files. What you should do is tie your browserUrl to a global variable. Protractor has one built in: browser.baseUrl

this.setBaseUrl = function (env) {
    if(env.includes("q")){
        browser.baseUrl = "http://xxxx.qa.xxx.com:8080";
    }
    else{
        if(env.includes("p")){
            baseUrl = "https://xxxxx.com";
        }
        else{
            baseUrl = "localhost:8080";
        }
    }console.log("baseUrl",baseUrl);
};

this.getBaseUrl = function () {console.log("getBaseUrl",browser.baseUrl);
    return browser.baseUrl;
};

Once your browser.baseUrl is set, in your spec files, you can now do browser.get('/some/path'); which should resolve to browser.baseUrl + '/some/path'.

Upvotes: 2

Related Questions