-1

While automating angular 4 app, i need to declare some global variables i.e. myVar = 'John'. I tried declaring it in protractor.conf but its not working. I already have seen Protractor set global variables but these solutions don't work for me. Is there any solution to this? I am using protractor version 5.4.2.

exports.config = {
//..
params:{
myVar = 'John'
},

//..
}
I already have used this as well but its not working

onPrepare() {
    require('ts-node').register({
      project: 'e2e/tsconfig.e2e.json'
    });
    //this global variable 
    global.myVar = 10000;
    
    browser.waitForAngularEnabled(false);
    browser.driver.manage().window().maximize();
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }

Thanks in Advance

Analyst
  • 751
  • 6
  • 15

3 Answers3

4

params is an object and therefore requires : to assign the value to myVar, not =.

exports.config = {
  params:{
    myVar:'John'
  },    
}

This is called in the following manner

console.log(browser.params.myVar)

The second method should work as it is. You just need to call it

onPrepare() {
    require('ts-node').register({
      project: 'e2e/tsconfig.e2e.json'
    });
    //this global variable 
    global.myVar = 10000;

    browser.waitForAngularEnabled(false);
    browser.driver.manage().window().maximize();
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }

Called with

console.log(myVar);
DublinDev
  • 2,318
  • 2
  • 8
  • 31
  • 1
    Thanks a lot, it worked. huh... I knew that and making the same mistake again & again.. Thanks for your quick reply. – Analyst Jan 07 '20 at 16:24
0

Also notice that when using 'global.blabla' or 'browser.params' with tests parallelization enabled (shardTestFiles: true) they will be read-only. This means if you will change these variables in one thread - other threads will be still having old value, since in protractor each test thread runs in separate nodejs process and they do not have shared memory.

Xotabu4
  • 3,063
  • 17
  • 29
-1

you are missing the tutorial:

params: {
   myVar: 500;
}

let a = browser.params.myVar;
console.log(a); // 500
Infern0
  • 2,565
  • 1
  • 8
  • 21