| 12345678910111213141516171819202122232425 |
- import {chromium} from 'playwright';
- import {mkdirSync,writeFileSync,readFileSync} from 'node:fs';
- import * as XLSX from 'xlsx';
- import assert from 'node:assert/strict';
- export async function checkLessonRecordsBrowser({auth,output,studentId=1857,memberObjectId}){
- mkdirSync(output,{recursive:true});const browser=await chromium.launch({headless:true,executablePath:'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'});const checks=[],errors=[];
- try{const context=await browser.newContext({viewport:{width:1440,height:1050}});await context.addInitScript(auth=>{localStorage.setItem('xiaoshu_admin_session_v1',JSON.stringify(auth));sessionStorage.setItem('xiaoshu_admin_session_v1',JSON.stringify(auth));},auth);
- const page=await context.newPage();page.on('pageerror',e=>errors.push(e.message));
- await page.goto('http://127.0.0.1:4300/admin/lessons?studentId='+studentId);await page.getByRole('button',{name:'查看反馈',exact:true}).first().waitFor({timeout:60000});
- assert.match(await page.locator('.record-count').innerText(),/累计已上课/);checks.push('集中查询入口、真实数据与累计课次');
- const total=Number((await page.locator('.record-count').innerText()).match(/当前筛选 (\d+) 节/)[1]);
- if(total>20){assert.equal(await page.locator('.lesson-table tbody tr').count(),20);const first=await page.locator('.lesson-table tbody tr').first().innerText();await page.getByRole('button',{name:'下一页',exact:true}).click();await page.waitForFunction(()=>document.querySelector('.lesson-pagination')?.textContent.includes('第 2 页')&&!document.querySelector('[role=status]'));assert.notEqual(await page.locator('.lesson-table tbody tr').first().innerText(),first);checks.push('超过一页的真实记录可以翻页');}
- await page.getByRole('button',{name:'查看该学员全部结果'}).click();await page.waitForFunction(n=>document.querySelectorAll('.lesson-table tbody tr').length===n&&!document.querySelector('[role=status]'),total,{timeout:60000});
- const dates=await page.locator('.lesson-table tbody tr td:nth-child(2)').allTextContents();assert.deepEqual(dates,[...dates].sort().reverse());checks.push('全部历史行数与累计一致、北京时间倒序');
- await page.screenshot({path:output+'/history-desktop.png',fullPage:true});
- const downloadPromise=page.waitForEvent('download');await page.getByRole('button',{name:'导出 Excel',exact:true}).click();const download=await downloadPromise;assert.match(download.suggestedFilename(),/上课记录.*xlsx/);await download.saveAs(output+'/lesson-records.xlsx');const book=XLSX.read(readFileSync(output+'/lesson-records.xlsx'));assert.equal(XLSX.utils.sheet_to_json(book.Sheets[book.SheetNames[0]]).length,total);checks.push('真实筛选结果导出 Excel,行数与课次一致');
- await page.getByRole('button',{name:'查看反馈',exact:true}).first().click();await page.getByRole('dialog',{name:'上课记录详情'}).waitFor();await page.getByRole('heading',{name:'老师点评',exact:true}).waitFor({timeout:60000}).catch(async e=>{await page.screenshot({path:output+'/detail-error.png',fullPage:true});throw Error((await page.getByRole('dialog').innerText())+' '+e.message);});
- await page.getByRole('button',{name:'更换抗遗忘陪练'}).click();await page.locator('.transfer-panel select').last().locator('option').nth(1).waitFor({timeout:60000,state:'attached'});
- assert.ok(await page.locator('.transfer-panel select').last().locator('option').count()>1);checks.push('完整反馈详情与有效陪练选择;未提交真实转交');
- await page.screenshot({path:output+'/feedback-desktop.png',fullPage:true});await page.getByRole('button',{name:'关闭',exact:true}).click();
- await page.setViewportSize({width:390,height:844});await page.screenshot({path:output+'/history-mobile-width.png',fullPage:true});assert.ok(await page.evaluate(()=>document.documentElement.scrollWidth<=window.innerWidth+1));checks.push('窄屏无整页横向溢出');
- if(memberObjectId){await page.setViewportSize({width:1440,height:1050});await page.goto('http://127.0.0.1:4300/admin/members/'+memberObjectId);await page.getByRole('button',{name:'上课记录',exact:true}).click();await page.locator('app-lesson-records .data-table tbody tr').first().waitFor();assert.equal(await page.locator('nav.pagination').count(),0);checks.push('会员详情复用同一列表且不重复分页');}
- assert.deepEqual(errors,[]);writeFileSync(output+'/browser-result.json',JSON.stringify({passed:true,checks},null,2));return{passed:true,checks};
- }finally{await browser.close();}
- }
|