thienphuong / playwright-with-typescript

0 stars 0 forks source link

Data-Driven Testing with CSV file #10

Open thienphuong opened 7 months ago

thienphuong commented 7 months ago

install papaparse

npm i --save-dev @types/papaparse
import { test, expect } from "@playwright/test";
import * as Papa from "papaparse";
import * as fs from "fs";

test.describe("User Registration", () => {
  test.beforeEach(async ({ page }) => {
    await page.goto("http://yourwebsite.com/register");
  });

  test("Unsuccessful registration due to invalid information", async ({
    page,
  }) => {
    interface User {
      name: string;
      email: string;
      password: string;
      message: string;
    }

    // Parse the CSV file
    const csv = fs.readFileSync("./testData.csv", "utf8");
    const results = Papa.parse(csv, {
      header: true,
      skipEmptyLines: true,
    }).data as User[];

    for (const data of results) {
      // Fill in the registration form
      await page.locator('input[name="name"]').fill(data.name);
      await page.locator('input[name="email"]').fill(data.email);
      await page.locator('input[name="password"]').fill(data.password);

      // Submit the form
      await page.getByRole("link", { name: "Register" }).click();

      // Check for the error message
      await expect(page.getByPlaceholder("Warning message")).toHaveText(data.message);

      // Refresh the page before the next test
      await page.reload();
    }
  });
});
thienphuong commented 7 months ago

testData.csv

name,email,password,message
"V","jacob.vu@example.com","paSswo1#rd","Name must be between 2 and 50 characters."
"Jacob Vu  Jacob Jacob Jacob Jacob Jacob Jacob Jacob Jacob Jacob","jacob.vu@example.com","paSswo1#rd","Name must be between 2 and 50 characters."
"Jacob Vu","@jacob 2023","paSswo1#rd","Invalid email address format."
"Jacob Vu","jacob.vu@example.com","paS1#rd","Passwords must be at least 8 characters."
"Jacob Vu","jacob.vu@example.com","weakpassword","Password must contain at least 1 uppercase letter, 1 numeric character, 1 special character."
"Jacob Vu","jacob.vu@example.com","WEAKPASSWORD","Password must contain at least 1 lowercase letter, 1 numeric character, 1 special character."
"Jacob Vu","jacob.vu@example.com","~`!@#$%^&()+=_-:;”’?","Password must contain at least 1 lowercase letter, 1 uppercase letter, 1 numeric character."
"Jacob Vu","jacob.vu@example.com","32372343","Password must contain at least 1 lowercase letter, 1 uppercase letter, 1 special character."
"Jacob Vu","jacob.vu@example.com","PaSswo1#rd","Password should not start with upper case."
"Jacob Vu","jacob.vu@example.com","paSswo1#rD","Password should not end with upper case."