Here is my code
const {test, expect} = require('@playwright/test')
const { generateRandomUser } = require('../helpers')
const firstUser = generateRandomUser();
const secondUser = generateRandomUser();
const users = [firstUser, secondUser];
// parametrized test
users.forEach(user => {
console.log('USER', user);
const { username, email, password } = user;
test(`should register ${username}`, async ({ page }) => {
await page.goto('https://conduit.mate.academy/user/register');
await page.getByPlaceholder('Username').fill(username);
await page.getByPlaceholder('Email').fill(email);
await page.getByPlaceholder('Password').fill(password);
await page.getByRole('button', { name: 'Sign up' }).click();
await expect(page.locator(`a.nav-link[href='/profile/${username}']`)).toBeVisible();
await page.pause();
});
});
Here is the generateRandomUser function
const { faker } = require('@faker-js/faker');
exports.generateRandomUser = () => {
const random = Math.random().toString().slice(2, 6);
let username = faker.lorem.word() + '_' + random;
username = username.toLowerCase()
const email = username + '@gmail.com';
const password = faker.internet.password();
return {
username,
email,
password
}
}
So, basically I just want to have a good grasp of Playwright and just decided to try parametrize test, but when I run them I get this error
Internal error: unknown test(s) in worker:
chromium > exploring_playwright.spec.js > should register officiis_0247
2 failed
[chromium] › exploring_playwright.spec.js:12:3 › should register beatae_1183 ───────────────────
[chromium] › exploring_playwright.spec.js:12:3 › should register officiis_0247 ─────────────────
And moreover, when I use console.log() to see if the user was generated right I can see that in my console more than 2 outputs of users, which is strange, I guess this is the reason it fails but I have no idea how is it possible. I used this generateRandomUser() function for the previous tests and also for the Sign Up Page, like here and everything works fine.
const {test, expect} = require('@playwright/test')
const { generateRandomUser, registerNewUser } = require('../../helpers')
test.beforeEach(async ({page}) => {
await page.goto('https://conduit.mate.academy/user/register');
})
test('should register a new user', async ({page}) => {
const {username, email, password} = generateRandomUser();
await page.getByPlaceholder('Username').fill(username);
await page.getByPlaceholder('Email').fill(email);
await page.getByPlaceholder('Password').fill(password);
await page.getByRole('button', { name: 'Sign up' }).click();
await expect(page.locator(`a.nav-link[href='/profile/${username}']`)).toBeVisible();
})
I tried to use resources like Youtube videos and documentation. Here is the references (photos) this one and this one
I asked ChatGPT, tried to add describe block, tried to use test.describe.parallel, but it didn’t do much.
This option is also incorrect
for(let i = 0; i < 2; ++i) {
const {username, email, password} = generateRandomUser();
console.log(username, email, password)
test(`should register ${username}`, async ({ page }) => {
await page.goto('https://conduit.mate.academy/user/register');
await page.getByPlaceholder('Username').fill(username);
await page.getByPlaceholder('Email').fill(email);
await page.getByPlaceholder('Password').fill(password);
await page.getByRole('button', { name: 'Sign up' }).click();
await expect(page.locator(`a.nav-link[href='/profile/${username}']`)).toBeVisible();
await page.pause();
});
}
I still see 6 users created instead of 2.
3
Answers
I tried your code in base form and it is working fine
Output:
and I’m on Playwright version 1.35.1
I think generateRandomUser does not have any await, or is that function is async or not?
seems like your issue here is with username not getting defined till it is called which is failing the tests.
Use for- of loop instead:
Reference: https://stackoverflow.com/a/37576787
I beleive you might be running the tests on several workers which cause
firstUser
andsecondUser
to be generated for each worker. If you run the test with a single worker, the test will most likely run as expected:Several workers
If you want to run the test on several workers, you could set a seed to persist the faker generated data, example of implementation: