Playwright Create tests via a CSV file

In this article, we will see how we can run tests from a CSV file in playwright. Here is the CSV file we will be working with today, which contains some URLs of sites, and we will visit them.

const { test } = require('@playwright/test');
const fs = require("fs");
 
var links = fs.readFileSync("CSV\\Book1.csv")
   .toString() 
   .split('\n') 
   .map(e => e.trim())
for (const link of links) {
   test('test for ' + link.toString(), async ({ page }) => {
      console.log(link);
       await page.goto(link);

   });
}
  • First, we are reading the CSV file from the path, cleaning, trimming, and reading the buffer of each line.
  • We iterate the links variable using an advanced for loop wrapped around the test.
  • We then added the link variable, which was iterated to the test step to avoid duplicate test name errors.
  • Then the link variable was passed into the goto method of the playwright, which will now visit all the sites with their respective name with different tests.
Fri, 12/16/2022 - 10:51
Ashwin possesses over five years of experience in the Quality Assurance industry and is currently serving as a Technical Lead at iVagus. His expertise encompasses a broad range of technologies, including Cypress, Rest Assured, Selenium, Cucumber, JavaScript and TypeScript.
Tags

Comments

Comments