Patrick Kenny
Patrick Kenny

Reputation: 6477

How do I access playwright test runner project info in a test?

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.

Upvotes: 12

Views: 12125

Answers (3)

Sasha Bond
Sasha Bond

Reputation: 1176

you can put custom project configuration -

  1. define your custom type TestOptions:
//testOptions.ts
import { test as base } from '@playwright/test';

export type TestOptions = {
    person: string;
    pageName: string;
    weight: number;
};

export const test = base.extend<TestOptions>({
    // change later in the playwright.config.ts
    person: ['AAA', { option: true }],
    pageName: ['nothing', { option: true }],
    weight: [0, { option: true }],
});
  1. in playwright.config.ts add actual custom properties for the project:
import { TestOptions } from './runner/testOptions';
...

projects: [
    {
      name: 'demo1',
      use: { person: 'Alice', pageName: 'open1', weight: 150 },
    },
    {
      name: 'demo2',
      use: { person: 'Alex', pageName: 'open2', weight: 180 },
    },
...
  1. access specific properties as
test('test project properties', async ({ page }, testInfo) => {    
    console.log(`--- calling project:  ${testInfo.project.name}`)

    console.log(`--- project.person:   ${testInfo.project.use["person"]}`)
    console.log(`--- project.pageName: ${testInfo.project.use["pageName"]}`)
    console.log(`--- project.weight:   ${testInfo.project.use["weight"]}`)
});
  1. call actual test for given project demo1
npx playwright test -g "test project properties" --project=demo1
  1. output:
--- calling project:  demo1
--- project.person:   Alice
--- project.pageName: open1
--- project.weight:   150

Upvotes: 4

pbaranski
pbaranski

Reputation: 25062

Nice option is to access testInfo:

test('config projects name', async ({}, testInfo) => {
  const projectsName = testInfo.project.name;
  expect(projectsName).toEqual('chromium');
});

Tested for config:

  projects: [
    {
      name: 'chromium',
      use: {
        ...devices['Desktop Chrome'],
      },
    }
  ],

Docs: https://playwright.dev/docs/api/class-testinfo

Hint: In VSC during debugging you can quickly find how this object looks like enter image description here

Hint2: Copy value of object testInfo from debugger and paste in new js file. Then find best parameter what suite you best (example below in Notepad++)
enter image description here

Upvotes: 3

Max Schmitt
Max Schmitt

Reputation: 3222

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.

Upvotes: 18

Related Questions