Rayrun
← Back to Discord Forum

Using dependency as a global setup structure to run for each spec file

Hey everyone,

I'm working on an API project and using request. I've created an auth.setup.ts file which has some test that are:

  • Call 1 - POST to authorize user
  • Call 2 - POST to get the token. I'm storing these in the .env file as the TOKEN is needed.
-tests
--folder1
---001_folder1_test_one.spec.ts
--folder2
---001_folder2_test_one.spec.ts
---002_folder2_test_two.spec.ts
auth.setup.ts

In my playwright.config.ts file

If I have set up a dependency for folder 2 to run auth.setup.ts before tests run. Does it get run before each of the spec files or does it get only once and then goes through each of the files inside the folder2?

I need to setup the auth for each spec file so that it's for a new user. How can I do this? Please help.

This thread is trying to answer question "How can I login with different user/token for each spec file in an API project using Playwright?"

3 replies

@zak2_4 could u send your pw config ?

Hi @yusufozt. yep here is my pw config. I need to be able to go through auth.setup.ts for each test spec as I need a fresh user token. Is this the best way? Or is there another way to do this, I've been reading about fixtures, but all new to this.

import { defineConfig, devices } from '@playwright/test';
import dotenv from 'dotenv';

require('dotenv').config();



export default defineConfig({
  testDir: './tests',
  reporter: [["list"], ["html"]],
  retries: 0,
  timeout: 30000,

  use: {
    baseURL: process.env.URL,,
    ignoreHTTPSErrors: true,
    trace: "retain-on-failure",
  },
  projects: [
    {
      name: 'setup_auth',
      testMatch: 'auth.setup.ts',
    },
    {
      name: 'basic_not_started_signup_flow',
      testDir: './tests/folder1',
      dependencies: ['setup_auth'],
      use: {
        baseURL: process.env.URL,
        ignoreHTTPSErrors: true,
        extraHTTPHeaders:{
          'Authorization': `Bearer ${process.env.TOKEN}`
        },
      }
    },
    {
      name: 'basic_signing_up',
      testDir: './tests/folder2',
      dependencies: ['setup_auth'],
      use: {
        baseURL: process.env.URL,
        ignoreHTTPSErrors: true,
        extraHTTPHeaders:{
          'Authorization': `Bearer ${process.env.TOKEN}`
        },
        
      }
    }
  ]  
});

Can anyone advice please? How can I login with different user/token for each spec file?

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].