I’m trying to set up a Playwright project for personal use, I’ve added the environment variables in the Project Configuration, but when I run the workflow, these variables are not being loaded in my tests.
Here’s my config.yml
version: 2.1
jobs:
run-test:
docker:
- image: mcr.microsoft.com/playwright:v1.36.0
environment:
USERNAME: ${USERNAME}
PASSWORD: ${PASSWORD}
MARVEL_PRIVATE_KEY: ${MARVEL_PRIVATE_KEY}
MARVEL_PUBLIC_KEY: ${MARVEL_PUBLIC_KEY}
# Steps to the job
steps:
- checkout
- run:
name: Install dependencies
command: npm i -D @playwright/test
- run:
name: Install Playwright with Chrome
command: npx playwright install --with-deps chrome
- run:
name: Check Lint
command:
npm run lint-check
- run:
name: Run API and E2E tests
environment:
PLAYWRIGHT_JUNIT_OUTPUT_DIR: /test-results
PLAYWRIGHT_JUNIT_OUTPUT_NAME: results.xml
command:
npm run test-headless
- store_test_results:
path: results.xml
- store_artifacts:
path: results.html
# Invoke jobs via workflows
workflows:
run-test-workflow:
jobs:
- run-test
This is the script that I’m using on package.json
"test-headless": "npx playwright test"
Example on how I’m calling one of the variables from one of my tests:
test('Login with valid credentials', async ({ page }) => {
const loginPage = new LoginPage(page);
const generalElementsPage = new GeneralElementsPage(page);
await loginPage.goto();
await loginPage.checkLoginPage();
await loginPage.inputUserCredentials(process.env.USERNAME, process.env.PASSWORD);
await generalElementsPage.checkTopHeaderText('Dashboard');
});
Okay, so I did received a response from Support via email and applied the following suggestion to pass my env variables into my .env file. And also removing the environment section:
- run:
name: Set up environment variables into .env
command: |
echo "USERNAME=${USERNAME}" > .env
echo "PASSWORD=${PASSWORD}" >> .env
echo "MARVEL_PRIVATE_KEY=${MARVEL_PRIVATE_KEY}" >> .env
echo "MARVEL_PUBLIC_KEY=${MARVEL_PUBLIC_KEY}" >> .env