Authentication using API | Bondar Academy
Course: Playwright UI Testing with TypeScript
Module: Interacting with APIs
Instructor: Artem Bondar
Lesson Summary
Lesson Overview: In this lesson, we optimize the authentication process of our application by using an API call instead of a user interface. This change enhances performance by eliminating the need for manual username and password entry. Key Concepts: Authentication Optimization: Replacing UI-based authentication with API requests. User State Management: The authenticated state is saved in a user.json file, which includes: Cookies: Currently empty, as no cookies are used for authentication. Origins: Contains the URL and local storage with the JWT token. Access Token Retrieval: The access token is obtained via an API call and stored in the user.json file. Implementation Steps: Comment out the UI authentication code. Import necessary modules: import user from './user.json'; import { writeFileSync } from 'fs'; Update the user object with the new access token. Write changes back to user.json using: writeFileSync('user.json', JSON.stringify(user)); Store the access token in a process environment variable for reuse: process.env.ACCESS_TOKEN = accessToken; Configure global HTTP headers in playwright-config.ts to use the access token. Conclusion: This lesson demonstrates how to streamline the authentication process by leveraging API calls, improving code readability, and enhancing performance. All tests passed successfully after these modifications.