Run batch from Test Case

@Ramakrushna Sahoo: What exactly is the problem?

There is no error in the log but bat file result isnot display.
1.bat
----------
ECHO OFF

ECHO “Hello World”

PAUSE

_____

09-12-2018 07:03:14 PM - [START] - Start Test Case : Test Cases/inputdat

09-12-2018 07:03:14 PM - [INFO] - Evaluating variables for test case

09-12-2018 07:03:15 PM - [START] - Start action : Statement - bf = com.kms.katalon.core.configuration.RunConfiguration.getProjectDir() + “/” + “1.bat”

09-12-2018 07:03:15 PM - [END] - End action : Statement - bf = com.kms.katalon.core.configuration.RunConfiguration.getProjectDir() + “/” + “1.bat”

09-12-2018 07:03:15 PM - [START] - Start action : Statement - println(bf)

C:/Users/ramakrushna.a.sahoo/Desktop/CWAC/Katalon/CMPM_Automation/1.bat

09-12-2018 07:03:15 PM - [END] - End action : Statement - println(bf)

09-12-2018 07:03:15 PM - [START] - Start action : Statement - java.lang.Runtime.getRuntime().exec(bf)

09-12-2018 07:03:15 PM - [END] - End action : Statement - java.lang.Runtime.getRuntime().exec(bf)

09-12-2018 07:03:15 PM - [PASSED] - Test Cases/inputdat

09-12-2018 07:03:15 PM - [END] - End Test Case : Test Cases/inputdat

What does println(bf) show?

C:/Users/ramakrushna.a.sahoo/Desktop/CWAC/Katalon/CMPM_Automation/1.bat location of the bat file

For linux Environment how to do this same… Pls can you share it

You want to see the stdout from the Process in which “.bat” runs. And you want to see it in the Katalon Studio’s console. Then your test case Groovy script need to read the stdout of the forked Process, and write the stdout content into the Katalon’s stdout.

See Capturing stdout when calling Runtime.exec for similar issue and sample codes

Here is my example

Hi,

I am able to execute batch file with your solution but if i try to execute batch file with arguments, it is not working. It just executes and does not throw any error but i am not getting the result. Any idea what could be the issue?

Below is the code i am using. “test1” is the argument for a.bat. Here i am trying to create file and argument is the name of the file

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

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!