Improve test coverage by parameterising tests with Playwright and other tools

Automated tests can be run multiple times with different values by parameterising tests. Parameterising your tests can improve test coverage because a value in the test can be parameterised which means that the test can iterate through a number of values. This enables one automated test to cover a number of test cases. You could, for example, write one test that checks the behaviour of the application on entering a text string, and then parameterise the test to run with a number of different text strings. 

Parameterised tests also have less code to maintain because one parameterised test covers multiple test cases. 

When a test has been parameterised it is also easier to see what tests have been created because you can review the array holding the parameters rather than read multiple test cases.

You can, for example, parameterise tests using Playwright.

When you parameterise a Playwright test you will need to parameterise the test’s name because the test’s name is used in the name of the screenshot. If the value of your parameter can be used in the test name and in the test you can use an array of strings to hold your parameters. Playwrights documentation shows you how to parameterise a test with an array of strings: https://playwright.dev/docs/test-parameterize.

If there is more than one value that you want to parameterise then you will need to hold the values in a data structure such as a typescript interface. The test can then use a for loop to iterate through an array of objects which means that you can change a number of values each time the test is called. A simple example in which three values are changed each time the test is run is shown below:

Other test automation tools offer similar functionality for parameterising tests to Playwright, for example:

Published by Mike Harris

Mike has been working in testing for 20 years and is the lone tester for Geckoboard. He has been a Test Lead and has also worked as a part of waterfall, lean and agile teams. He has a B.Sc.(HONS) from Middlesex University and is an Associate of the University of Hertfordshire. He has set up and led a Testing Community of Practice and been part of a successful agile transition. He is Vice-Chair of the British Computer Society’s Specialist Interest Group in Software Testing. He also contributed to the e-books Testing Stories and How Can I test This? and has had articles published by the Ministry of Testing, LambdaTest and The QA Lead.

Join the Conversation

1 Comment

  1. Parameterizing tests (particularly for high(er) level parameterization) also allow for refactoring (or changing) of the test tooling or low level test logic for UI element locators and the actions performed on them.
    As an example, abstracting a sequence of actions to select a color into one (page object) method, and parameterize it to allow for selecting any color in the test. I have seen cases where the automation staff did not abstract it and/or didn’t parameterize, hard coding it to a single color which could fail if the color no longer existed and take more work to update test than a simply switch of color name if parameterized well.

    Like

Leave a comment

Design a site like this with WordPress.com
Get started