Project Dependencies for authentication

Example project showing how project dependencies in Playwright work so that you can login to an application and save your credentials in storage state. Tests that rely on the logged in setup can then use this storage stage when running the tests meaning they don't have to login again before each test.

projects: [
    // this matches all tests ending with .setup.ts
    {
      name: 'setup',
      testMatch: '**/*.setup.ts',
    },
    // this project depends on the setup project
    {
      name: 'e2e tests logged in',
      testMatch: '**/*loggedin.spec.ts',
      dependencies: ['setup'],
      use: {
        storageState: STORAGE_STATE,
      },
    },
    // this project runs all tests expect ones for logged in
    {
      name: 'e2e tests',
      testIgnore: ['**/*loggedin.spec.ts', '**/*.setup.ts'],
    },
  ],

Clone project and run tests

In order to run the tests from this project locally first clone this repo and then create an account on Wikepedia and add your username and password to a .env file which you need to create at root level.

Example of .env file:

USERNAME: your_username
PASSWORD: your_password

Make sure you install all dependencies.

npm install

Install necessary browsers

npx playwright install 

Run the tests using the VS Code extension or the CLI.

npx playwright test

Running specific tests

To run only the setup test

npx playwright test --project setup

To run only logged in tests that depend on the setup project

npx playwright test --project "e2e tests logged in"

To run the tests where no login is needed

npx playwright test --project "e2e tests"

View reports and traces of the tests

Once you have run the tests you can see a report of the tests by running the show-report command.

npx playwright show-report

To see a trace of your tests locally including the setup tests run the tests with the flag --trace on and then open the report and click on the trace icon.

npx playwright test --trace on

trace viewer showing trace of the login test

Running the tests on CI

To run this project on CI you need to add the USERNAME and PASSWORD as GitHub secrets. This is done through the settings and under security you will see Secrets and Variables. Click on the actions and then add a new repository secret for both the USERNAME and PASSWORD.