Forum Discussion

MaksZin's avatar
MaksZin
Occasional Contributor
12 months ago

Authenticate with API request or reuse existing authentication state in TestComplete

Hello, 

 

I am looking for a way in TestComplete to authenticate with API request or reuse authenticated state. I want to test authentication (login) only once and avoid going through login page in all other tests. This could speed up test execution significantly (currently authentication step takes 4-6 sec). 

 

I know that TestCompete allows sending HTTP requests Sending and Receiving HTTP Requests From Script Tests | TestComplete Documentation (smartbear.com).However, I am not sure how to implement this with API login, store and reuse authenticated browser state. 

I was not able to find related solutions in the docs and community answers. 

 

I know that this approach is recommended when using other tools like Playwright  and  Cypress . I would appreciate it if you could guide me on what would be the best way to implement reusing authenticated  session in multiple tests.

 

Some context. I do not have much experience with test automation, but I know that our app uses Bearer token and cookie with token sent in headers when user is authenticated. We use Auth0 for authentication and I was able to implement login test in Postman using Auth0 API and user credentials. We have multiple user roles, so, ideally, we would like to store authentication session separately for each user role. We have a subset of tests that can run at the same time with the same account, without affecting each other. Currently most of our tests implemented as keyword tests but we use some scripts too. We plan to run our tests in multiple environments (QA, UAT, production).

 

Example test (currently):

1. (2s) Launch browser 

2. (3s) Open login (UI form)

3. (2s) Submit  login form

4. (2s) Open Dashboard page

5. (0.5s) Verify dashboard page opened (text is displaying and menu is visible).

6. (2s) Logout

TOTAL time: 11.5s.

 

 

Example test (desired):

1.(0.1s) Login with API or load authentication state from previous test. 

2. (2s) Launch browser 

3. (0.1s) Apply authentication state in browser

4. (2s) Open Dashboard page

5. (0.5s) Verify dashboard page opened (text is displaying and menu is visible).

6. (2s) Logout.

TOTAL time: 6.7s.

 

Thank you,

Maksim

 

 

5 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If you have used Postman, then you should know what HTTP request methods to use and also what HTTP headers to set, so that the server can respond to your request.

     

    The same can be done using the methods shown in Sending and Receiving HTTP Requests From Script Tests.

     

    Remember, sessions do expire. So reusing the previous session may not work.

    • MaksZin's avatar
      MaksZin
      Occasional Contributor

      Hi rraghvani . Thank you for your reply. 

       

      I understand how to send HTTP requests in TC, I mentioned it in my post, but I think my questions remain unanswered.

      1. How can I set header and cookie in browser once I logged in with API request? 

      2. Does TC provide built-in tools for that? 

      3. Does TC provide built-in tools to reuse authentication state similar to Cypress and Playwright? (see links in initial post). 

      4. Example code would be helpful.

       

       

      Thanks  

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    The application that you write will be the Client, and you will send HTTP request/response, to the Server. The Client in this instance will act like a "web browser", which is similar to Postman.

     

    I very doubt you can set headers and cookies in MS Edge and Chrome externally. I'm not aware of TC having build-in tools for this. To reuse the authenication, save the authentication key, and then pass this key with its appropriate header, to the next request.

    • MaksZin's avatar
      MaksZin
      Occasional Contributor

      rraghvani wrote:

      The application that you write will be the Client, and you will send HTTP request/response, to the Server. The Client in this instance will act like a "web browser", which is similar to Postman.

       


      Not sure I understand this part. Where this should be done? What is the step number in my example test?

      Does that mean I should login via API, then send HTTP request (what will be request?) with required headers and then launch browser and go through all actions required in my test? 

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    The coding that you write, using TC, will be acting as the Client - it will be like developing a web browser application.

     

    A short simple answer, it will be too difficult to do