Forum Discussion

nastester's avatar
nastester
Regular Contributor
2 years ago

Best way to handle bat execution within TC

I have several use cases where executing a batch script before executing TC tests would be very beneficial.

This seems simple but the solutions I've tried have started the bat file and then TC marks the task as completed prompting the first test in the suite to start before the bat is actually done executing. 

Does anyone have any solutions for this? 

Can I pass in some command line parameter in TestedApps to keep the bat open until it finishes? 

  • My solution for this was just writing a custom script that runs in the execution plan:

     

    function runBatchFile(batchFilePath) {

    try {

    var process = Sys.OleObject("WScript.Shell");
    process.Run('cmd.exe /C "' + batchFilePath + '"', 1, true);

    while (process.Status == 0) {
    Delay(100);
    }
    Log.Message("Batch file executed")
    }
    catch (e) {
    Log.Error("An error occurred while running the batch file: " + e.message);
    }
    }

    var batchFilePath = "D:\\SmokeTestCleanup.bat";
    runBatchFile(batchFilePath);

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Within your batch file, you need something like,

     

    START /B /WAIT

     

    Where, /B to stay in the same process and /WAIT to wait until the command has finished.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Just for clarification, are you running a batch file from TC in TestedApps? What does your batch file do?

    • nastester's avatar
      nastester
      Regular Contributor

      That was one method I tried. I also tried running from a script. The script method wasn't very clean and also wasn't working for other coworkers/stopped working for me. I thought maybe the TestedApps approach would be better.

      The bat does a bunch of things but namely runs SQL commands and ReadyAPI dataloads into the test env

  • nastester's avatar
    nastester
    Regular Contributor

    My solution for this was just writing a custom script that runs in the execution plan:

     

    function runBatchFile(batchFilePath) {

    try {

    var process = Sys.OleObject("WScript.Shell");
    process.Run('cmd.exe /C "' + batchFilePath + '"', 1, true);

    while (process.Status == 0) {
    Delay(100);
    }
    Log.Message("Batch file executed")
    }
    catch (e) {
    Log.Error("An error occurred while running the batch file: " + e.message);
    }
    }

    var batchFilePath = "D:\\SmokeTestCleanup.bat";
    runBatchFile(batchFilePath);