[Solved] Variable scope in protractor


Try changing var to let. This allows to access your version inside your specs.

describe('Nodeprojectpart2Component', () => {
    let version = '';
    beforeEach(() => {
        version = '1.0';
      });

    it('test', () => {
      console.log( 'version' + version);
    });

  });

Issue with your code – Your are retrieving the value of version inside an asynchronous/callback function. Now before your function executes, your console is executed and prints undefined. I am not sure why would you like to define code outside specs. But if you still want to, you shall have the retrieval logic right after you define it in desribe block, something like –

describe('Nodeprojectpart2Component', () => {
        let version = '';
        version = //logic to find the version here itself
        ....

1

solved Variable scope in protractor