Rayrun
← Back to Discord Forum

JavaScript: making a template for tests, used by test cases within the same file

I've got a file full of rather similar tests, with about 6 or so parameters that change with each test. I'd like to make a template that I can give the parameters to, but my first attempt I ended up with something that needs those 6 parameters (which made the thing hard to read); it didn't quite return what I needed; and it didn't show up properly in VSCode.

Clarifying example of the kind of test cases I have, with changing parts highlighted:

test(*functionName*, async ({}, testInfo) => {
  const command = new Commands(page);
  let screenshot;

  await test.step(*formName*, async () => await command.openForm(*formName*);
  await test.step(*whereToUseFunction*, async () => {
    const locator = await page.locator(*fieldName*);
    expect( await *testFunction*(locator).toBe(true);
    await *functionName*(locator);
  }

  await test.step('Take a screenshot', async () => screenshot = await page.screenshot({path: *functioName*.png});
  await testInfo.attach('screenshot', {body: screenshot, contentType: 'image/png'});

So, changing parts are functionName, formName, whereToUseFunction, fieldName, testFunction.

What I'm after is something like this: const testTemplate = (functionName, *other parameters*) => test(functionName, async ({}, testInfo) => *function body*);

This is possible for test.step, at least: const stepTemplate = exampleValue => { return test.step( exampleValue, async () => console.log('example') ); }; but I couldn't quite get it right for the whole test.

This thread is trying to answer question "How can I create a template for similar tests in JavaScript that takes multiple parameters?"

1 reply

Oh. I figured it out immediately after postings this... rubber ducks, huh. It just didn't show up properly in VSCode before I tried running the test from the command line.

Here is what worked out:

const testTemplate = exampleValue => {return test(exampleValue, async () => console.log(exampleValue)); };

const asdf = testTemplate('asdf');

asdf;

Then from command line I ran: npx playwright test -g "asdf"

Answer

Creating Parameterized Tests with Playwright

With Playwright Test, you can easily create parameterized tests in JavaScript.

Test Level Parameterization

First, you can define an array of parameters and loop over them. Here's an example:

const people = ['Alice', 'Bob'];
for (const name of people) {
  test(`testing with ${name}`, async () => {
    // ... your test code here
  });
}

This will generate multiple tests, each with a different parameter value.

Project Level Parameterization

You can also define options for your tests using the extend function from Playwright Test.

import { test as base } from '@playwright/test';

export type TestOptions = {
  person: string;
};

export const test = base.extend<TestOptions>({
  person: ['John', { option: true }],
});

test('test 1', async ({ page, person }) => {
  // ... your test code here
});

In this example, the person option is defined with a default value of "John". You can override this value in the configuration file or when running the tests.

Using Fixtures with Options

You can use fixtures with options to further customize your tests. Fixtures let you set up reusable state or perform common actions before each test.

import { test as base } from '@playwright/test';

export type TestOptions = {
  person: string;
};

export const test = base.extend<TestOptions>({
  person: ['John', { option: true }],
  
  page: async ({ page, person }, use) => {
    await page.goto('https://ray.run/chat');
    await page.getByLabel('User Name').fill(person);
    await page.getByText('Enter chat room').click();
    await use(page);
  },
});

test('test 1', async ({ page, person }) => {
  // ... your test code here
});

In this example, the page fixture is overridden to include the person parameter as a "name" for the chat room. Each test will receive a pre-configured page with the person's name already filled in.

By using Playwright Test's parameterization and fixture capabilities, you can create a template for similar tests in JavaScript that takes multiple parameters. This allows you to easily generate and customize tests based on different input values or configurations.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

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 luc@ray.run.