0

In the wdio.conf.js file, I'm using the beforeTest section to set a JSON web token so that, later on in the test suites I no longer need to login into the web application.

If the token is hardcode, this action is working perfectly! Yet I would like to create a variable 'jwt' and assign it the value of the JSON web token. When I do this, an error is given "jwt is not defined".

At the top of wdio.conf.js I write the following code const jwt = 'eyJ0eXAiOiJKV1QiLCJhb...'

In the before action I write the following code

   console.log('before exe ' + jwt)
   browser.execute(() => localStorage.setItem('usertoken', jwt))
   console.log('after exe ' + jwt)

The console log's are showing the token, so the variable is working inside of the beforeTesting action. Yet I'm getting the error '[0-0] Error in "BeforeTest Hook" javascript error: jwt is not defined'

  • My understanding is that, jwt is declared and used at nodejs context while the execute api executes the script in browser context. so jwt is undefined at browser level. Maybe you could try to pass the jwt as an input to the execute script? – Raju Feb 07 '21 at 04:17

1 Answers1

1

The browser doesn't know the jwt variable. You should pass this value as the argument of the function as stated in the docs: https://webdriver.io/docs/api/browser/execute.html.

browser.execute((browser_jwt) => localStorage.setItem('usertoken', browser_jwt), jwt)
bguiz
  • 27,371
  • 47
  • 154
  • 243
tjmortie
  • 26
  • 1