vitalets/playwright-bdd

Question: How to use playwright-bdd configurations for multiple envs with different user credentials : Description below

Closed this issue · 2 comments

Hello ,

--> need to run feature file/script on both the envs
--> URLs is different for the envs
--> feature file steps are same
--> user credentials will be different when running on different envs.

for example :

test case / feature file 01 : env1
user name : user01 should be used

test case / feature file 01 : env2
user name : user02 should be used

test case / feature file 02 : env1
user name : user03 should be used

test case / feature file 02 : env2
user name : user04 should be used

Can you please let me know what are the options/configurations I can use to execute this by using playwright-bdd framework

I'd suggest the following:

  1. take auth example as a starting point.
  2. add auth for all needed users to setup project
  3. configure projects passing env to use options:
const testDir = defineBddConfig({
  features: 'features/*.feature',
  steps: 'features/steps/*.ts',
});

const envs = [ 'stage',  'prod' ];
const bddProjects = envs.map(env => {
   return {
      name: `chromium (${env})`,
      testDir, 
      use: { env },
      dependencies: ['auth'],
    };
});

export default defineConfig({
  projects: [
    {
      name: 'auth',
      testDir: 'features/auth',
      testMatch: /setup\.ts/,
    },
    ...bddProjects
  ],
});
  1. overwrite baseURL and storageState fixtures to set correct base url and auth based on current env and filename:
export const test = base.extend({
  baseURL: async ({ baseURL, env }, use, testInfo) => {
    if (env === 'stage') {
        baseURL = 'https://stage.example.com';
    }
    if (env === 'prod') {
        baseURL = 'https://example.com';
    }
    await use(baseURL);
 },
  storageState: async ({ storageState, env }, use, testInfo) => {
    if (env === 'stage' && testInfo.file.includes('feature-01')) {
        storageState = 'playwright/.auth/user1.json';
    }
    if (env === 'prod' && testInfo.file.includes('feature-01')) {
        storageState = 'playwright/.auth/user2.json';
    }
    if (env === 'stage' && testInfo.file.includes('feature-02')) {
        storageState = 'playwright/.auth/user3.json';
    }
    if (env === 'prod' && testInfo.file.includes('feature-02')) {
        storageState = 'playwright/.auth/user4.json';
    }
    await use(storageState);
  },
});

Thanks @vitalets , Will try this approach and see how it goes..