bootstrap.js 1.7 KB
const wd = require('wd');
const open = require('open');
const chalk = require('chalk');
const caseLayout = require('./case-layout');
const logger = require('debug')('tester');
const axios = require('axios');
const config = require('../config.json');

const post = (url, data) => {
  return axios({
    url,
    method: 'POST',
    data
  })
}

const driverWork = async(config, scenes) => {
  let driver
  for (let si = 0; si < scenes.length; si++) {
    const {scene, title, driverFn} = scenes[si];

    logger(chalk.yellow(`  ${si + 1}). 测试场景:${title}`));
    await post('start-scene-case', {scene});
    if (si === 0) {
      driver = await wd.promiseChainRemote(config.serverConfig);
      await driver.init(config.caps);
    }
    try {
      await driverFn(driver);
    } catch (error) {
      logger(chalk.red(error.message));
    }
    if (si === scenes.length - 1) {
      await driver.quit();
    }
    const {data} = await post('stop-scene-case', {scene});

    if (data.code === 200) {
      logger(chalk.green(`埋点数据检查: ${scene}-成功,${data.data.overLogs.length}项上报数据未匹配`));
    } else {
      logger(chalk.red(`埋点数据检查: ${scene}, ${data.data.notValidates.length}项规则失败`));
    }
  }
  return true;
}


const invokeCases = async(cases) => {
  for (let ci = 0; ci < cases.length; ci++) {
    const {name, config, scenes} = cases[ci];

    logger(chalk.yellow(`${ci + 1}. 编排任务:${name} 正在开始...`));
    await driverWork(config, scenes);
  }
  logger(chalk.yellow('正在生成埋点测试报告...'));
  setTimeout(() => {
    open(`http://localhost:${config.port}/report`);
  }, 1000);
}

const cases = caseLayout.getCases();


invokeCases(cases);