Skip to main content

7. Luyện tập nâng cao

Luyện tập với csv

Bài tập

Giả sử có các file csv sau:

categories.csv
"name","description"
"Name 1","Desc 1"
"Name 2","Desc 2"
"Name 3","Desc 3"
"Name 4","Desc 4"
"Name 5","Desc 5"
suppliers.csv
"name","email","phone","address"
"SONY","contact@sony.com","123456789","JAPAN"
"TOSHIBA","contact@toshiba.com","223456789","JAPAN"
"SAMSUNG","contact@samsung.com","323456789","KOREA"
"LG","contact@lg.com","423456789","KOREA"
"APPLE","contact@apple.com","523456789","USA"
customers.csv
first_name,last_name,email,phone,address,birthday
First Name 1,Last Name 1,customer1@gmail.com,123456789,Address 11
First Name 2,Last Name 2,customer2@gmail.com,223456789,Address 12
First Name 3,Last Name 3,customer3@gmail.com,323456789,Address 13
First Name 4,Last Name 4,customer4@gmail.com,423456789,Address 14
First Name 6,Last Name 5,customer5@gmail.com,523456789,Address 15
employees.csv
"first_name","last_name","email","phone","address","birthday"
"First Name 1","Last Name 1","employee1@gmail.com","123456789","Address 11"
"First Name 2","Last Name 2","employee2@gmail.com","223456789","Address 12"
"First Name 3","Last Name 3","employee3@gmail.com","323456789","Address 13"
"First Name 4","Last Name 4","employee4@gmail.com","423456789","Address 14"
"First Name 6","Last Name 5","employee5@gmail.com","523456789","Address 15"
products.csv
"name","description","price","discoun","stock","category_id","supplier_id"
"Product 1","Desc 1","100","10","100","Điện tử","SONY"
"Product 2","Desc 2","200","20","200","Điện lạnh","SAMSUNG"
"Product 3","Desc 3","300","30","300","Điện lạnh","LG"
"Product 4","Desc 4","400","40","400","Điện thoại","APPLE"
"Product 5","Desc 5","500","50","500","Điện tử","SONY"
  1. Viết test cases để kiểm thử Categories:
    • Thêm mới theo file csv
  2. Viết test cases để kiểm thử Suppliers:
    • Thêm mới theo file csv
  3. Viết test cases để kiểm thử Customers:
    • Thêm mới theo file csv
  4. Viết test cases để kiểm thử Employees:
    • Thêm mới theo file csv
  5. Viết test cases để kiểm thử Products:
    • Thêm mới theo file csv

Hướng dẫn

const { test, expect } = require('@playwright/test');

import fs from 'fs';
import path from 'path';
import { parse } from 'csv-parse/sync';
Đọc file csv
const records = parse(fs.readFileSync(path.join(__dirname, 'categories.csv')), {
columns: true, // Tự động đọc header
delimiter: ',', // Dấu phân cách
skip_empty_lines: true, // Bỏ qua dòng trống
});
// Trước khi chạy mỗi test case
test.beforeEach(async ({ page }) => {
// Đảm bảo luôn login trước khi chạy testcase
await page.goto('https://os-admin.aptech.io/login');

// Thực hiện login
await page.getByPlaceholder('Enter email').fill('tungnt@softech.vn');
await page.getByPlaceholder('Enter password').fill('123456789');
await page.getByRole('button', { name: 'Log In' }).click();

// Chờ cho đến khi chuyển trang
await page.waitForURL('https://os-admin.aptech.io/dashboard');

// Vào đúng đường dẫn
await page.goto('https://os-admin.aptech.io/online-shop/data-management/categories');
});
// Lặp qua từng dòng khi đọc được
let index = 0;
for (const record of records) {
index++;

const testName = 'TC-CATEGORIES: Add new category ' + index;
test(testName, async ({ page }) => {
// Khai báo biến
const name = record.name;
const description = record.description;

await page.getByRole('textbox', { name: '* Name :' }).fill(name);
await page.getByLabel('Description').fill(description);
await page.getByRole('button', { name: 'Save' }).click();

// Đảm bảo không có lỗi
await expect(page.getByRole('alert')).not.toBeVisible();

// Chờ tầm 1s
await page.waitForTimeout(1000);

// Dòng đầu tiên
const firstRow = await page.locator('.ant-table-row-level-0:nth-child(1)');
const newName = await firstRow.locator('.ant-table-cell:nth-child(2)').textContent();
const newDescription = await firstRow.locator('.ant-table-cell:nth-child(3)').textContent();

await expect(name).toEqual(newName);
await expect(description).toEqual(newDescription);
});
}

Luyện tập với screenshot

Bài tập

  • Thực hiện 1 chụp hình BEFORE save và AFTER save để so sánh sự khác biệt
  • Áp dụng cho tất cả testcases

Hướng dẫn

const { test, expect } = require('@playwright/test');

import fs from 'fs';
import path from 'path';
import { parse } from 'csv-parse/sync';
Đọc file csv
const records = parse(fs.readFileSync(path.join(__dirname, 'categories.csv')), {
columns: true, // Tự động đọc header
delimiter: ',', // Dấu phân cách
skip_empty_lines: true, // Bỏ qua dòng trống
});
// Trước khi chạy mỗi test case
test.beforeEach(async ({ page }) => {
// Đảm bảo luôn login trước khi chạy testcase
await page.goto('https://os-admin.aptech.io/login');

// Thực hiện login
await page.getByPlaceholder('Enter email').fill('tungnt@softech.vn');
await page.getByPlaceholder('Enter password').fill('123456789');
await page.getByRole('button', { name: 'Log In' }).click();

// Chờ cho đến khi chuyển trang
await page.waitForURL('https://os-admin.aptech.io/dashboard');

// Vào đúng đường dẫn
await page.goto('https://os-admin.aptech.io/online-shop/data-management/categories');
});
// Lặp qua từng dòng khi đọc được
let index = 0;
for (const record of records) {
index++;

const testName = 'TC-CATEGORIES: Add new category ' + index;
test(testName, async ({ page }) => {
// Khai báo biến
const name = record.name;
const description = record.description;

await page.getByRole('textbox', { name: '* Name :' }).fill(name);
await page.getByLabel('Description').fill(description);

// Chụp ảnh
await page.screenshot({ path: './tests/screen-shots/TC-CATEGORIES-Add-new-category-' + index + '-BEFORE', clip: { x: 0, y: 0, height: 1080, width: 1920 } });
await page.getByRole('button', { name: 'Save' }).click();

// Đảm bảo không có lỗi
await expect(page.getByRole('alert')).not.toBeVisible();

// Chờ tầm 1s
await page.waitForTimeout(1000);

await page.screenshot({ path: './tests/screen-shots/TC-CATEGORIES-Add-new-category-' + index + '-AFTER', clip: { x: 0, y: 0, height: 1080, width: 1920 } });

// Dòng đầu tiên
const firstRow = await page.locator('.ant-table-row-level-0:nth-child(1)');
const newName = await firstRow.locator('.ant-table-cell:nth-child(2)').textContent();
const newDescription = await firstRow.locator('.ant-table-cell:nth-child(3)').textContent();

await expect(name).toEqual(newName);
await expect(description).toEqual(newDescription);
});
}