37 lines
3.9 KiB
JavaScript
37 lines
3.9 KiB
JavaScript
// 在独立 Chrome 上核对规则入口和指标联动,接口写入使用测试替身。
|
|
const fs=require('node:fs/promises');const path=require('node:path');
|
|
const {chromium}=require(process.env.SW_PLAYWRIGHT_MODULE||'playwright');
|
|
(async()=>{
|
|
const token=(await fs.readFile(path.join(process.env.TEMP,'sw-kra-http-test-token'),'utf8')).trim();
|
|
const browser=await chromium.launch({channel:'chrome',headless:true});const results=[];
|
|
try{
|
|
for(const level of [0,1,2,3]){
|
|
const context=await browser.newContext({viewport:{width:1440,height:1000}});await context.addInitScript(t=>localStorage.setItem('token',t),token);
|
|
const page=await context.newPage();const errors=[];const writes=[];page.on('pageerror',e=>errors.push(e.message));
|
|
await page.route('http://127.0.0.1:8080/api/**',async route=>{
|
|
const req=route.request();const url=new URL(req.url());const api=url.pathname.slice(4);
|
|
if(api==='/sw/alertRule/list'){await route.fulfill({json:{code:0,msg:'成功',data:{total:1,rows:[{id:1,ruleName:'测试规则',ruleType:'测试分类',ruleTypeLabel:'测试分类',monitorMetric:1,statPeriodDays:7,momDirection:2,momThreshold:20,warningColor:'#f56c6c',creatorId:70001,creatorName:'测试一级商务',monitorScope:1,enabled:1,ruleDescription:'渠道近 7 天注册量较上一同期环比下降 20% 时触发'}]}}});return;}
|
|
if(api.startsWith('/sw/alertRule/')&&req.method()!=='GET'){writes.push({path:api,body:req.postDataJSON()});await route.fulfill({json:{code:0,msg:'成功',data:{id:2}}});return;}
|
|
const response=await route.fetch({url:'http://127.0.0.1:18000'+api+url.search,headers:{...req.headers(),'x-token':token}});
|
|
if(api==='/user/getUserInfo'){const body=await response.json();Object.assign(body.data.userInfo,{ID:70000+level,level,nickName:'页面核对账号',phone:'',email:'',authorityId:level===0?888:900+level});await route.fulfill({response,json:body});}
|
|
else await route.fulfill({response});
|
|
});
|
|
await page.goto('http://127.0.0.1:8080/#/layout/sw-AlertCenter/AlertRules');await page.getByText('测试规则',{exact:true}).waitFor();
|
|
const create=await page.getByRole('button',{name:'新建规则',exact:true}).count();const edit=await page.getByRole('button',{name:'编辑',exact:true}).count();
|
|
const allowed=level===0||level===1;if(create!==(allowed?1:0)||edit!==(allowed?1:0))throw new Error('规则写入口权限错误 level='+level);
|
|
const result={level,createButton:create,editButton:edit,passed:true};
|
|
if(level===1){
|
|
await page.getByRole('button',{name:'新建规则',exact:true}).click();const dialog=page.getByRole('dialog',{name:'新建规则',exact:true});
|
|
await dialog.getByPlaceholder('请输入规则名称').fill('登录测试');await dialog.getByPlaceholder('如:注册异常 / 充值异常 / 登录异常').fill('自定义类型');
|
|
await dialog.locator('.el-form-item').filter({hasText:'监控指标'}).locator('.el-select__wrapper').click();await page.getByRole('option',{name:'登录天数',exact:true}).click();
|
|
if(await dialog.getByText('统计周期(天)',{exact:true}).count())throw new Error('登录指标仍显示环比字段');
|
|
await dialog.getByText('未登录天数',{exact:true}).waitFor();await dialog.getByRole('button',{name:'保存',exact:true}).click();await dialog.waitFor({state:'hidden'});
|
|
const saved=writes.find(x=>x.path==='/sw/alertRule/add');if(!saved||saved.body.monitorMetric!==3||saved.body.unloginDays!==7)throw new Error('规则联动表单参数错误');result.metricFields=true;
|
|
}
|
|
result.pageErrors=errors;if(errors.length)throw new Error(errors.join(';'));
|
|
results.push(result);console.log(JSON.stringify(result));await context.close();
|
|
}
|
|
await fs.writeFile('docs/sw6-to-kra/alert-rules-page-2026-11-03.json',JSON.stringify({method:'真实 Chrome 渲染,受控角色及规则数据;不写真实规则',results},null,2));
|
|
}finally{await browser.close();}
|
|
})().catch(e=>{console.error(e.message);process.exitCode=1});
|