jws-frontend/tests/01-Manage/04-ProductandService/JWS_PD_001_CreateProductandServiceGroup.spec.ts
2024-12-04 09:12:52 +07:00

94 lines
3.5 KiB
TypeScript

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('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-productService');
await page.click('id=sub-menu-productService');
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.click('id=btn-add');
// กรอกข้อมูลกลุ่มสินค้าและบริการ
// await page.click("(//i[@aria-hidden='false'])[2]");
// await page.click("(//input[@id='input-source-nationality'])[2]");
// await page.click(
// "//span[normalize-space(text())='บริษัท คาโมมายด์ จำกัด']",
// );
await page.fill("(//input[@id='input-name'])[2]", 'ประกัน');
await page.fill("(//textarea[@id='input-detail'])[2]", 'รายละเอียดประกัน');
await page.fill("(//textarea[@id='input-remark'])[2]", 'หมายเหตุประกัน');
// บันทึกการสร้างกลุ่มสินค้นและบริการ
await page.click('id=btn-info-basic-save');
// ตรวจสอบหลังทำการสร้างกลุ่มสินค้าและบริการ
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('เกิดข้อผิดการในการทดสอบ');
isLoginSuccessful = false;
throw error;
}
await page.waitForTimeout(2000);
});