Environment Variables not loading up during Playwright tests runs

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');
});

Looking at the docs for using environment variables, looks like you need to echo and export the variables into the shell environment:

Hi @jerdog, thanks for the quick reply!

I’ve updated the config.yml to do the echo and export as mentioned in the docs, you can see it below:

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: Set up environment variables
          command: |
            echo 'export USERNAME="${USERNAME}"' >> "$BASH_ENV"
            echo 'export PASSWORD="${PASSWORD}"' >> "$BASH_ENV"
            echo 'export MARVEL_PRIVATE_KEY="${MARVEL_PRIVATE_KEY}"' >> "$BASH_ENV"
            echo 'export MARVEL_PUBLIC_KEY="${MARVEL_PUBLIC_KEY}"' >> "$BASH_ENV"
      - 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

Whoever, my tests are still failing as they’re not getting the variables.

This is what the output looks like on the workflow:

Let me know if you need more details of the project or anything else

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

With that I’ve managed to make my tests pass

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.