0

When adding a global variable in the onPrepare method within the Protractor config, Typescript provides error "Cannot find name '____'" when attempt to utilize in test file.

Below is how I am defining the global variable in my Protractor config.

protractorConfig.js

onPrepare: function () {
  global.logger = log4js.getLogger( 'log' );
},

Below is how I am utilizing the global variable.

homepageTests.js

  it( '1@tests homepage', function () {
       logger.info( 'password for application: ' + pswd );
  } );

In reference to the following SO post, setting global variables in the onPrepare method is how Protractor library does this, so it should be valid. Protractor set global variables

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
JTAN
  • 411
  • 2
  • 5
  • 15

1 Answers1

1

This error is a TypeScript compilation time error that occurs because TS does not know about globals and the fact that something was added there. In order to fix that you need to add a declaration for globals somehow or just use assertions.

it('1@tests homepage', function () {
   (global as any).logger.info('password for application: ' + pswd);
});

Of course any can be replace with something more useful.

Another option is to create globals.d.ts near to tsconfig.json used for e2e tests and put this line there:

declare const logger: any; // Again, 'any' may be replaced with real type

This way you can access logger as you wish

it('1@tests homepage', function () {
   logger.info('password for application: ' + pswd);
});

You may find some other suitable options in this SO question

Shlang
  • 2,495
  • 16
  • 24