This thread is trying to answer question "Is there an equivalent of the Cypress `run` API in Playwright for programmatically running tests and injecting environment variables?"
To be more clear we want to inject environment variables into our tests when it is running on pipeline . We are using bitbucket as our pipeline . For few environment variables we are using the bitbuckets repository variables but for few we have some constants defined in code . These constants are different for different tests . In cypress we do something like this
cypress.run({ config, env: ( we are injecting variables here)}
Searching the documentation for .env
should give you the required info I think.
https://playwright.dev/docs/intro
Something like this - https://docs.cypress.io/guides/guides/module-api#cypressrun
I had to convert how we implemented override configuration from Robot Framework, I am using https://www.npmjs.com/package/dotenv to build my .env files and I call a function in the playwright.config file that reads in an override file as well as the original .env file. It also validates that it has an entry for all environment files as safeguard from running the suite without a environment variable set. In GH actions, you just need to setup the .env file since it is holding secrets and should not be persisted, assuming BB has a similar approach. If you are interested in more details on this solution, let me know and I can share a sanitized version.
Related Ask AI answer for "How do I programmatically run tests and inject environment variables in Playwright?".
Sure, you can run tests and inject environment variables in Playwright. Use the npx playwright test
command to run tests. To inject environment variables, set them in the command line like this: USERNAME=me PASSWORD=secret npx playwright test
.
You can also use a configuration file, playwright.config.ts
, to define your base URL and other configurations using environment variables. Here's an example:
import { defineConfig } from '@playwright/test';
export default defineConfig({
use: {
baseURL: process.env.STAGING === '1' ? 'http://staging.ray.run/' : 'http://ray.run/',
},
});
Run your tests with different values for the STAGING
environment variable to switch between environments.
If you prefer, use .env
files to manage environment variables. Create a .env
file with key-value pairs:
STAGING=0
USERNAME=me
PASSWORD=secret
Then, in your playwright.config.ts
, read these values using a package like dotenv:
import { defineConfig } from '@playwright/test';
import dotenv from 'dotenv';
import path from 'path';
dotenv.config();
dotenv.config({ path: path.resolve(__dirname, '..', 'my.env') });
export default defineConfig({
use: {
baseURL: process.env.STAGING === '1' ? 'http://staging.ray.run/' : 'http://ray.run/',
},
});
This way, you can edit the .env
file to set any variables you'd like without modifying your test code. Playwright provides multiple ways to run tests and inject environment variables. You can customize your test runs based on different environments or configurations.
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].