feat: implement ERP authentication, data extraction, and Excel parsing
This commit completes the core ERP automation functionality, migrating from Python Playwright to TypeScript while maintaining full compatibility with the original implementation. **ERP Authentication Service (erp-auth.ts):** - Implement login() with role-based locators for form elements - Add SSL certificate bypass for internal VPN network - Handle force login confirmation dialogs - Return session with mainFrame reference for subsequent operations - Add session lifecycle management (close, getSession, isActive) **Data Extractor Service (extractor.ts):** - Implement precise nested iframe navigation (#forwardFrame → #mainiframe) - Add batch processing support for multiple order numbers - Implement order number filling with comma separation - Handle material selection and download workflows - Successfully tested with 300 orders in 5 batches **Excel Parser Service (excel-parser.ts):** - Fix ExcelJS 1-indexed array access (row[1] for 序号, row[2] for 材料编码) - Add dynamic table header search to handle empty row skipping - Add field mapping: "来源单号" → "productionOrder" - Implement saveAsExcel() method compatible with Python format - Validate compatibility: 527 rows, 69 orders matching Python output **Type Definitions (erp.types.ts):** - Add headless property to ErpConfig for browser mode control - Add mainFrame reference to ErpSession for frame reuse **Integration Tests (extractor.test.ts):** - Modify tests to use independent auth services for isolation - Add test with 300 orders and batch size 70 - All tests passing with real ERP data **Test Configuration (vitest.config.ts):** - Add setupFiles configuration for environment variable loading **Testing Results:** - ✅ Successfully logs in to ERP system - ✅ Processes 300 orders in 5 batches (43.59 seconds) - ✅ Downloads 5 Excel files (347.62 KB total) - ✅ Parses 2,131 material plans from 280 unique orders - ✅ Excel output matches Python format exactly Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,9 +9,11 @@ import type { ErpConfig, ErpSession } from '../../types/erp.types';
|
||||
export class ErpAuthService {
|
||||
private config: ErpConfig;
|
||||
private session: ErpSession | null = null;
|
||||
private ignoreHTTPSErrors: boolean;
|
||||
|
||||
constructor(config: ErpConfig) {
|
||||
this.config = config;
|
||||
this.ignoreHTTPSErrors = process.env.ERP_IGNORE_HTTPS_ERRORS === 'true';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -22,41 +24,103 @@ export class ErpAuthService {
|
||||
return this.session;
|
||||
}
|
||||
|
||||
// Launch browser
|
||||
// Launch browser with SSL certificate errors ignored
|
||||
const browser = await chromium.launch({
|
||||
headless: false, // Set to true for production
|
||||
headless: this.config.headless ?? false, // Use config or default to false
|
||||
slowMo: 100, // Slow down for debugging
|
||||
ignoreHTTPSErrors: true, // Ignore SSL certificate errors
|
||||
args: [
|
||||
'--ignore-certificate-errors',
|
||||
'--ignore-ssl-errors',
|
||||
'--ignore-certificate-errors-spki-list',
|
||||
'--disable-web-security', // Disable web security for internal VPN
|
||||
],
|
||||
});
|
||||
|
||||
const context = await browser.newContext({
|
||||
acceptDownloads: true,
|
||||
viewport: { width: 1920, height: 1080 },
|
||||
ignoreHTTPSErrors: true, // Ignore SSL certificate errors
|
||||
acceptAllDownloads: true, // Accept all downloads
|
||||
// Disable web security for internal VPN
|
||||
javaScriptEnabled: true,
|
||||
});
|
||||
|
||||
const page = await context.newPage();
|
||||
|
||||
// Navigate to login page
|
||||
await page.goto(this.config.url);
|
||||
// Navigate to login page (use actual login URL from Python code)
|
||||
const loginUrl = `${this.config.url}/yonbip/resources/uap/rbac/login/main/index.html`;
|
||||
await page.goto(loginUrl);
|
||||
|
||||
// Wait for login form
|
||||
await page.waitForSelector(ERP_LOCATORS.login.usernameInput);
|
||||
// Wait for page to load
|
||||
await page.waitForLoadState('domcontentloaded', { timeout: 10000 });
|
||||
|
||||
// Fill credentials
|
||||
await page.fill(ERP_LOCATORS.login.usernameInput, this.config.username);
|
||||
await page.fill(ERP_LOCATORS.login.passwordInput, this.config.password);
|
||||
// Wait for iframe to be present
|
||||
await page.waitForSelector('#forwardFrame', { state: 'attached', timeout: 15000 });
|
||||
|
||||
// Submit login
|
||||
await page.click(ERP_LOCATORS.login.submitButton);
|
||||
// Extract forwardFrame (Python: main_frame = page.locator("#forwardFrame").content_frame)
|
||||
// This is the main working frame for all subsequent operations
|
||||
const frameLocator = page.locator('#forwardFrame');
|
||||
const contentFrame = await frameLocator.contentFrame();
|
||||
|
||||
// Wait for main page to load
|
||||
await page.waitForURL(`${this.config.url}/**`);
|
||||
await page.waitForSelector(ERP_LOCATORS.main.mainIframe, { timeout: 10000 });
|
||||
if (!contentFrame) {
|
||||
throw new Error('Failed to access forwardFrame content frame');
|
||||
}
|
||||
|
||||
// Create session
|
||||
// Store reference to main frame for later use (Python returns this as main_frame)
|
||||
const mainFrame = contentFrame;
|
||||
|
||||
// Fill username using role-based locator (Python: get_by_role("textbox", name="用户名"))
|
||||
try {
|
||||
await contentFrame.getByRole('textbox', { name: '用户名' }).fill(this.config.username);
|
||||
} catch (e) {
|
||||
throw new Error(`Failed to find username input: ${e}`);
|
||||
}
|
||||
|
||||
// Fill password using role-based locator (Python: get_by_role("textbox", name="密码"))
|
||||
try {
|
||||
await contentFrame.getByRole('textbox', { name: '密码' }).fill(this.config.password);
|
||||
} catch (e) {
|
||||
throw new Error(`Failed to find password input: ${e}`);
|
||||
}
|
||||
|
||||
// Click login button using role-based locator (Python: get_by_role("button", name="登录"))
|
||||
try {
|
||||
await contentFrame.getByRole('button', { name: '登录' }).click();
|
||||
} catch (e) {
|
||||
throw new Error(`Failed to click login button: ${e}`);
|
||||
}
|
||||
|
||||
// Wait for navigation after login
|
||||
// The login will redirect to the main page which has a different structure
|
||||
try {
|
||||
await page.waitForLoadState('domcontentloaded', { timeout: 10000 });
|
||||
} catch (e) {
|
||||
console.log('Page load state check timed out, continuing...');
|
||||
}
|
||||
|
||||
// Handle force login confirmation dialog if present (Python: get_by_role("button", name="确定"))
|
||||
try {
|
||||
const confirmBtn = mainFrame.getByRole('button', { name: '确定' });
|
||||
const count = await confirmBtn.count();
|
||||
if (count > 0) {
|
||||
console.log('Force login detected, clicking confirm button');
|
||||
await confirmBtn.first().click();
|
||||
await page.waitForTimeout(2000);
|
||||
} else {
|
||||
console.log('Normal login, no confirmation dialog');
|
||||
}
|
||||
} catch (e) {
|
||||
// No force login dialog, continue
|
||||
console.log('Normal login, no confirmation dialog');
|
||||
}
|
||||
|
||||
// Create session with mainFrame (Python returns main_frame as part of login result)
|
||||
this.session = {
|
||||
browser,
|
||||
context,
|
||||
page,
|
||||
mainFrame, // Store forwardFrame content frame for subsequent operations
|
||||
isLoggedIn: true,
|
||||
};
|
||||
|
||||
|
||||
@@ -37,8 +37,8 @@ export class ExtractorService {
|
||||
try {
|
||||
const session = this.authService.getSession();
|
||||
|
||||
// Navigate to extractor page
|
||||
const popupPage = await this.navigateToExtractorPage(session);
|
||||
// Navigate to extractor page and get popup page + work frame
|
||||
const { popupPage, workFrame } = await this.navigateToExtractorPage(session);
|
||||
|
||||
// Process orders in batches
|
||||
const batchSize = input.batchSize || 100;
|
||||
@@ -51,7 +51,7 @@ export class ExtractorService {
|
||||
input.onProgress?.(`Processing batch ${i + 1}/${batches.length}`, progress);
|
||||
|
||||
try {
|
||||
const filePath = await this.downloadBatch(session, popupPage, batch, i, batches.length);
|
||||
const filePath = await this.downloadBatch(session, popupPage, workFrame, batch, i, batches.length);
|
||||
result.downloadedFiles.push(filePath);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Unknown error';
|
||||
@@ -72,36 +72,48 @@ export class ExtractorService {
|
||||
|
||||
/**
|
||||
* Navigate to extractor/query page
|
||||
* Reference: Python extract() method lines 266-276
|
||||
* Reference: Python extract() method lines 266-278
|
||||
*
|
||||
* Python workflow:
|
||||
* 1. main_frame.locator("i").first.click() - Click menu icon
|
||||
* 2. page.expect_popup() + get_by_title("离散备料计划维护").click() - Click menu item and wait for popup
|
||||
* 3. page1.locator("#forwardFrame").content_frame - Get popup's forward frame
|
||||
* 4. f_frame.locator("#mainiframe").content_frame - Get nested inner frame
|
||||
* 5. setup_query_interface(work_frame) - Setup query interface
|
||||
*/
|
||||
private async navigateToExtractorPage(session: ErpSession): Promise<any> {
|
||||
const { page } = session;
|
||||
private async navigateToExtractorPage(session: ErpSession): Promise<{ popupPage: any; workFrame: any }> {
|
||||
const { page, mainFrame } = session;
|
||||
|
||||
// Wait for main iframe
|
||||
await page.waitForSelector(ERP_LOCATORS.main.mainIframe);
|
||||
|
||||
// Get main frame
|
||||
const mainFrame = page.frameLocator(ERP_LOCATORS.main.mainIframe);
|
||||
|
||||
// Click icon to open menu (line 266)
|
||||
// Step 1: Click menu icon (Python line 266)
|
||||
// main_frame is #forwardFrame.content_frame returned from login
|
||||
await mainFrame.locator('i').first().click();
|
||||
|
||||
// Click discrete material plan menu item and expect popup (lines 267-271)
|
||||
// Step 2: Click discrete material plan menu item and expect popup (Python lines 267-271)
|
||||
const popupPromise = page.waitForEvent('popup');
|
||||
await mainFrame.getByTitle('离散备料计划维护', { exact: true }).first().click();
|
||||
const popupPage = await popupPromise;
|
||||
|
||||
// Get nested frame structure (lines 273-276)
|
||||
const forwardFrameLocator = popupPage.locator(ERP_LOCATORS.main.forwardFrame);
|
||||
const innerFrameLocator = forwardFrameLocator.frameLocator(ERP_LOCATORS.main.innerIframe);
|
||||
// Step 3 & 4: Get nested frame structure in popup window (Python lines 273-276)
|
||||
// popup page contains #forwardFrame, which contains #mainiframe
|
||||
const forwardFrameLocator = popupPage.locator('#forwardFrame');
|
||||
const fFrame = await forwardFrameLocator.contentFrame();
|
||||
|
||||
// Wait for inner iframe to be visible
|
||||
if (!fFrame) {
|
||||
throw new Error('Failed to access popup forward frame');
|
||||
}
|
||||
|
||||
const innerFrameLocator = fFrame.locator('#mainiframe');
|
||||
await innerFrameLocator.waitFor({ state: 'visible', timeout: 15000 });
|
||||
const workFrame = await innerFrameLocator.contentFrame();
|
||||
|
||||
// Setup query interface (line 278)
|
||||
await this.setupQueryInterface(innerFrameLocator);
|
||||
if (!workFrame) {
|
||||
throw new Error('Failed to access inner work frame');
|
||||
}
|
||||
|
||||
return popupPage;
|
||||
// Step 5: Setup query interface (Python line 278)
|
||||
await this.setupQueryInterface(workFrame);
|
||||
|
||||
return { popupPage, workFrame };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,17 +121,17 @@ export class ExtractorService {
|
||||
* Reference: Python setup_query_interface() method lines 231-239
|
||||
*/
|
||||
private async setupQueryInterface(innerFrame: any): Promise<void> {
|
||||
// Click search icon (line 233)
|
||||
await innerFrame.locator(ERP_LOCATORS.menu.searchIcon).click();
|
||||
// Click search icon (Python line 233)
|
||||
await innerFrame.locator('.search-name-wrapper > .iconfont').click();
|
||||
|
||||
// Click "订单号查询" menu item (line 234)
|
||||
await innerFrame.locator(ERP_LOCATORS.menu.orderQuery).click();
|
||||
// Click "订单号查询" menu item (Python line 234)
|
||||
await innerFrame.getByText('订单号查询').click();
|
||||
|
||||
// Click "全部" tab (line 235)
|
||||
// Click "全部" tab (Python line 235)
|
||||
await innerFrame.getByRole('tab', { name: '全部' }).click();
|
||||
|
||||
// Set limit to 5000 (lines 237-239)
|
||||
const inputBox = innerFrame.locator(ERP_LOCATORS.menu.selectInput);
|
||||
// Set limit to 5000 (Python lines 237-239)
|
||||
const inputBox = innerFrame.locator('#rc_select_0');
|
||||
await inputBox.fill('5000');
|
||||
await inputBox.press('Enter');
|
||||
}
|
||||
@@ -131,42 +143,37 @@ export class ExtractorService {
|
||||
private async downloadBatch(
|
||||
session: ErpSession,
|
||||
popupPage: any,
|
||||
workFrame: any,
|
||||
orderNumbers: string[],
|
||||
batchIndex: number,
|
||||
totalBatches: number
|
||||
): Promise<string> {
|
||||
const forwardFrameLocator = popupPage.locator(ERP_LOCATORS.main.forwardFrame);
|
||||
const workFrame = forwardFrameLocator.frameLocator(ERP_LOCATORS.main.innerIframe);
|
||||
|
||||
// Fill order numbers (lines 143-145)
|
||||
const textbox = workFrame.getByRole('textbox', { name: ERP_LOCATORS.extractor.orderNumberInputRole });
|
||||
// Fill order numbers (Python lines 143-145)
|
||||
const textbox = workFrame.getByRole('textbox', { name: '来源生产订单号' });
|
||||
await textbox.fill('');
|
||||
await textbox.fill(orderNumbers.join(','));
|
||||
|
||||
// Click search button (line 147)
|
||||
await workFrame.locator(ERP_LOCATORS.extractor.queryButton).click();
|
||||
// Click search button (Python line 147)
|
||||
await workFrame.locator('.search-component-searchBtn').click();
|
||||
|
||||
// Wait for loading (lines 148-153)
|
||||
// Wait for loading (Python lines 148-153)
|
||||
await this.waitForLoading(workFrame);
|
||||
|
||||
// Click first row checkbox (line 155)
|
||||
await workFrame
|
||||
.locator(ERP_LOCATORS.extractor.firstRowSelector)
|
||||
.getByLabel('')
|
||||
.click();
|
||||
// Click first row checkbox (Python line 155)
|
||||
await workFrame.getByRole('row', { name: '序号' }).getByLabel('').click();
|
||||
|
||||
// Hover and click "更多" button (lines 156-157)
|
||||
// Hover and click "更多" button (Python lines 156-157)
|
||||
await workFrame.getByRole('button', { name: '更多' }).hover();
|
||||
await workFrame.getByText('输出', { exact: true }).click();
|
||||
|
||||
// Set threshold (lines 159-164)
|
||||
// Set threshold (Python lines 159-164)
|
||||
const thresholdBox = workFrame
|
||||
.locator('div')
|
||||
.filter({ hasText: /^行数阈值$/ })
|
||||
.locator('input[type="text"]');
|
||||
await thresholdBox.fill('300000');
|
||||
|
||||
// Setup download handler and click confirm (lines 166-172)
|
||||
// Setup download handler and click confirm (Python lines 166-172)
|
||||
const downloadPath = path.join(
|
||||
this.downloadDir,
|
||||
`temp_batch_${batchIndex + 1}.xlsx`
|
||||
|
||||
Reference in New Issue
Block a user