Playwright Passing Environment Variables

This article will show how we can use environmental variables to configure tests from the command line. Sometimes we should keep our input credentials, for example, username and password, secret, and perhaps not a good idea to store them in source code.

This way, using environmental variables, we can pass in the credentials secretly from outside.

For this will be using our programsbuzz site, for example:

await page.goto("");
await page.locator('#edit-name').fill(process.env.USERNAME)
await page.locator('#edit-pass').fill(process.env.PASSWORD)
  • It got filled in once we passed the environmental variables for the username and password.

We can also read environmental variables from the config file.

const config = {
 use: {
   baseURL: process.env.URLS === '1' ? '' : '',
module.exports = config;

We can execute this the same way via this.

npx playwright testname

We can also use .env files to make things easier.

// Read from default ".env" file.
// Alternatively, read from "../my.env" file.
require('dotenv').config({ path: path.resolve(__dirname, '..', 'my.env') });

We can now edit the env file with the variable we choose to name and call.

Mon, 01/09/2023 - 12:41
Ashwin possesses over five years of experience in the Quality Assurance industry and is currently serving as a Technical Lead at iVagus. His expertise encompasses a broad range of technologies, including Cypress, Rest Assured, Selenium, Cucumber, JavaScript and TypeScript.