102 lines
3.8 KiB
TypeScript
102 lines
3.8 KiB
TypeScript
import { test, expect, Page } from '@playwright/test';
|
|
import { strictEqual } from 'assert';
|
|
|
|
let page: Page;
|
|
let isLoginSuccessful = false;
|
|
|
|
test.beforeAll(async ({ browser }) => {
|
|
page = await browser.newPage();
|
|
});
|
|
|
|
test.afterAll(async () => {
|
|
if (page !== undefined) {
|
|
await page.close();
|
|
}
|
|
});
|
|
|
|
async function login(page) {
|
|
try {
|
|
// Login
|
|
await page.goto('/');
|
|
await expect(page).toHaveTitle(/^Sign in to /);
|
|
await page.fill("input[name='username']", 'admin');
|
|
await page.fill("input[name='password']", '1234');
|
|
await page.click('id=kc-login');
|
|
await page.waitForTimeout(2000);
|
|
// await page.click('id=acceptBtn');
|
|
|
|
// เข้าสู่เมนูลูกค้า
|
|
await page.click('id=menu.manage');
|
|
await page.waitForSelector('id=sub-menu-customer');
|
|
await page.click('id=sub-menu-customer');
|
|
await page.waitForTimeout(2000);
|
|
|
|
isLoginSuccessful = true;
|
|
console.log('Login สำเร็จ');
|
|
} catch (error) {
|
|
console.error('เกิดข้อผิดพลาดในการ Login', error);
|
|
isLoginSuccessful = false;
|
|
}
|
|
}
|
|
|
|
test('Login', async () => {
|
|
await login(page);
|
|
});
|
|
|
|
test('ทดสอบการค้นหาข้อมูลเมนูลูกค้า', async () => {
|
|
if (!isLoginSuccessful) {
|
|
await login(page);
|
|
}
|
|
try {
|
|
// กำหนดคำที่จะค้นหา
|
|
const searchDatas = [
|
|
'Yossapat',
|
|
'ยศพัฒน์ ธนากานต์',
|
|
'AsiaTech Systems Co., Ltd',
|
|
'Systems',
|
|
'Tanakarn',
|
|
];
|
|
|
|
for (const searchData of searchDatas) {
|
|
// พิมพ์คำที่ต้องการค้นหา
|
|
await page.fill('id=input-search', searchData); // ดึงคำมาจาก searchDatas
|
|
await page.waitForTimeout(2000);
|
|
|
|
// ดึงผลลัพฑ์การค้นหาทั้งหมด
|
|
const searchResults = page.locator("//table[@class='q-table']//tbody[1]");
|
|
const resultCount = await searchResults.count();
|
|
|
|
// ถ้าไม่มีผลลัพธ์ให้ข้ามคำถัดไป
|
|
if (resultCount === 0) {
|
|
console.error(`ไม่พบการค้นหา: '${searchData}', ข้ามไปคำถัดไป`);
|
|
continue; // ข้ามคำค้นหาคำถัดไป
|
|
}
|
|
|
|
// ดึงข้อความทั้งหมดของผลลัพธ์การค้นหา
|
|
const searchResultTexts = await searchResults.allTextContents();
|
|
console.log(`ผลลัพธ์ของการค้นหา '${searchData}'`, searchDatas);
|
|
|
|
// ตรวจสอบว่าผลลัพธ์ทั้งหมดมีคำที่ค้นหาหรือไม่
|
|
const allResultContainSearchData = searchResultTexts.every((result) =>
|
|
result.includes(searchData),
|
|
);
|
|
|
|
// ถ้าไม่พบคำค้นหา ให้แค่แจ้งเตือนและข้ามไปคำถัดไป
|
|
if (!allResultContainSearchData) {
|
|
console.error(
|
|
`\x1b[31mผลลัพธ์บางรายการไม่ตรงกับคำค้นหา: '${searchData}', ข้ามไปคำถัดไป\x1b[0m`,
|
|
);
|
|
continue; // ข้ามไปคำค้นหาถัดไป
|
|
}
|
|
|
|
expect(allResultContainSearchData).toBe(true);
|
|
|
|
console.log(`การค้นหา '${searchData}' และการแสดงผลสำเร็จ`);
|
|
}
|
|
} catch (error) {
|
|
console.error('เกิดข้อผิดพลาดในการทดสอบการค้นหาหลายคำค้นหา', error);
|
|
isLoginSuccessful = false;
|
|
throw error;
|
|
}
|
|
await page.waitForTimeout(2000);
|
|
});
|