Hi, I have user.json file with my users' credentials and want to create a central login that will log in all the users and the auth is then used in the tests for the different user. I get the following issue and went in different directions that took me to changes that I don't understand yet as I am a newbie. I reverted back to this point.
import { users } from './../config/user.json';
import { test, expect } from '@playwright/test';
type TokenData = { token: string | null };
const login = test<TokenData>('login', {}, async ({}, runTest) => {
async function login(username: string, password: string): Promise<string | null> {
try {
const response = await request.post(`baseURL` + '/sessions', {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password }),
});
const responseBody = JSON.parse(response);
const token = responseBody.accessToken;
return token;
} catch (error) {
console.error('Failed to obtain token:', error.message);
return null;
}
}
for (const user of users) {
const token = await login(user.username, user.password);
await runTest({ token }, async ({ token }) => {
// Run the actual test logic within the context of the logged-in user
console.log('Token:', token);
});
}
});
export default login;
"resource": "/c:/Users/souwehand/Documents/Automation/approach/src/fixture/login.fixture.ts",
"owner": "typescript",
"code": "2558",
"severity": 8,
"message": "Expected 0 type arguments, but got 1.",
"source": "ts",
"startLineNumber": 7,
"startColumn": 20,
"endLineNumber": 7,
"endColumn": 29
}]```
This thread is trying to answer question "How to create a central login using user credentials from a JSON file in Playwright using TypeScript?"
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].