jws-frontend/tests/01-Manage/04-ProductandService/JWS_PD_001_CreateProductandServiceGroup.spec.ts

95 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-09-18 17:52:25 +07:00
import { test, expect, Page } from '@playwright/test';
import { strictEqual } from 'assert';
2024-09-26 11:05:54 +07:00
import { log } from 'console';
2024-09-18 17:52:25 +07:00
let page: Page;
2024-12-03 17:59:46 +07:00
let isLoginSuccessful = false;
2024-09-18 17:52:25 +07:00
test.beforeAll(async ({ browser }) => {
page = await browser.newPage();
});
test.afterAll(async () => {
if (page !== undefined) {
await page.close();
}
});
2024-12-03 17:59:46 +07:00
async function login(page) {
try {
// Login
2025-01-13 11:47:01 +07:00
await page.goto('/');
2024-12-03 17:59:46 +07:00
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');
isLoginSuccessful = true;
console.log('Login สำเร็จ');
} catch (error) {
console.error('เกืดข้อผิดพลาดในการ Login', error);
isLoginSuccessful = false;
}
}
2024-09-18 17:52:25 +07:00
test('Login', async () => {
2024-12-03 17:59:46 +07:00
await login(page);
2024-09-18 17:52:25 +07:00
});
test('ทดสอบการเพิ่มกลุ่มสินค้าและบริการ', async () => {
2024-12-03 17:59:46 +07:00
if (!isLoginSuccessful) {
await login(page);
}
2024-09-18 17:52:25 +07:00
try {
// เพิ่มกลุ่มสินค้าและบริการ
2024-12-03 17:59:46 +07:00
2024-09-18 17:52:25 +07:00
await page.click('id=btn-add');
// กรอกข้อมูลกลุ่มสินค้าและบริการ
2024-09-26 11:05:54 +07:00
// await page.click("(//i[@aria-hidden='false'])[2]");
// await page.click("(//input[@id='input-source-nationality'])[2]");
// await page.click(
// "//span[normalize-space(text())='บริษัท คาโมมายด์ จำกัด']",
// );
2024-09-18 17:52:25 +07:00
await page.fill("(//input[@id='input-name'])[2]", 'ประกัน');
await page.fill("(//textarea[@id='input-detail'])[2]", 'รายละเอียดประกัน');
await page.fill("(//textarea[@id='input-remark'])[2]", 'หมายเหตุประกัน');
// บันทึกการสร้างกลุ่มสินค้นและบริการ
2024-12-03 17:59:46 +07:00
await page.click('id=btn-info-basic-save');
2024-09-18 17:52:25 +07:00
// ตรวจสอบหลังทำการสร้างกลุ่มสินค้าและบริการ
const newProductAndServiceLocator = page.locator(
"//div[normalize-space(text())='ประกัน']",
);
await newProductAndServiceLocator.waitFor({ state: 'visible' });
// ดึงข้อความให้ตรงจาก XPath
const newProductAndServiceName =
await newProductAndServiceLocator.textContent();
// ตรวจสอบความถูกต้องหลังทำการสร้างกลุ่มสินค้าและบริการ
if (newProductAndServiceName !== null) {
const trimmedName = newProductAndServiceName.trim();
expect(trimmedName).toBe('ประกัน');
console.log('การตรวจสอบสำเร็จ : ถูกต้อง');
} else {
throw new Error('ไม่พบข้อมูลที่บันทึก');
}
} catch (error) {
console.error('เกิดข้อผิดการในการทดสอบ');
2024-12-03 17:59:46 +07:00
isLoginSuccessful = false;
2024-09-18 17:52:25 +07:00
throw error;
}
await page.waitForTimeout(2000);
});