How to implement Data Driven methods in playwright using playwright/test typescript? How do you get external data(excel or csv) into script and drive multiple iterations with different data sets?
-
1Does this answer your question? Playwright Typescript how to convert a CSV file in to JSON object and use that data in Script to perform data driven testingYevhen Laichenkov– Yevhen Laichenkov2022-01-19 12:04:35 +00:00Commented Jan 19, 2022 at 12:04
-
No It is not working for me.Prasad– Prasad2022-01-19 14:44:40 +00:00Commented Jan 19, 2022 at 14:44
-
1It depends what you want. Seems like you do not want to convert .csv to JSON. That is the most natural thing when working with TS. But if not just tell what you want, you know that your .csv must be some kind of object so y can use it for DDT. What kind of object you want if it is not JSON?Gaj Julije– Gaj Julije2022-01-19 15:44:44 +00:00Commented Jan 19, 2022 at 15:44
-
Did you get anywhere with this problem?abe1432181– abe14321812022-02-21 17:40:35 +00:00Commented Feb 21, 2022 at 17:40
-
I tried using NPM modules to covert CSV format to Json, somehow nothing worked. I resolved it by writing custom method using typescript.Prasad– Prasad2022-02-24 13:40:34 +00:00Commented Feb 24, 2022 at 13:40
3 Answers
Data driven complete code example:
1). First create a CSV with required data:
"test_case","Name","Age"
"TC1","John","46"
"TC2","Chris","28"
2). Then read it in your test file using CSV library:
import fs from 'fs';
import path from 'path';
import { parse } from 'csv-parse/sync';
const csvFilePath = path.join(__dirname, 'input.csv');
const csvData = fs.readFileSync(csvFilePath, 'utf8');
const testData = parse(csvData, { columns: true });
// Now testData contains an array of test data representing each row in the CSV.
import { test } from '@playwright/test';
for (const row of testData) {
test(`Test case: ${row.test_case}`, async ({ page }) => {
// Use the data from the CSV file
console.log(`Name of User=> ${row.Name}`)
console.log(`Age of User=> ${row.Age}`)
});
}
For Further Reading:
https://playwright.dev/docs/test-parameterize
https://github.com/microsoft/playwright/issues/7036#issuecomment-925164212
How to pass different test data to a test in Playwright test runner?
Comments
According to Playwright Docs about Parameterized test (DDT), this is the official way:
const names = ["foo", "bar"];
for (const name of names) {
test(`testing with ${name}`, async ({ page }) => {
await page.goto("/");
console.log(name);
});
}
When executing this test it will run twice and appear in the log as two tests.

Comments
There is another way to do. You can use xlsx which will read (.xls/.xlsx/.xlsm/.csv) formats also and converts into array of Json Object.
import { readFile, utils, read as _read } from 'xlsx';
const read = ({ file, sheet }: { file: string, sheet: string }): Promise<any> => {
const workbook: any = readFile(file, { type: 'binary'});
if (sheet) {
const rows: any = utils.sheet_to_json(workbook.Sheets[sheet], {
raw: false,
})
return rows //This will return in the format of [{ <data from XL> }]
}}
example: if you have name and age as columns with multiple rows, the outcome will be as below, [{ name:'', age:'' },{ name:'', age:'' },]