import { test, expect, Page } from '@playwright/test'; import { strictEqual } from 'assert'; import { log } from 'console'; import { errorMonitor } from 'events'; 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('http://192.168.1.62:20101/'); 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-personnel'); await page.click('id=sub-menu-personnel'); isLoginSuccessful = true; console.log('Login สำเร็จ'); } catch (error) { console.error('เกิดข้อผิดพลาดในการ Login'); isLoginSuccessful = false; } } test('Login', async () => { await login(page); }); test('Search Personnel', async () => { if (!isLoginSuccessful) { await login(page); } try { // กำหนดคำที่จะค้นหา const searchDatas = [ 'อานน', '0842262228', '1000001', '10', '20', '30', '40', ]; 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); });