This thread is trying to answer question "How to implement authentication in Cucumber using Javascript on Playwright, using different JSON files for different roles?"
I mean this one: https://github.com/Tallyb/cucumber-playwright
Do you mean with playwright without cucumber? Like https://playwright.dev/docs/auth? But how do we implement this in cucumber for the repo of Tallyb??
@tallyb Do you mean a before hook like so a admin json will be load , and I can add another one for teamlead : Before(async function (this: ICustomWorld, { pickle }: ITestCaseHookParameter) { this.startTime = new Date(); this.testName = pickle.name.replace(/\W/g, '-');
// Load the storage state from the file const storageState = JSON.parse( await fs.readFile( 'C:/Users/Client/cucumber-playwright/src/auth/Admin.json', 'utf-8', ), );
yes that what I am doing: I add a step to my existing test cases. This step would allow me to log in as an admin using StorageState JSON, eliminating the need to enter login details every time. I also want to add another step to log in as a team lead using StorageState JSON, and another one to log in as a user using StorageState JSON. In this step, the following should occur: If no JSON file for the desired user exists, it is created. This is done by logging in with regular login details and storing the JSON. If the necessary JSON file exists, it is used. If the JSON file is no longer valid, it is recreated.
/* eslint-disable indent / / eslint-disable prettier/prettier */ // eslint-disable-next-line import/no-unresolved import { ICustomWorld } from '../support/custom-world'; import { Given } from '@cucumber/cucumber'; import { existsSync, readFileSync } from 'fs';
Given('the user logged in as an admin', async function (this: ICustomWorld) { const url = 'https://practice.automationtesting.in/my-account/'; const authFile = 'playwright/.auth/admin.json'; let storageState;
// Check if the JSON file exists and is not empty if (existsSync(authFile)) { try { const fileContent = readFileSync(authFile, 'utf-8').trim(); if (fileContent !== '') { storageState = JSON.parse(fileContent); } } catch (error) { console.error('Error reading or parsing the JSON file:', error); } }
if (storageState) { // Restore state await this.setStorageState(authFile); } else { // Sign in await this.page?.goto(url); await this.loginPage?.login('[email protected]', 'Test1234567'); // Save the StorageState await this.saveStorageState(authFile); await this.page?.waitForLoadState('load'); } });
// In Custom-world I have saveStorageState and setStorageState methods: async saveStorageState(path: string) { if (this.context) { const storage = await this.context.storageState(); await fs.writeFile(path, JSON.stringify(storage)); } }
async setStorageState(path: string) { if (this.context) { const storageState = JSON.parse(await fs.readFile(path, 'utf-8')); this.context = await this.browser.newContext({ storageState, // Use the storageState here acceptDownloads: true, recordVideo: process.env.PWVIDEO ? { dir: 'screenshots' } : undefined, viewport: { width: 1200, height: 800 }, }); } }
// And in Common-hooks in the "before" I will use the storageState Before(async function (this: ICustomWorld, { pickle }: ITestCaseHookParameter) { this.startTime = new Date(); this.testName = pickle.name.replace(/\W/g, '-'); const storageState = JSON.parse(await fs.readFile('playwright/.auth/admin.json', 'utf-8')); this.context = await browser.newContext({ storageState, // Use the storageState here acceptDownloads: true, recordVideo: process.env.PWVIDEO ? { dir: 'screenshots' } : undefined, viewport: { width: 1200, height: 800 }, }); });
Is this the right approach? because I want to run later admin,teamlead und user storageState JSON in parallel, will this be defined in before hooks? and how??
Thank you and Iwould be pleased to receive more information and how I can manage my storage state with multiple different users. And I asked the same question in stack overflow but still no answer there: https://stackoverflow.com/questions/76587815/using-storagestate-json-of-different-userse-g-admin-teamlead-with-playwright
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].