Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Cli util for e2e tests automation
- *
- * args:
- * --stable show latest CI stable failures instead of devel
- * --rows=xx number of cli rows
- * --rlwrap use rlwrap cli when invoking chimp (recommended, google rlwrap)
- */
- const fs = require('fs');
- const path = require('path');
- const childProcess = require('child_process');
- const glob = require('glob');
- const _ = require('lodash');
- const http = require('axios');
- const featuresPath = './specifications/flows';
- const jsonPath = '.reports/e2e-results.json';
- const Gherkin = require('gherkin')
- const parser = new Gherkin.Parser()
- const inquirer = require('inquirer');
- const minimist = require('minimist');
- const args = minimist(process.argv.slice(2));
- const colors = require('colors/safe');
- const Log = {
- silly(text) { console.log(colors.rainbow(text)) },
- input(text) { console.log(colors.grey(text)) },
- verbose(text) { console.log(colors.cyan(text)) },
- prompt(text) { console.log(colors.grey(text)) },
- info(text) { console.log(colors.green(text)) },
- data(text) { console.log(colors.grey(text)) },
- help(text) { console.log(colors.cyan(text)) },
- warn(text) { console.log(colors.yellow(text)) },
- debug(text) { console.log(colors.blue(text)) },
- error(text) { console.log(colors.red(text)) },
- }
- Log.silly('Test Runner')
- let develResults = []
- const updateCIResults = async () => {
- const auth = {
- username: process.env.BEN_BAMBOO_USERNAME,
- password: process.env.BEN_BAMBOO_PASSWORD,
- }
- if (!auth.username || !auth.password) {
- Log.warn('\nCI credentials not found')
- Log.warn('set BEN_BAMBOO_USERNAME and BEN_BAMBOO_PASSWORD env vars to show latest CI failures\n')
- await new Promise(resolve => setTimeout(resolve, 3000));
- } else {
- const env = args.stable ? 'BNR-E2E30-TES' : 'BNR-E2E-TES'
- const url = `https://bamboo.bengroup.com/rest/api/latest/result/${env}/latest.json`
- Log.data(`Fetching latest ${args.stable ? 'stable' : 'devel'} results..`)
- try {
- const res = await http.get(url, { auth, params: { expand: 'testResults.failedTests' }})
- develResults = _.get(res, 'data.testResults.failedTests.testResult') || []
- develResults.forEach(r => {
- const split = r.className.split(';');
- r.featureName = _.kebabCase(split[0]);
- r.scenarioName = _.kebabCase(split[1]);
- });
- } catch(e) {
- Log.error('Failed to get latest devel results')
- }
- }
- }
- const getFeatures = () => glob
- .sync(path.join(featuresPath, '**/*.feature'))
- .map(path => ({
- path,
- parsed: parser.parse(fs.readFileSync(path).toString())
- }))
- /**
- * Main loop, lists all tests and asks user which test to run
- */
- let lastSpec // reemember last feature/scenario executed
- const main = async () => {
- // read features list from local specifications dir
- const features = getFeatures();
- if (!features || !features.length) {
- throw new Error('No specifications found')
- }
- // create inquirer tests list from features/scenarios
- const specs = features.reduce((acc, doc) => {
- const feature = doc.parsed.feature
- return acc
- .concat({
- name: ` ${colors.bold((acc.length+1)
- + ' ' + _.kebabCase(feature.name))}`,
- value: doc.path,
- })
- .concat(feature.children
- .filter(children => children.type === 'Scenario')
- .map((scenario, idx) => {
- const featureName = _.kebabCase(feature.name.toLowerCase());
- const scenarioName = _.kebabCase(scenario.name.toLowerCase());
- const failed = !!develResults.find(r =>
- r.featureName === featureName && r.scenarioName === scenarioName);
- const index = acc.length + idx + 1
- return {
- name: `${failed ? colors.red('X') : ' '} ${index+1} ${featureName}/${scenarioName}`,
- value: `${doc.path}:${scenario.location.line}`
- }
- })
- )
- }, [])
- // prompt to select test from tests list
- const spec = await inquirer.prompt([{
- type: 'list',
- name: 'value',
- message: `Select scenario or feature spec`,
- choices: specs,
- default: lastSpec || 0,
- pageSize: args.rows || 13
- }])
- .then(res => specs.find(s => s.value === res.value))
- // reemember last selected spec when asking which feature to run
- lastSpec = specs.indexOf(spec)
- // prompt action after selecting a test
- await inquirer.prompt([{
- type: 'list',
- name: 'value',
- message: 'Select action',
- default: args.local ? 2 : args.stable ? 1 : 0,
- choices: [{
- name: 'Test Devel',
- value: () => runTest(spec, 'https://devel.goben.rocks/')
- }, {
- name: 'Test Stable',
- value: () => runTest(spec, 'https://stable.goben.rocks/')
- }, {
- name: 'Test Local',
- value: () => runTest(spec, 'http://localhost:4000/')
- }, {
- name: 'Open in VSCode',
- value: () => openVSCode(spec)
- }, {
- name: 'cancel',
- value: () => {}
- }]
- }])
- .then(res => res.value())
- // loop main
- await main()
- }
- updateCIResults()
- .then(main)
- .catch(console.error);
- // -------------------
- // COMMANDS
- // -------------------
- const runTest = (spec, baseUrl) => {
- const command = [
- `node_modules/.bin/chimp chimp.config.js`,
- `${process.argv.slice(3).join(' ')}`,
- `--debugOnError`,
- `--timeout=15000`,
- `--baseUrl=` + baseUrl,
- `--jsonOutput=${jsonPath}`,
- `"${spec.value}"`,
- ]
- if (args.rlwrap) {
- command.unshift('rlwrap');
- }
- try {
- childProcess.execSync(command.join(' '), { stdio: 'inherit' });
- } catch (err) {
- Log.error(err)
- }
- }
- const openVSCode = (spec) =>
- childProcess.execSync(`code -g "${spec.value}"`, { stdio: 'inherit' });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement