2
votes

I'm writing a test to register users on my site. Using @playwright/test, I have defined several different projects in playwright.config.ts:

{
  name: 'iPhone 12 Pro',
  use: devices['iPhone 12 Pro'],
},
{
  name: 'iPhone 12 Pro Max',
  use: devices['iPhone 12 Pro Max'],
},
{
  name: 'iPhone 5/SE',
  use: devices['iPhone SE'],
},

My test looks like this:

test('Register a user', async ({ page }) => {
  // Go to baseUrl/webapp/
  await page.goto(`${baseUrl}webapp/`);

  // Click the register tab.
  await page.click('ion-segment-button[role="tab"]:has-text("Register")');
  await page.click('input[name="mail"]');
  // @TODO: How do I get the project name here?
  await page.fill('input[name="mail"]', `[email protected]`);
  // Press Tab
  await page.press('input[name="mail"]', 'Tab');
  await page.fill('input[name="pass"]', 'password');

However, when I run this test, it only works for the first worker because you can only register an email address once.

So what I would like to do is to get access to the project name (for example, iPhone 12 Pro) in my test so that I can convert that to an email address so that each time the test is run, it will register a user based on the project name.

How can I get the project name within a playwright test?

I read the Playwright documentation about the workerInfo object but I can't figure out how to apply it within the test.

1

1 Answers

3
votes

You have access to the workerInfo like that as a second parameter, which you can use in that case to make your email unique per worker:

test('Register a user', async ({ page }, workerInfo) => {
  // Go to baseUrl/webapp/
  await page.goto(`${baseUrl}webapp/`);

  // Click the register tab.
  await page.click('ion-segment-button[role="tab"]:has-text("Register")');
  await page.click('input[name="mail"]');
  await page.fill('input[name="mail"]', `test-user-${workerInfo.workerIndex}@example.com`);

You can also get the project name by workerInfo.project.name.

See here for more information.