import { test, expect, Page } from '@playwright/test'; import { strictEqual } from 'assert'; import { log } from 'console'; import exp from 'constants'; 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-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('Create Branch Management - ไม่มีการกรอกข้อมูล', async () => { if (!isLoginSuccessful) { await login(page); } try { // เปลี่ยนภาษา await page.click('id=btn-change-language'); await page.waitForSelector('id=btn-change-language-eng'); await page.click('id=btn-change-language-eng'); await page.click('id=hq-add-btn'); await page.click('id=btn-info-basic-save'); const expectedErrors = [ { locator: "(//div[@class='q-field__messages col']//div)[1]", message: 'This field is required.', }, { locator: "(//div[@class='q-field__messages col']//div)[2]", message: 'This field is required.', }, { locator: "(//div[@class='q-field__messages col']//div)[3]", message: 'This field is required.', }, { locator: "(//div[@class='q-field__messages col']//div)[4]", message: 'This field is required.', }, { locator: "(//div[@class='q-field__messages col']//div)[5]", message: 'This field is required.', }, { locator: "(//div[@class='q-field__messages col']//div)[6]", message: 'This field is required.', }, { locator: "(//div[@class='q-field__messages col']//div)[7]", message: 'Please select Province.', }, { locator: "(//div[@class='q-field__messages col']//div)[8]", message: 'Please select District.', }, { locator: "(//div[@class='q-field__messages col']//div)[9]", message: 'Please select Sub-district.', }, { locator: "(//div[@class='q-field__messages col']//div)[10]", message: 'This field is required.', }, ]; for (const error of expectedErrors) { const locator = page.locator(error.locator); await expect(locator).toHaveText(error.message); } console.log('ระบบตรวจสอบการแจ้งเตือนแล้ว'); } catch (error) { console.error('เกิดข้อผิดพลาดในการทดสอบ', error); isLoginSuccessful = false; throw error; } await page.waitForTimeout(4000); await page.click('id=btn-form-close'); console.log('การตรวจสอบ Validation ถูกต้อง'); }); test('Create Branch Managenment - กรณีที่กรอกชื่อย่อที่ไม่ใช่ภาษาอังกฤษ', async () => { if (!isLoginSuccessful) { await login(page); } try { await page.reload(); await page.click('id=hq-add-btn'); await page.fill("(//input[@id='input-abbreviation'])[2]", 'ภาษาไทย'); const invalidDataabbreviationError = page.locator( // ตรวจสอบการแสดง Validation แจ้งเตือนชื่อย่อในกรณีที่ไม่ใช้ภาษาอังกฤษ "(//div[contains(@class,'q-field__bottom row')])[1]", ); await expect(invalidDataabbreviationError).toHaveText( 'Invalid value. Only _ letters and number are allowed', { timeout: 2000, }, ); console.log( 'ตรวจสอบการแสดง Vidation แจ้งเตือนในกรณีที่ไม่ใช่ภาษาอังกฤษถูกต้อง', ); } catch (error) { console.error('เกิดข้อผิดพลาดในการทดสอบ', error); isLoginSuccessful = false; throw error; } await page.waitForTimeout(4000); await page.click('id=btn-form-close'); }); test('Create Branch Management - กรอกเลขนิติบุคคลในกรณีที่กรอกไม่ครบจำนวน 13 หลัก', async () => { if (!isLoginSuccessful) { await login(page); } try { await page.reload(); await page.click('id=hq-add-btn'); await page.fill("(//input[@id='input-tax-no'])[2]", '123'); const taxNoError = page.locator( // ตรวจสอบการแสดง Validation แจ้งเตือนชื่อย่อในกรณีที่ไม่ใช้ภาษาอังกฤษ "(//div[@class='q-field__messages col']//div)[2]", ); await expect(taxNoError).toHaveText( 'Invalid value. Please enter 13 character', { timeout: 2000, }, ); console.log( 'ตรวจสอบการแสดง Vidation แจ้งเตือนในกรณีที่กรอกทะเบียนนิติบุคคลเลขที่ไม่ครบ 13 หลัก', ); } catch (error) { console.error('เกิดข้อผิดพลาดในการทดสอบ', error); isLoginSuccessful = false; throw error; } await page.waitForTimeout(4000); await page.click('id=btn-form-close'); }); test('Create Branch Management - กรอกอีเมลในกรณีที่ไม่ตรงรูปแบบอีเมล', async () => { if (!isLoginSuccessful) { await login(page); } try { await page.reload(); await page.click('id=hq-add-btn'); await page.fill("(//input[@id='input-email'])[2]", '123'); const emailError = page.locator( // ตรวจสอบการแสดง Validation แจ้งเตือนในกรณีที่กรอกอีเมลไม่ถูกรูปแบบ "(//div[@class='q-field__messages col']//div)[2]", ); await expect(emailError).toHaveText('Invalid value.', { timeout: 2000, }); console.log( 'ตรวจสอบการแสดง Vidation แจ้งเตือนในกรณีที่กรอกอีเมลไม่ถูกรูปแบบ', ); } catch (error) { console.error('เกิดข้อผิดพลาดในการทดสอบ', error); isLoginSuccessful = false; throw error; } await page.waitForTimeout(4000); await page.click('id=btn-form-close'); });