gulp命令获取参数 [英] gulp command to take parameters

查看:534
本文介绍了gulp命令获取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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

问题


  1. 如何从npm传递这些值以使gulp理解

  2. 如果我传递给gulp e2e --server staging等其他值,并希望在我的规范文件中使用变量staging,如

  1. How do I pass these values from npm to make gulp understand
  2. 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 {
//执行此操作
}其他{
//执行
}

if server=== staging{ // do this } else { // do that }

我应该如何将它们从gulp文件传递到我的spec文件?

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命令获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆