Environment Variables | Bondar Academy
Course: Playwright UI Testing with TypeScript
Module: Advanced Tricks and Techniques
Instructor: Artem Bondar
Lesson Summary
Environment Variables in Playwright are essential for managing different configurations across various environments, such as development and QA. This lesson demonstrates how to configure these variables effectively within the Playwright framework. Configuring Base URL Instead of hardcoding the application URL in multiple spec files, it is best practice to define it in a single location: Use the base URL property in the Playwright configuration file. Set the application URL in the base URL field. Replace hardcoded URLs in the tests with a placeholder / . Using .env Files For managing multiple environment configurations: Install the .env package to handle environment variables. Create a .env file for the default environment and a .env.qa for QA. Define environment-specific variables, such as URL , in these files. Switching Environments To switch between different environment files: Use a command line variable testenv to specify which .env file to load. Modify the script in package.json to include this variable for easy switching. Best Practices Always add .env files to .gitignore to prevent sensitive data from being committed. In CI/CD environments, manage environment variables securely through a secret manager. Override environment variables directly in the command line when necessary. By following these steps, you can effectively manage environment variables in Playwright, ensuring a smooth testing process across different environments.