How to Run the Same Playwright Test With Multiple Inputs
Do you need to test the same Playwright feature with different input values?
For example, you may want to check whether a login page accepts correct credentials, rejects an incorrect password, and displays a validation message when a required field is empty.
Writing a separate copy of the entire test for every input creates unnecessary repetition.
You can use an array of test data and a loop to define multiple Playwright tests that share the same basic structure.
This tutorial explains how to set up a parameterized test, add multiple inputs, define expected results, run the tests, and troubleshoot common problems.
The examples use Playwright Test with TypeScript.
What Does Running the Same Test With Multiple Inputs Mean?
Running the same test with multiple inputs means using a shared test structure to verify different input values.
Suppose you are testing a search field.
You want to search for:
Automation Testing
Software Testing
The basic steps are the same for every search:
Open the application.
Enter a search term.
Submit the search.
Check the expected result.
Instead of copying those steps three times, you can create test data and reuse the same structure.
Each input should still have a meaningful assertion.
Step 1: Set Up a Playwright Test File
If you already have a Playwright project, use its existing test folder.
For a new project, you can initialize Playwright using:
npm init playwright@latest
Create a file named:
tests/multiple-inputs.spec.ts
You will use this file to define the parameterized tests.
Step 2: Create an Array of Input Values
Start by creating an array containing the inputs you want to test.
const searchTerms = [
'Playwright',
'Automation Testing',
'Software Testing'
];
Each item represents a search term.
When you add another value to this array, you can test another input without duplicating the full test implementation.
Step 3: Use a Loop to Define the Tests
Import Playwright's test and expect functions.
import { test, expect } from '@playwright/test';
const searchTerms = [
'Playwright',
'Automation Testing',
'Software Testing'
];
for (const term of searchTerms) {
test(`Search for ${term}`, async ({ page }) => {
await page.goto('https://example.com');
await page.getByRole('textbox', {
name: 'Search'
}).fill(term);
await page.getByRole('button', {
name: 'Search'
}).click();
await expect(page).toHaveURL(/search/);
});
}
This example assumes that the application uses a search field named Search, a button named Search, and a URL containing search after submission.
Replace these details with the actual behavior of your application.
How Does This Code Work?
The array stores the inputs.
The searchTerms array contains the values you want to test.
The loop processes each input.
The for...of loop defines a test for each item.
The test title identifies the input.
A title such as Search for Playwright makes the report easier to understand.
The browser performs the actions.
The test opens the page, enters the current search term, and submits the form.
The assertion verifies the result.
The URL assertion checks whether the browser navigated to a URL matching the supplied pattern.
For production tests, verify the actual search results or expected page content when possible. A URL change alone may not prove that the search feature works correctly.
Step 4: Use Different Expected Results for Different Inputs
Sometimes, different inputs must produce different results.
For example, an empty username should display a validation message, while a valid username should be accepted.
Store the input and expected result together.
import { test, expect } from '@playwright/test';
const usernameCases = [
{
name: 'valid username',
username: 'rakesh123',
expectedMessage: 'Username accepted'
},
{
name: 'empty username',
username: '',
expectedMessage: 'Username is required'
},
{
name: 'short username',
username: 'ab',
expectedMessage: 'Username must be at least 3 characters'
}
];
for (const data of usernameCases) {
test(`Check ${data.name}`, async ({ page }) => {
await page.goto('https://example.com/register');
await page.getByLabel('Username').fill(data.username);
await page.getByRole('button', {
name: 'Submit'
}).click();
await expect(
page.getByRole('alert')
).toHaveText(data.expectedMessage);
});
}
This example assumes that the application displays the expected validation message inside an accessible alert.
Change the URL, locator, and expected messages to match your application.
Why Is This Better Than a Simple Array?
A simple array is enough when you only need to vary one input and the same assertion applies to every case.
A structured array of objects is more useful when each scenario has its own expected outcome.
It also makes the test data easier to understand.
Step 5: Run the Test File
Use the following command:
npx playwright test tests/multiple-inputs.spec.ts
Playwright executes the tests defined in the file.
If you define three tests from three input values, you should see three test cases in the test results, assuming no filtering or other configuration excludes them.
Step 6: Watch the Browser Execute the Tests
To open the browser window while the tests run, use:
npx playwright test tests/multiple-inputs.spec.ts --headed
This is useful when you want to observe the navigation, form filling, and button clicks.
Headed mode requires an environment that supports displaying a browser window.
Step 7: Run Tests With Different Browsers
Playwright supports browser projects, allowing a test suite to run in configured browsers.
For example, a project may include Chromium, Firefox, and WebKit.
If you run three input cases in each of three configured browser projects, the suite can execute up to nine combinations.
The total depends on the project configuration and any filters you use.
Parameterized tests vary the data. Browser projects vary the browser configuration.
These features can work together.
Step 8: Organize the Test Data in a Separate File
When your test data becomes large, place it in a separate file.
For example, create search-data.ts.
export const searchCases = [
{
name: 'Playwright search',
term: 'Playwright',
expectedHeading: 'Search Results'
},
{
name: 'Automation Testing search',
term: 'Automation Testing',
expectedHeading: 'Search Results'
}
];
Import the data into your test file.
import { test, expect } from '@playwright/test';
import { searchCases } from './search-data';
for (const data of searchCases) {
test(data.name, async ({ page }) => {
await page.goto('https://example.com');
await page.getByRole('textbox', {
name: 'Search'
}).fill(data.term);
await page.getByRole('button', {
name: 'Search'
}).click();
await expect(
page.getByRole('heading', {
name: data.expectedHeading
})
).toBeVisible();
});
}
The import path depends on where your files are located.
Use a separate data file when it makes the project easier to organize. For a small test, keeping the data in the same file may be simpler.
Step 9: Debug a Failed Input Case
Suppose the test for Automation Testing fails while the other search cases pass.
The test title helps you identify the failed input.
Next, investigate:
Was the input entered correctly?
Did the application return the expected result?
Is the expected heading correct?
Is the locator targeting the right element?
Does the application require additional setup?
Is the test affected by shared state or external dependencies?
Do not immediately change the assertion to make the test pass.
First determine whether the problem comes from the application, the test data, or the test implementation.
Common Mistakes When Running Multiple Inputs
Mistake 1: Using the Same Assertion for Every Scenario
Different inputs may require different expected results.
Solution: Include expected outcomes in the test data when the scenarios behave differently.
Mistake 2: Creating Duplicate Test Code
Copying the same workflow for every input makes future changes harder.
Solution: Reuse a common test structure when the scenarios share the same steps.
Mistake 3: Choosing Unclear Test Names
Test names such as test1 or test2 do not explain what failed.
Solution: Include the scenario or input category in the test title.
Mistake 4: Testing Too Many Combinations
The number of combinations can grow quickly when multiple fields have multiple possible values.
Solution: Prioritize important scenarios and boundary values instead of blindly testing every combination.
Mistake 5: Ignoring Test Independence
Two tests that modify the same account or record may interfere with each other.
Solution: Use independent test data and appropriate setup and cleanup procedures.
Frequently Asked Questions
How can I run the same Playwright test with multiple inputs?
Create an array of input values and use a loop to define a test for each value. Add assertions that verify the expected behavior for each input.
Can I use multiple input values in Playwright Test?
Yes. You can use arrays of strings or arrays of objects to store input values, scenario names, and expected outcomes.
Should each input be a separate test?
Usually, separate test cases are useful because each scenario can have its own title, result, and failure details. Playwright can then schedule and report those cases through its normal test runner.
Can I run multiple input tests in parallel?
Yes, if the cases are defined as separate tests. Actual parallel execution depends on Playwright's configuration, worker settings, and test isolation.
Can I test valid and invalid inputs in the same file?
Yes. You can store valid and invalid scenarios in one data array, provided the test structure and assertions remain clear.
Does using a loop reduce the number of test executions?
No. A loop can reduce duplicated source code, but each defined test case still needs to execute.
Conclusion
Running the same Playwright test with multiple inputs is straightforward when you use arrays, loops, and meaningful assertions.
Start by defining the inputs you want to test. Add the expected outcome for each scenario, create the tests, and run them using Playwright Test.
For simple cases, an array of strings may be enough. For more complex scenarios, an array of objects can make the inputs and expected results easier to understand.
The goal is not just to run the same steps repeatedly. It is to verify that every important input produces the correct application behavior.
For more practical automation testing tutorials, visit PlaywrightMasters.in.

Comments
Post a Comment