119 lines
4.9 KiB
JavaScript
119 lines
4.9 KiB
JavaScript
|
|
const fs = require('node:fs')
|
||
|
|
const path = require('node:path')
|
||
|
|
const { spawn } = require('node:child_process')
|
||
|
|
|
||
|
|
const root = path.resolve(__dirname, '../..')
|
||
|
|
const chromePath = 'C:/Program Files/Google/Chrome/Application/chrome.exe'
|
||
|
|
const outputDir = path.join(root, '.codex-tmp', 'ui-render-check')
|
||
|
|
const profileDir = path.join(outputDir, `profile-${Date.now()}`)
|
||
|
|
const port = 9333
|
||
|
|
|
||
|
|
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
|
||
|
|
|
||
|
|
async function waitJson(url, timeout = 15000) {
|
||
|
|
const deadline = Date.now() + timeout
|
||
|
|
while (Date.now() < deadline) {
|
||
|
|
try { return await (await fetch(url)).json() } catch (_) { await delay(250) }
|
||
|
|
}
|
||
|
|
throw new Error(`Timed out waiting for ${url}`)
|
||
|
|
}
|
||
|
|
|
||
|
|
async function connectCdp(url) {
|
||
|
|
const socket = new WebSocket(url)
|
||
|
|
const pending = new Map()
|
||
|
|
let id = 0
|
||
|
|
await new Promise((resolve, reject) => {
|
||
|
|
socket.addEventListener('open', resolve, { once: true })
|
||
|
|
socket.addEventListener('error', reject, { once: true })
|
||
|
|
})
|
||
|
|
socket.addEventListener('message', event => {
|
||
|
|
const message = JSON.parse(event.data)
|
||
|
|
if (!message.id || !pending.has(message.id)) return
|
||
|
|
const { resolve, reject } = pending.get(message.id)
|
||
|
|
pending.delete(message.id)
|
||
|
|
if (message.error) reject(new Error(message.error.message))
|
||
|
|
else resolve(message.result)
|
||
|
|
})
|
||
|
|
return {
|
||
|
|
send(method, params = {}) {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
const messageId = ++id
|
||
|
|
pending.set(messageId, { resolve, reject })
|
||
|
|
socket.send(JSON.stringify({ id: messageId, method, params }))
|
||
|
|
})
|
||
|
|
},
|
||
|
|
close() { socket.close() }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function waitReady(cdp, expectedText) {
|
||
|
|
const deadline = Date.now() + 20000
|
||
|
|
let lastText = ''
|
||
|
|
while (Date.now() < deadline) {
|
||
|
|
const result = await cdp.send('Runtime.evaluate', { expression: `document.readyState + '|' + document.body.innerText`, returnByValue: true })
|
||
|
|
const text = result.result && result.result.value || ''
|
||
|
|
lastText = text
|
||
|
|
if (text.startsWith('complete|') && (!expectedText || text.includes(expectedText))) return text
|
||
|
|
await delay(350)
|
||
|
|
}
|
||
|
|
const location = await cdp.send('Runtime.evaluate', { expression: 'location.href', returnByValue: true })
|
||
|
|
throw new Error(`Page did not render expected text: ${expectedText}; url=${location.result && location.result.value}; body=${lastText.slice(0, 500)}`)
|
||
|
|
}
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
fs.mkdirSync(outputDir, { recursive: true })
|
||
|
|
const loginResponse = await fetch('http://127.0.0.1:8080/login', {
|
||
|
|
method: 'POST',
|
||
|
|
headers: { 'content-type': 'application/json' },
|
||
|
|
body: JSON.stringify({ username: 'admin', password: 'admin123', code: '', uuid: '' })
|
||
|
|
})
|
||
|
|
const login = await loginResponse.json()
|
||
|
|
if (login.code !== 200 || !login.token) throw new Error(`Login failed: ${login.msg || login.code}`)
|
||
|
|
|
||
|
|
const chrome = spawn(chromePath, [
|
||
|
|
'--headless=new', `--remote-debugging-port=${port}`, `--user-data-dir=${profileDir}`,
|
||
|
|
'--window-size=1600,1000', '--hide-scrollbars', '--disable-gpu', '--no-first-run',
|
||
|
|
'http://127.0.0.1:8088/login'
|
||
|
|
], { stdio: 'ignore' })
|
||
|
|
try {
|
||
|
|
await waitJson(`http://127.0.0.1:${port}/json/version`)
|
||
|
|
const targets = await waitJson(`http://127.0.0.1:${port}/json`)
|
||
|
|
const page = targets.find(item => item.type === 'page')
|
||
|
|
if (!page) throw new Error('No Chrome page target found')
|
||
|
|
const cdp = await connectCdp(page.webSocketDebuggerUrl)
|
||
|
|
try {
|
||
|
|
await cdp.send('Page.enable')
|
||
|
|
await cdp.send('Network.enable')
|
||
|
|
await cdp.send('Runtime.enable')
|
||
|
|
await waitReady(cdp)
|
||
|
|
const cookie = await cdp.send('Network.setCookie', { name: 'Admin-Token', value: login.token, url: 'http://127.0.0.1:8088/' })
|
||
|
|
if (!cookie.success) throw new Error('Failed to install authenticated browser session')
|
||
|
|
const pages = [
|
||
|
|
['project', '/project/info', '项目列表'],
|
||
|
|
['purchase', '/procurement/details', '采购明细'],
|
||
|
|
['arrival', '/procurement/arrival', '到货验收'],
|
||
|
|
['stock', '/warehouse/stock', '库存查询'],
|
||
|
|
['production', '/production/tasks', '生产任务'],
|
||
|
|
['loans', '/warehouse/loans', '借用与归还'],
|
||
|
|
['items', '/businessBasics/product', '物品档案'],
|
||
|
|
['users', '/system/user', '用户管理']
|
||
|
|
]
|
||
|
|
for (const [name, route, text] of pages) {
|
||
|
|
await cdp.send('Page.navigate', { url: `http://127.0.0.1:8088${route}` })
|
||
|
|
await waitReady(cdp, text)
|
||
|
|
await delay(500)
|
||
|
|
const screenshot = await cdp.send('Page.captureScreenshot', { format: 'png', captureBeyondViewport: false })
|
||
|
|
fs.writeFileSync(path.join(outputDir, `${name}.png`), Buffer.from(screenshot.data, 'base64'))
|
||
|
|
}
|
||
|
|
cdp.close()
|
||
|
|
} finally {
|
||
|
|
chrome.kill()
|
||
|
|
}
|
||
|
|
} finally {
|
||
|
|
if (!chrome.killed) chrome.kill()
|
||
|
|
}
|
||
|
|
console.log(`PASS: rendered 8 authenticated pages to ${outputDir}`)
|
||
|
|
}
|
||
|
|
|
||
|
|
main().catch(error => { console.error(error); process.exitCode = 1 })
|