Files
sam-design/mes기획서_리액트/test_menu_structure.js
2025-12-18 09:28:44 +09:00

143 lines
5.0 KiB
JavaScript

const { chromium } = require('playwright');
const fs = require('fs');
const path = require('path');
async function runTests() {
const browser = await chromium.launch();
const page = await browser.newPage();
const results = [];
const screenshotDir = path.join(__dirname, 'test_screenshots');
// Create screenshot directory if it doesn't exist
if (!fs.existsSync(screenshotDir)) {
fs.mkdirSync(screenshotDir, { recursive: true });
}
try {
console.log('테스트 시작...\n');
// 1. 페이지 로드 및 로그인
console.log('1단계: 페이지 로드 및 관리자 로그인');
await page.goto('http://localhost:3000', { waitUntil: 'networkidle' });
await page.screenshot({ path: path.join(screenshotDir, '01_initial_page.png') });
results.push('✅ 페이지 로드 완료');
// 로그인 폼 대기 및 입력
await page.fill('input[name="username"]', 'admin');
await page.fill('input[name="password"]', 'admin123');
await page.click('button:has-text("로그인")');
// 로그인 후 페이지 로드 대기
await page.waitForNavigation({ waitUntil: 'networkidle' });
await page.screenshot({ path: path.join(screenshotDir, '02_after_login.png') });
results.push('✅ 관리자 로그인 완료');
// 2. 기준정보 메뉴 클릭 및 서브메뉴 펼치기
console.log('\n2단계: 기준정보 메뉴 클릭');
// 좌측 메뉴 찾기 및 클릭
const menuItem = page.locator('text=기준정보').first();
await menuItem.click();
await page.waitForTimeout(500); // 서브메뉴 펼치기 대기
await page.screenshot({ path: path.join(screenshotDir, '03_menu_expanded.png') });
results.push('✅ 기준정보 메뉴 펼침');
// 3. 5개 메뉴 존재 확인
console.log('\n3단계: 서브메뉴 존재 확인');
const expectedMenus = [
'품목기준관리',
'공정기준관리',
'검사기준관리',
'거래처기준관리',
'현장기준관리'
];
const menuResults = [];
for (const menuName of expectedMenus) {
try {
const menu = page.locator(`text=${menuName}`);
const count = await menu.count();
if (count > 0) {
results.push(`✅ "${menuName}" 메뉴 존재`);
menuResults.push({ menu: menuName, exists: true });
console.log(`${menuName}`);
} else {
results.push(`❌ "${menuName}" 메뉴 미존재`);
menuResults.push({ menu: menuName, exists: false });
console.log(`${menuName}`);
}
} catch (e) {
results.push(`❌ "${menuName}" 메뉴 확인 실패 - ${e.message}`);
menuResults.push({ menu: menuName, exists: false });
console.log(`${menuName} (오류: ${e.message})`);
}
}
// 4. 각 메뉴 클릭 및 탭 확인
console.log('\n4단계: 각 메뉴 클릭 및 탭 확인');
for (const menuName of expectedMenus) {
try {
const menu = page.locator(`text=${menuName}`);
if (await menu.count() > 0) {
console.log(`\n [${menuName}] 클릭...`);
await menu.click();
// 페이지 로드 대기
await page.waitForTimeout(800);
// 탭 찾기
const tabs = page.locator('.ant-tabs-tab, [role="tab"], .tab-item');
const tabCount = await tabs.count();
console.log(` 탭 개수: ${tabCount}`);
if (tabCount === 6) {
results.push(`✅ "${menuName}" - 6개 탭 확인됨`);
console.log(` ✓ 6개 탭 확인`);
} else {
results.push(`⚠️ "${menuName}" - ${tabCount}개 탭 확인됨 (예상: 6개)`);
console.log(` ! ${tabCount}개 탭 확인 (예상: 6개)`);
}
// 각 메뉴별 스크린샷 찍기
const screenshotName = `04_${menuName}_loaded.png`;
await page.screenshot({ path: path.join(screenshotDir, screenshotName) });
}
} catch (e) {
results.push(`❌ "${menuName}" 메뉴 클릭 실패 - ${e.message}`);
console.log(` ✗ 오류: ${e.message}`);
}
}
// 최종 스크린샷
await page.screenshot({ path: path.join(screenshotDir, '05_final_state.png') });
} catch (error) {
console.error('테스트 중 오류 발생:', error);
results.push(`❌ 테스트 오류: ${error.message}`);
} finally {
// 결과 파일 저장
const resultFile = path.join(__dirname, 'test_results.txt');
const resultContent = `테스트 실행 결과
=====================================
실행 시간: ${new Date().toLocaleString()}
상세 결과:
${results.map((r, i) => `${i + 1}. ${r}`).join('\n')}
스크린샷 저장 위치: ${screenshotDir}
=====================================`;
fs.writeFileSync(resultFile, resultContent, 'utf-8');
console.log('\n\n테스트 결과:');
console.log(resultContent);
await browser.close();
}
}
runTests().catch(console.error);