如何使用玩笑将命令行参数或环境变量从批处理脚本传递给 frisby 脚本

How to pass command line argument or environment variable to frisby scripts from batch script using jest

在 cmd 中 运行ning 时不带参数。基本上,它将参数作为参数并在整个文件中搜索该参数,并且 运行 如果有与该参数匹配的所有文件,则不限于特定的文件。

类似的文件是

1.login_account_spec.js

代码:

    var frisby = require("frisby");
    const Joi = frisby.Joi;

   console.log(process.argv)

    /**
    * Commandline parameter includes
    */
    /*  
    var name = process.env['name'];
    var password = process.env['password'];
    var dateTime = record.getDateTime();

    frisby.globalSetup({
        request: { 
            headers : { 
                    "accept": "application/json", 
                    "content-type" : "application/json", 
            }
        }
    });

    it("Test login_account" + dateTime, function (done){
        frisby.get(url)
        .expect("status", 200)
        .expect("header", "content-type", "application/json; charset=utf-8")
        .expect("jsonTypes", "data", {
            "message" : Joi.string(),
            })
        .then(function(res) {
            var body = res.body;
            body = JSON.parse(body);
            expect(body.data.message).toBeDefined();

        })
        .then(function(res) {
            record.createLogFile("login_account" + dateTime, null, res);
        })
        .done(done);
    }); 

2.get_device_details_spec.js

代码:

 var frisby = require("frisby");
 const Joi = frisby.Joi;

    console.log(process.argv)


    frisby.globalSetup({
        request: { 
            headers : { 
                "Accept": "application/json", 
                "content-type" : "application/json" 
            }
        }
    });

    it("Test get_device_details" + dateTime, function(done){
        frisby.get(url)
        .expect("status", 200)
        .expect("header", "content-type", "application/json; charset=utf-8")
        .expect("jsonTypes", "data", {
            "description": Joi.string(),
        })
            .then(function(res) {
                var body = res.body;
                body = JSON.parse(body);
                expect(body.data.description).toBeDefined();
            })  
        .after(function(res) {
            record.createLogFile("get_device_details" + dateTime, err, res);
        })
        .done(done);
});

Unit_test.bat 包含脚本(这里只有两个)

@echo off
set URL=%1
set NAME=%2
set PASSWORD=%3

set /a var2=1
set /a var=0
set /a varx=0
IF /I "%ITERATION%" NEQ "0" (
    SET var2=%ITERATION%
    SET /a ITERATION=1
)

for /L %%n in (1,%ITERATION%,%var2%) do @(

    jest login_account_spec.js %URL% %NAME% %PASSWORD% 

    REM jest get_device_details_spec.js %URL%  %NAME% %PASSWORD% 

)

在CMD中:

Unit_Test.bat 192.168.1.17:XXXX 测试 123

然后它给出了这两个文件的结果,这些 js 文件与 Frisby 有关。

所以我只想 运行(login_account_spec.js) 那个文件,其中包含我在 cmd 中传递的参数。但是,它给出了我不想要的两个脚本的结果,也没有采用我传递的参数。

.bat文件

@echo off
set URL=%1
set HTTPMODE=%2
set USERNAME=%3
set PASSWORD=%4
set ENCRYPTION=%5
set ITERATION=%6
set COMPANYID=%7

REM call current_time.bat   
        REM set config={"httpMode": "%HTTPMODE%","url": "%URL%","encryption": "%ENCRYPTION%","username": "%USERNAME%@gmail.com","password": "%PASSWORD%","companyId": "%COMPANYID%","role": "Owner"}

在 .js 文件中:

var objConfig = JSON.parse(process.env.config);

var username = objConfig.username;
var encryption = objConfig.encryption;

最后 运行 在 cmd 上: xx.bat 用户名密码...

希望对其他人有所帮助