Soemtimes there will come a need where we wanna execute same test with different data at the same time in playwright we can parameterize a test easily.
For this we are gonna use the login page of programsbuzz site. Various usernames will be used on the same test using parameterized method.
1.) First Step
First let us create a constant with three names for our username.
const people = ['Sasuke', 'Itachi','Naruto'];
2.) For Each
Using an advanced for loop encase the test within it.
for(){
test(, async ({page})=>
{
});
}
It should look like this.
3.) Demo In Action
const {test, expect} = require('@playwright/test');
const people = ['Sasuke', 'Itachi','Naruto'];
for (const name of people) {
test(`Parameterized Testing With ${name}`, async ({page})=>
{
await page.goto('https://www.programsbuzz.com/user/login');
await page.locator('#edit-name',`${name}`)
});
}
- Make sure to use backticks `` when using the parameterized name variable.
- From the report it is thus clear it has used three names from the constant we just iterated and performed the type input three times.
- The test ran three times with different inputs.
Conclusion:
This is how parameterization works and how different data can be tested within same test.
- Log in to post comments