jws-frontend/tests/01-Manage/01-Admin-BranchManagement/JWS_BM_011_SearchBranchmanagement.spec.ts
Linpiing 272183bca9
Some checks failed
Spell Check / Spell Check with Typos (push) Failing after 6s
update
2025-05-22 09:26:31 +07:00

106 lines
3.9 KiB
TypeScript

import { test, expect, Page } from '@playwright/test';
import { strictEqual } from 'assert';
import { Console, error, log } from 'console';
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: 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-branch');
await page.click('id=sub-menu-branch');
await page.waitForTimeout(2000);
isLoginSuccessful = true;
console.log('ระบบทำการ Login');
} catch (error) {
console.error('เกิดข้อผิดพลาดในการ Login', error);
isLoginSuccessful = false;
}
}
test('Login', async () => {
await login(page);
});
test('Search Data Branch Management', async () => {
if (!isLoginSuccessful) {
await login(page);
}
try {
// กำหนดคำที่จะค้นหา
const searchDatas = [
'โคโมโดะ',
'คาโมมายด์',
'CM',
'CMO',
'00000',
'บางจาก',
'บางรัก',
'สีลม1',
];
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);
});