batch-filecmdjasminejestjsfrisby.js

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


it's doesn't take argument when running in cmd. Basically, it takes the argument as a parameter and searches that argument throughout the file and runs all the files if anything matches to that argument, it's not limited to the particular one.

like files are

1.login_account_spec.js

code:

    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

code:

 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 that contain scripts(here only two)

@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% 

)

In CMD:

Unit_Test.bat 192.168.1.17:XXXX test1 123

then it gave the result of both these files and these js files are related to Frisby.

so I wants to run only(login_account_spec.js) that file with arguments that i passed in cmd . But, it gave result of both the scripts that, i don't want and it also not taking the arguments that i passed.


Solution

  • .bat file

    @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"}
    

    In .js file:

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

    And in last run on cmd: xx.bat username password ...

    hope it helps to other