Run batch from Test Case

I guess you should escape a backslash character with a backslash character

Process p=Runtime.getRuntime().exec(“cmd /c start d:\\a.bat test1”)

Yes i have done it as below. Still its not working.
Process p=Runtime.getRuntime().exec(“cmd /c start d:\\a.bat test1”)

i have a python program to create a text file. I am passing the file name to the python program through the batch file parameter. After launching cmd, i can see the argument being passed to the batch file and to the python file but new file is not getting created.

image

import time;
import sys;
print(“python programto create file”)
time.sleep(2)
a1 = sys.argv[1]
print (“Argument receieved is “,sys.argv[1])
f = open(a1+”.txt”, “w+”)

How about this ?

Process p=Runtime.getRuntime().exec(“C:\\WINDOWS\\system32\\cmd.exe /c start d:\\a.bat test1”)

No luck. I think starting cmd.exe is no problem, it is launching the command prompt and passing the argument to python file. Is python file execution process getting killed once katalon execution process is completed? I can see that, katalon TC status is shown as passed while the process of creating a text file[open(a1+".txt",“w+”) ] is still being executed. I even tried p.waitFor() but still not working. I want to use this concept to do other things, but stuck here.

I’m not an expert in this area but …

I’m concerned that using start without /WAIT means the process for a.bat is running asynchronously (or, at least, independently) in a different shell invocation. IOW, the result p is coming from the launching cmd.exe and not the cmd.exe that’s running a.bat.

https://ss64.com/nt/start.html

1 Like

katalon TC status is shown as passed while the process of creating a text file[open(a1+".txt",“w+”) ] is still being executed.

As JDK doc https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String) explains,
java.lang.Runtime#exec() executes the specified command and arguments in a separate process.

Katalon’s TC status would be PASSED as it has successfully invoked a new process, and that’s all TC does. TC does not implicitly wait for the invoked process to finish, as the new process runs independently.

If you want your TC to wait for the process to finish, the easiest way you can do would be reading the STDOUT and STDERR emitted by the invoked process while waiting for some ‘DONE’ message. As for how to read STDOUT and STDERR, please have a look at

1 Like

Hi @kazurayam@Russ_Thomas,

Thank you so much for your inputs. It was working but file was created in location other than batch file’s location. My batch file is placed in "D:". On execution of this batch file manually, it was creating the file in “D:\”. But when i executed through katalon’s test case, it considered katalon test case’s directory as current directory and created the file there. But i was looking at “D:\”, My BAD!!!. Accidentally i scrolled down in the Test Explorer of the katalon and found all the files which i thought it did not create :).

Thank you both for your time and suggestions.

@kpns
you can add the absolute path in your python script to make sure the file is created in the right place every time , no matter from where the script is invoked.

I had kept python script and bat file in the same location. But it took Test Case directory for creating the file.

that’s why is better to use absolute path, to avoid flakines

yes sure. Will try that once. Thanks!