UNPKG

uirecorder

Version:
191 lines (166 loc) 5.43 kB
const fs = require('fs'); const path = require('path'); const cp = require('child_process'); const chai = require("chai"); const should = chai.should(); const JWebDriver = require('jwebdriver'); chai.use(JWebDriver.chaiSupportChainPromise); const resemble = require('resemblejs-node'); resemble.outputSettings({ errorType: 'flatDifferenceIntensity' }); const rootPath = getRootPath(); const appPath = '{$appPath}'; const platformName = '{$platformName}'; module.exports = function(){ var driver, testVars; before(function(){ var self = this; driver = self.driver; testVars = self.testVars; }); {$testCodes} function _(str){ if(typeof str === 'string'){ return str.replace(/\{\{(.+?)\}\}/g, function(all, key){ return testVars[key] || ''; }); } else{ return str; } } }; if(module.parent && /mocha\.js/.test(module.parent.id)){ runThisSpec(); } function runThisSpec(){ // read config let config = require(rootPath + '/config.json'); let webdriverConfig = Object.assign({},config.webdriver); let host = webdriverConfig.host; let port = webdriverConfig.port || 4444; let testVars = config.vars; let specName = path.relative(rootPath, __filename).replace(/\\/g,'/').replace(/\.js$/,''); let arrDeviceList = getEnvList() || getDeviceList(platformName); if(arrDeviceList.length ===0 ){ console.log('Search mobile device failed!'); process.exit(1); } arrDeviceList.forEach(function(device){ let caseName = specName + ' : ' + (device.name?device.name+' ['+device.udid+']':device.udid); describe(caseName, function(){ this.timeout(600000); this.slow(1000); before(function(){ let self = this; let driver = new JWebDriver({ 'host': host, 'port': port }); self.driver = driver.session({ 'platformName': platformName, 'udid': device.udid, 'app': /^(\/|[a-z]:\\|https?:\/\/)/i.test(appPath) ? appPath : rootPath + '/' + appPath }); self.testVars = testVars; let casePath = path.dirname(caseName); self.screenshotPath = rootPath + '/screenshots/' + casePath; self.diffbasePath = rootPath + '/diffbase/' + casePath; self.caseName = caseName.replace(/.*\//g, '').replace(/\s*[:\.\:\-\s]\s*/g, '_'); mkdirs(self.screenshotPath); mkdirs(self.diffbasePath); self.stepId = 0; return self.driver; }); module.exports(); beforeEach(function(){ let self = this; self.stepId ++; }); afterEach(async function(){ let self = this; let filepath = self.screenshotPath + '/' + self.caseName + '_' + self.stepId; let driver = self.driver; await driver.getScreenshot(filepath + '.png'); let json = await driver.source(); fs.writeFileSync(filepath + '.json', json); }); after(function(){ return this.driver.close(); }); }); }); } function getRootPath(){ let rootPath = path.resolve(__dirname); while(rootPath){ if(fs.existsSync(rootPath + '/config.json')){ break; } rootPath = rootPath.substring(0, rootPath.lastIndexOf(path.sep)); } return rootPath; } function mkdirs(dirname){ if(fs.existsSync(dirname)){ return true; }else{ if(mkdirs(path.dirname(dirname))){ fs.mkdirSync(dirname); return true; } } } function callSpec(name){ try{ require(rootPath + '/' + name)(); } catch(e){ console.log(e) process.exit(1); } } function getEnvList(){ let strEnvList = process.env.devices; if(strEnvList){ return strEnvList.split(/\s*,\s*/).map(function(udid){ return {udid: udid}; }); } } function getDeviceList(platformName){ let arrDeviceList = []; let strText, match; if(platformName === 'Android') { // for android strText = cp.execSync('adb devices').toString(); strText.replace(/(.+?)\s+device\r?\n/g, function(all, deviceName){ arrDeviceList.push({ udid: deviceName }); }); } else{ // ios real device strText = cp.execSync('idevice_id -l').toString(); strText.replace(/(.+)\r?\n/g, function(all, udid){ let deviceName = cp.execSync('idevice_id '+udid).toString(); deviceName = deviceName.replace(/\r?\n/g, ''); arrDeviceList.push({ name: deviceName, udid: udid }); }); // ios simulator strText = cp.execSync('xcrun simctl list devices').toString(); strText.replace(/\r?\n\s*(.+?)\s+\((.+?)\) \(Booted\)/g, function(all, deviceName, udid){ arrDeviceList.push({ name: deviceName, udid: udid }); }); } return arrDeviceList; }