测试脚本 const phantom = require(‘phantom‘ );const fs =require(‘fs‘ );const mkdir =require(‘./utils/mkdir‘ );const path =require(‘path‘ );const configFiles = [ ‘./pages/login.js‘ , ‘./pages/credit.js‘ , ‘./page
测试脚本
const phantom = require(‘phantom‘); const fs=require(‘fs‘); const mkdir=require(‘./utils/mkdir‘); const path=require(‘path‘); const configFiles=[ ‘./pages/login.js‘, ‘./pages/credit.js‘, ‘./pages/xyhset.js‘, ] const dirdist=__dirname+‘/dist/cookies/‘; mkdir(dirdist) const configs=configFiles.map(function (path1) { const conf=require(path1) conf.dir=__dirname+‘/dist/‘+path.basename(path1,‘.js‘)+‘/‘; return conf; }) var page,instance; async function openUrl() { for(let i=0;i<configs.length;i++){ await runTestConfig(configs[i]); } } const Sleep=function (time) { return new Promise(function (resolve) { setTimeout(resolve,time) }) }; async function runTestConfig(testConfig) { page.property(‘viewportSize‘,testConfig.viewportSize); page.setting(‘userAgent‘,testConfig.userAgent); //获取cookie缓存 let cookies=[] if(fs.existsSync(dirdist+testConfig.precookies)){ cookies=JSON.parse(fs.readFileSync(dirdist+testConfig.precookies).toString()); } await page.property(‘cookies‘,cookies); // page.on(‘onConsoleMessage‘,function(msg, lineNum, sourceId) { // console.log(‘CONSOLE: ‘ + msg + ‘ (from line #‘ + lineNum + ‘ in "‘ + sourceId + ‘")‘); // }); console.log(‘openUrl‘,testConfig.url) const status = await page.open(testConfig.url); try { for(let i=0;i<testConfig.actions.length;i++){ const action=testConfig.actions[i]; const key =action[0]; const logs=action.filter(function (item) { return typeof item===‘string‘; }) if(key===‘click‘){ const selector=action[1] const rect=await page.evaluate(function(selector) { return document.querySelector(selector).getBoundingClientRect(); },selector) page.sendEvent(‘click‘, (rect.left+rect.right)/2,(rect.top+rect.bottom)/2); }else if(key===‘keypress‘){ page.sendEvent(‘keypress‘, action[1]); }else if(key===‘sleep‘){ const val=action[1] await Sleep(val) }else if(key===‘render‘){ const val=action[1] await page.render(testConfig.dir+val) }else if(key===‘log‘){ const val=action[1] const res=await page.evaluate(val) if(typeof res==‘object‘){ if(res.log){ logs.push(res.log); } if(!res.success){ console.log(logs); break; } } console.log(logs); }else if(key===‘goBack‘){ await page.goBack() } } //缓存cookie cookies=await page.cookies(); fs.writeFileSync(dirdist+testConfig.postcookies,JSON.stringify(cookies)) }catch (e) { await page.render(‘error.png‘) } // const content = await page.property(‘content‘); // console.log(content) } (async function(){ instance = await phantom.create(); page = await instance.createPage(); await openUrl() await instance.exit(); })()
//credit.js
const test={ //页面前cookie precookies:‘postlogin.cookie‘, //页面后cookie postcookies:‘postlogin.cookie‘, //页面大小 viewportSize:{ width: ‘375‘, height: ‘667‘ }, //设备 userAgent:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36‘, url:‘http://stg1-lps-web.daikuan.360.cn/h5xyh/credit.html‘, actions:[ [‘log‘, function () { const obj={ success:true, log:‘已登录,credit.html,测试开始‘ }; if(location.href.indexOf(‘/credit.html‘)===-1){ obj.success=false; obj.log=‘未登录,请登录‘; } return obj; }],//判断是否是credit页 [‘render‘,‘credit.png‘,],//截图 [‘click‘,‘#app > div > div > div.am-list.btn-list.f30.c333.mt20 > div > div:nth-child(1)‘],//点击帮助中心 [‘sleep‘, 2000 ], [‘log‘,function () { const obj={ success:true, log:‘进入帮助中心页面‘ }; if(location.href.indexOf(‘/help.html‘)===-1){ obj.success=false; obj.log=‘进入帮助中心页面,失败‘; } return obj; }],//判断是否是帮助 [‘render‘,‘help.png‘,],//截图 [‘scrollPosition‘,{ top: 100, left: 0 }], [‘render‘,‘help2.png‘,],//截图 [‘goBack‘],//回退 [‘log‘,function () { const obj={ success:true, log:‘回到credit.html‘ }; if(location.href.indexOf(‘/credit.html‘)===-1){ obj.success=false; obj.log=‘回到credit.html,失败‘; } return obj; }],//判断是否是credit页 [‘click‘,‘#app > div > div > div.am-list.btn-list.f30.c333.mt20 > div > div:nth-child(2)‘],//点击设置 [‘sleep‘, 2000 ], [‘log‘,function () { const obj={ success:true, log:‘进入设置‘ }; if(location.href.indexOf(‘/xyhset.html‘)===-1){ obj.success=false; obj.log=‘进入设置,失败‘; } return obj; }],//判断是否是设置 [‘render‘,‘xyhset.png‘,],//截图 [‘goBack‘],//回退 [‘log‘,function () { const obj={ success:true, log:‘回到credit.html‘ }; if(location.href.indexOf(‘/credit.html‘)===-1){ obj.success=false; obj.log=‘回到credit.html,失败‘; } return obj; }],//判断是否是credit页 ] }; module.exports=test;
//xyhset.js
const test={ //页面前cookie precookies:‘postlogin.cookie‘, //页面后cookie postcookies:‘prelogin.cookie‘, //页面大小 viewportSize:{ width: ‘375‘, height: ‘667‘ }, //设备 userAgent:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36‘, url:‘http://stg1-lps-web.daikuan.360.cn/h5xyh/xyhset.html‘, actions:[ [‘log‘, function () { const obj={ success:true, log:‘已登录,设置页,测试开始‘ }; if(location.href.indexOf(‘/xyhset.html‘)===-1){ obj.success=false; obj.log=‘未登录,请登录‘; } return obj; }],//判断是否是设置页 [‘render‘,‘xyhset.png‘,],//截图 [‘click‘,‘#app > div > div > div:nth-child(3) > div > div:nth-child(1)‘],//点击微信公众号 [‘sleep‘, 2000 ], [‘render‘,‘xyhset-dialog.png‘,],//截图 [‘click‘,‘div.am-modal-wrap > div > div > div.am-modal-footer > div > a‘],//点击弹框我知道了 [‘sleep‘, 2000 ], [‘render‘,‘xyhset2.png‘,],//截图 [‘click‘,‘#app > div > div > div:nth-child(3) > div > div:nth-child(2)‘],//点击产品反馈 [‘sleep‘, 2000 ], [‘render‘,‘feedback.png‘,],//截图 [‘goBack‘],//回退 [‘click‘,‘#app > div > div > a‘],//安全退出 [‘sleep‘, 2000 ], [‘render‘,‘xyhset3.png‘,],//截图 [‘click‘,‘div.am-modal-wrap > div > div > div.am-modal-footer > div > a:nth-child(2)‘],//立即退出 [‘sleep‘, 2000 ], [‘render‘,‘login.png‘,],//截图 [‘log‘, function () { const obj={ success:true, log:‘退出登录成功,跳转‘+location.href }; if(location.href.indexOf(‘/login.html‘)===-1){ obj.success=false; obj.log=‘退出登录失败‘; } return obj; }],//判断是否是登录页 ] }; module.exports=test;