Reputation: 121
How to pass different test data to a test in Playwright test runner? Like sending the test data from a csv file?
Upvotes: 2
Views: 9248
Reputation: 1
Data feeding to test cases via .csv file
Step-1: Install csv dependency first
npm install csv
Step-2: Import them in test file where you need to use csv files data
import fs from 'fs';
import path from 'path';
import { parse } from 'csv-parse/sync';
Step-3: Declaring and Initializing the data in csv file
const csv_records = parse(fs.readFileSync(path.join('./directory/file_data.csv')), {
columns: true,
skip_empty_lines: true
});
Step-4: Using the data in csv file using initialized variable.Jus console it and check it
for (const csv_record of csv_records) {
test(`check csv data ${csv_record.loop_count}`, async () => {
console.log("===>> no. of times the whole tests ran:" + csv_record.loop_count);
console.log("first_name:" + csv_record._first_name);
console.log("last_name:" + csv_record.last_name);
});
}
Final Result: After running the test case you will get log messages in terminal with values what we have console logged above..
===>> no. of times the whole tests ran: 1
first_name: Mega
last_name: K S
Upvotes: 0
Reputation: 640
You can loop through some JSON and parameterise your tests
JSON
const testData = [{
name:"test1",
url:"https://www.youtube.com"
},{
name:"test2",
url:"https://www.google.com"
},
{
name:"test3",
url:"https://www.bing.com"
}
]
Loop through the JSON
testData.forEach(data =>{
test(`MyTest ${data.name}`, async ({ page}) => {
await page.goto(data.url);
//Test assertion here
});
})
Upvotes: 0
Reputation: 464
function PreformMultipleTestsWithData(data) {
for(let i=0; i<data.length; i++) {
console.log(`xyz${data[i]}`);
test(`Testing data ${data[i]}`, async({page}, testInfo) => {
expect(data[i]).toBe(2);
})
}
}
PreformMultipleTestsWithData([1,2,3]);
Putting async issues aside, this works the UI and test results correctly reflect that 3 tests were preformed.
Update: According to https://github.com/microsoft/playwright/issues/9916 We cannot put the async issue aside.
In other words the following will not work
function async PreformMultipleTestsWithData() {
let data = await getData();
console.log("Data is present:", JSON.stringify(data))
for(let i=0; i<data.length; i++) {
console.log(`xyz${data[i]}`);
test(`Testing data ${data[i]}`, async({page}, testInfo) => {
expect(data[i]).toBe(2);
})
}
}
PreformMultipleTestsWithData();
but this will work:
function async PreformMultipleTestsWithData() {
let data = getData();
console.log("Data is present:", JSON.stringify(data))
for(let i=0; i<data.length; i++) {
console.log(`xyz${data[i]}`);
test(`Testing data ${data[i]}`, async({page}, testInfo) => {
expect(data[i]).toBe(2);
})
}
}
PreformMultipleTestsWithData();
Upvotes: 2
Reputation: 3222
You could use a csv-parser like this one and loop over the data to create new tests automatically. If you don't need to create tests automatically, you can also use it directly inside your test to get the externally data loaded. It might also make sense to wait / subscribe to this issue, which adds test.each
support to Playwright test.
Upvotes: 3