import { test, expect, Page } from '@playwright/test'; import { strictEqual } from 'assert'; import { Console, error, log } from 'console'; let page: Page; test.beforeAll(async ({ browser }) => { page = await browser.newPage(); }); test.afterAll(async () => { if (page !== undefined) { await page.close(); } }); test('Login', async () => { // Login await page.goto('http://192.168.1.90:20001/'); 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'); }); test('Search Data Branch Management', async () => { await page.click('id=menu-icon-branch-management'); 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); throw error; } await page.waitForTimeout(2000); });