Environment Variables | Bondar Academy
Course: Playwright UI Testing with TypeScript
Module: Advanced Tricks and Techniques
Instructor: Artem Bondar
Lesson Summary
Environment Variables Overview In this lesson, we discussed environment variables , which are variables that hold values specific to different environments (e.g., development, QA, staging). Each environment may have unique values for parameters such as base URLs and credentials. Configuring Environment Variables in Playwright To configure environment variables in Playwright: Use the predefined environment variable base URL located under the use block in the configuration file. Replace hardcoded URLs in tests with / to reference the base URL . Switching Between Environments To dynamically switch between environments: Create projects for each environment (e.g., dev , staging ) in the projects section. Define the base URL for each project, allowing Playwright to select the appropriate URL based on the project name. Defining Custom Environment Variables For additional environment variables: Create a testoptions.ts file to define custom variables. Import and extend the test type from Playwright to include these variables. Use process.env to access these variables in your tests. Using .env Files Environment variables can also be stored in a .env file: Install the dotenv library to read from the .env file. Define sensitive information (e.g., usernames, passwords) in the .env file and ensure it is included in .gitignore . Command Line Environment Variables Environment variables can be passed via the command line when running tests: URL=your_url npm test In summary, this lesson covered how to manage environment variables in Playwright, including defining base URLs, switching environments, and handling sensitive information securely.