TestProject Forum

Obtaining bearer token from session storage for API testing

Hello fellow TestProject users!

I wanted to share how I accomplished something that was a bit of a struggle for me. The following is one way that you can deal with obtaining your bearer token for authentication prior to testing an endpoint. This is what has worked for me:

  1. Create steps to log into your web app as the user for whom you want to get the bearer token.
  2. Once successful login is accomplished, for the next test step, use the ‘Execute JavaScript’ action and add the following JavaScript return window.sessionStorage.key('1'). Output the result to a parameter.

This is saving the Key that is associated with the bearer token value, as seen below


  1. Add another step with the ‘Execute JavaScript’ action again, this time add the following JavaScript return window.sessionStorage.getItem('{<parameterFromPriorStep>}'). Output the result as a parameter.

This is saving the value associated to the key defined in step 2. This value includes the bearer token as well as other data we will need to parse through.

  1. Create a new step that uses action ‘Get substring in range’, and find the index start/finish values that will extract only the bearer token string from the entire key/value pair (for us it is start index = 1014 and end index = 1967).

This step is parsing through the value of the key and extracting ONLY the bearer token portion of the parameter

  1. Output result as a parameter called bearerToken (or whatever you want). This will be your bearer token value.
  2. Now create a step with the action ‘HTTP GET Request’, enter the endpoint value in the ‘Uri’ field, ‘200’ in the ‘ExpectedStatus’ field, and enter the following in the ‘Headers’ field Authorization=Bearer {bearerToken}.

This step is using the bearer token to authenticate to the endpoint and confirming a successful connection (200 status code)

At this point you should be able to authenticate to the endpoint successfully and get a 200 response code!