Rayrun

What is the recommended way to handle sensitive environment variables like username and password in Playwright?

Answer

To handle sensitive environment variables in Playwright, you can use the following methods:

  1. Set variables in the command line: Run tests with your secret username and password set in the command line.
USERNAME=me PASSWORD=secret npx playwright test
  1. Read variables from a configuration file: Import defineConfig from '@playwright/test' and read environment variables.
import { defineConfig } from '@playwright/test';

export default defineConfig({
  use: {
    baseURL: process.env.STAGING === '1' ? 'http://staging.ray.run/' : 'http://ray.run/',
  },
});

Run tests against a staging or production environment:

STAGING=1 npx playwright test
  1. Use .env files: Use the dotenv package to read environment variables directly in the configuration file.
import { defineConfig } from '@playwright/test';
import dotenv from 'dotenv';
import path from 'path';

// Read from default ".env" file.
dotenv.config();

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

export default defineConfig({
  use: {
    baseURL: process.env.STAGING === '1' ? 'http://staging.ray.run/' : 'http://ray.run/',
  },
});

Edit the .env file to set variable values.

  1. Read variables from a CSV file: Use Node.js to read files from the filesystem and parse them with a CSV library before running tests.
Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].