Get variable in conf.js and use it through spec files

  • This is what I have in protractor.conf.js:

        let localNodeStorage = require('./helpers/localStorageNode');
    exports.config ={onPrepare: async () => {
      project: require('path').join(__dirname, './tsconfig.e2e.json')
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
    await browser.driver.get('');
    await browser.driver.findElement(by.css('#username')).sendKeys('user');
    await browser.driver.findElement(by.css('#password')).sendKeys('pass');
    await browser.driver.findElement(by.css('.btn-enter')).click();
    localNodeStorage.getValue("AuthenticationToken").then((token) => {
        let authToken= token;
    <p>Is there a way to access <code>authToken</code> in my login.ts class? And if so, how?</p>
    <p>I tried <code>params{}</code> but it won't work
    If I just get token from localStorage in login.ts I get authToken undefined.</p>

  • Hi if your params variable is token , then in onprepare call it as

    browser.token , instead of browser.parans.token

    Another way is to declare a global variable in config file and use it .

    In JavaScript it's straight forward as putting

    global token , in the top of conf.js and using it as token anywhere else

    You can also try out steps listed out in below stack overflow session:

    It include approachs like using environment variable ,getting full processedconfig and getting the params property from it , to name a few.

Log in to reply

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2