import { test, expect, Page } from '@playwright/test'; import { strictEqual } from 'assert'; import { 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) { 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-productService'); await page.click('id=sub-menu-productService'); await page.waitForTimeout(2000); // เปลี่ยนภาษา await page.click('id=btn-change-language'); await page.waitForSelector('id=btn-change-language-eng'); await page.click('id=btn-change-language-eng'); 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 { await page.waitForSelector('id=tree-enter-ประกัน', { state: 'visible' }); await page.click('id=tree-enter-ประกัน'); await page.waitForSelector('id=tree-enter-Products and Services-ประกัน', { state: 'visible', }); await page.click('id=tree-enter-Products and Services-ประกัน'); await page.click('id=btn-add'); await page.click('id=btn-info-basic-save'); // ตรวจสอบหลังการแจ้งเตือนหลังจากคลิกปุ่มบันทึก const ProduceandSeviceCodeError = page.locator( "(//div[@class='q-field__messages col']//div)[1]", ); const nameProduceandSeviceError = page.locator( "(//div[@class='q-field__messages col']//div)[2]", ); await expect(ProduceandSeviceCodeError).toHaveText( 'This field is required.', ); await expect(nameProduceandSeviceError).toHaveText( 'This field is required.', ); console.log('ระบบแสดงการแจ้งเตือนถูกต้อง'); } catch (error) { console.log('เกิดข้อผิดพลาดในการทดสอบ'); isLoginSuccessful = false; throw error; } await page.waitForTimeout(2000); });