問題描述
我的 package.json 有這樣的腳本
my package.json has scripts like this
{
"scripts": {
"pretest": "npm run tsc",
"test": "gulp e2e",
}
}
我們使用 typescript 和 webdriverIO 來實現自動化.我想使用 gulp 以便我可以將參數傳遞給我的測試框架.示例:
we use typescript and webdriverIO for automation. I want to use gulp so that i can pass parameters to my test framework. Example:
npm test --suite HomePageTests
那么與主頁相關的規范必須運行.
then the specs related to Home page must run.
我有這樣的 gulp 文件
I have the gulp file like this
// gulpfile.js
const gulp = require('gulp');
const Launcher = require('webdriverio/build/lib/launcher');
const wdio = new Launcher(path.join(__dirname,
'src/config/conf.ts'));
// fetch command line arguments
const arg = (argList => {
let arg = {}, a, opt, thisOpt, curOpt;
for (a = 0; a < argList.length; a++) {
thisOpt = argList[a].trim();
opt = thisOpt.replace(/^-+/, '');
if (opt === thisOpt) {
// argument value
if (curOpt) arg[curOpt] = opt;
curOpt = null;
}else {
// argument name
curOpt = opt;
arg[curOpt] = true;
}
}
console.log("arg", arg)
return arg;
})(process.argv);
gulp.task('e2e', () => {
return wdio.run(code => {
process.exit(code);
}, error => {
console.error('Launcher failed to start the test',error.stacktrace);
process.exit(1);
});
});
所以當我像直接調用 gulp 時
So when I call gulp directly like
gulp e2e --suite HomePageTests
它被打印為
suite: HomePageTests
但是如果我使用
npm test --suite HomePageTests
它在打印 gulp e2e HomePageTests
問題
- 如何從 npm 傳遞這些值以使 gulp 理解
如果我傳遞給另一個值,比如 gulp e2e --server staging,并且想在我的規范文件中使用變量staging",比如
- How do I pass these values from npm to make gulp understand
If I am pass to another value like gulp e2e --server staging and would like to use the variable "staging" in my spec file like
如果服務器=== 暫存
{//做這個} 別的 {//去做}
if server=== staging
{
// do this
} else {
// do that
}
我應該如何將它們從 gulp 文件傳遞??到我的規范文件?
How should I pass them from gulp file to my spec file?
謝謝!!
推薦答案
你可以使用 yargs 依賴
var argv = require('yargs').argv;
gulp.task('test', function(){
console.log(argv.arg);
});
那么如果你在一個 gulp 上運行一個命令,像這樣傳遞 arg
then if you run a command on a gulp passing the arg like this
gulp test --arg HomePageTests
它將在控制臺輸出HomePageTests
這篇關于獲取參數的 gulp 命令的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!