如何使用yarn调试测试

时间:2018-10-15 09:40:01

标签: javascript node.js yarnpkg

我有许多用javascript编写的UI测试(使用硒和黄瓜)。目前,我正在按以下方式运行测试:

yarn run loginTest

有用于调试测试的命令吗?

更新: 以下是我的package.json的快照,其中包括loginTest脚本参考:

"scripts": {
    "loginTest": "node ./scripts/loginTest.js"
    ....
},

loginTest.js文件如下所示:

const chalk = require('chalk');
const path = require('path');
const fs = require('fs-extra');
const cp = require('child_process');

const srcPath = './projects/';

const projects = fs.readdirSync(srcPath)
    .filter(file => fs.lstatSync(path.join(srcPath, file)).isDirectory())
    .filter(file => file !== 'commons');

process.env.HOST = process.argv.slice(2)[0] || 'localhost';

console.log(chalk.magenta('Carrying out integration tests against: ') + process.env.HOST);

projects.forEach((project) => {
    if (fs.existsSync(`./projects/${project}/login-tests/features/`)) {
        console.log(chalk.yellow('Starting integration tests for: ') + project);
        cp.fork(__dirname + '/runtime/pickle.js',
            [
                '-f', `./projects/${project}/login-tests/features/`,
                '-s', `./projects/${project}/login-tests/step-definitions/`,
                '-p', `./projects/${project}/login-tests/page-objects/`,
                '-r', `./build/reports/${project}`
            ]);
    }
});

上面显示的pickle.js文件显示如下:

#!/usr/bin / env node

'use strict';

const fs = require('fs-plus');
const path = require('path');
const program = require('commander');
const cucumber = require('cucumber');

function collectPaths(value, paths) {
    paths.push(value);
    return paths;
}

function coerceInt(value, defaultValue) {
    const int = parseInt(value);
    if (typeof int === 'number') return int;
    return defaultValue;
}

let config = {
    steps: './step-definitions',
    pageObjects: './page-objects',
    sharedObjects: './shared-objects',
    reports: './reports',
    browser: 'chrome',
    timeout: 15000
};

const configFileName = path.resolve(process.cwd(), 'selenium-cucumber-js.json');

if (fs.isFileSync(configFileName)) {
    config = Object.assign(config, require(configFileName));
}

program
    .option('-s, --steps <path>', 'path to step definitions. defaults to ' + config.steps, config.steps)
    .option('-p, --pageObjects <path>', 'path to page objects. defaults to ' + config.pageObjects, config.pageObjects)
    .option('-o, --sharedObjects [paths]', 'path to shared objects (repeatable). defaults to ' + config.sharedObjects, collectPaths, [config.sharedObjects])
    .option('-b, --browser <path>', 'name of browser to use. defaults to ' + config.browser, config.browser)
    .option('-r, --reports <path>', 'output path to save reports. defaults to ' + config.reports, config.reports)
    .option('-d, --disableLaunchReport [optional]', 'Disables the auto opening the browser with test report')
    .option('-j, --junit <path>', 'output path to save junit-report.xml defaults to ' + config.reports)
    .option('-t, --tags <tagName>', 'name of tag to run', collectPaths, [])
    .option('-f, --featureFiles <paths>', 'comma-separated list of feature files to run')
    .option('-x, --timeOut <n>', 'steps definition timeout in milliseconds. defaults to ' + config.timeout, coerceInt, config.timeout)
    .option('-n, --noScreenshot [optional]', 'disable auto capturing of screenshots when an error is encountered')
    .parse(process.argv);

program.on('--help', function () {
    console.log('  For more details please visit https://github.com/john-doherty/selenium-cucumber-js#readme\n');
});

// store browserName globally (used within world.js to build driver)
global.browserName = program.browser;

// store Eyes Api globally (used within world.js to set Eyes)
global.eyesKey = config.eye_key;

// used within world.js to import page objects
global.pageObjectPath = path.resolve(program.pageObjects);

// used within world.js to output reports
global.reportsPath = path.resolve(program.reports);
if (!fs.existsSync(program.reports)) {
    fs.makeTreeSync(program.reports);
}

// used within world.js to decide if reports should be generated
global.disableLaunchReport = (program.disableLaunchReport);

// used with world.js to determine if a screenshot should be captured on error
global.noScreenshot = (program.noScreenshot);

// used within world.js to output junit reports
global.junitPath = path.resolve(program.junit || program.reports);

// set the default timeout to 10 seconds if not already globally defined or passed via the command line
global.DEFAULT_TIMEOUT = global.DEFAULT_TIMEOUT || program.timeOut || 10 * 1000;

// used within world.js to import shared objects into the shared namespace
global.sharedObjectPaths = program.sharedObjects.map(function (item) {
    return path.resolve(item);
});

// rewrite command line switches for cucumber
process.argv.splice(2, 100);

// allow specific feature files to be executed
if (program.featureFiles) {
    var splitFeatureFiles = program.featureFiles.split(',');

    splitFeatureFiles.forEach(function (feature) {
        process.argv.push(feature);
    });
}

// add switch to tell cucumber to produce json report files
process.argv.push('-f');
process.argv.push('pretty');
process.argv.push('-f');
process.argv.push('json:' + path.resolve(__dirname, global.reportsPath, 'cucumber-report.json'));

// add cucumber world as first required script (this sets up the globals)
process.argv.push('-r');
process.argv.push(path.resolve(__dirname, 'world.js'));

// add path to import step definitions
process.argv.push('-r');
process.argv.push(path.resolve(program.steps));

// add tag
if (program.tags) {
    program.tags.forEach(function (tag) {
        process.argv.push('-t');
        process.argv.push(tag);
    });
}

// add strict option (fail if there are any undefined or pending steps)
process.argv.push('-S');

//
// execute cucumber
//
var cucumberCli = cucumber.Cli(process.argv);
global.cucumber = cucumber;
cucumberCli.run(function (succeeded) {

    var code = succeeded ? 0 : 1;

    function exitNow() {
        process.exit(code);
    }

    if (process.stdout.write('')) {
        exitNow();
    } else {
        // write() returned false, kernel buffer is not empty yet...
        process.stdout.on('drain', exitNow);
    }
}); 

0 个答案:

没有答案