angularprotractore2e-testingangular9angular-e2e

Protractor Angular 9: browser.executeAsyncScript() - window.ng is undefined


I'm trying to reference one of my services while executing e2e tests. I need this to have possibility to enable/disable my feature toggles mocking mechanism, and could perform tests for both states on/off of some feature.

I've tried with:

const data = await browser.executeScript(function() {
  //@ts-ignore
  return window.ng.getInjector(window.getAllAngularRootElements()[0]).get('MyService')
})

but the problem is that window.ng is undefined, I got error message:

Failed: javascript error: Cannot read property 'getInjector' of undefined
(Session info: chrome=80.0.3987.122)
(Driver info: chromedriver=80.0.3987.16 (320f6526c1632ad4f205ebce69b99a062ed78647-refs/branch-heads/3987@{#185}),platform=Linux 4.19.121-linuxkit x86_64)

I receive the same error when using browser.executeAsyncScript()

Tests are running in docker


Solution

  • The issue was inside my own configuration.

    Tests were executed against different environment than I was expecting. This was environment in production mode and this is the reason that ng object was not available.