Run batch from Test Case

Hi, is possible to execute batch file directly from Test Case?

I tried Runtime.runtime.execute(“test.bat”) but with no result. TC is done with no errors, but batch dont run.

Thanks for tips
Milan

1 Like

Hi Milan

This works for me…

import com.kms.katalon.core.configuration.RunConfiguration

  /**
   * Execute a batch file situated in the KS project directory.
   * @param batchFile (String) e.g. "myfile.bat"
   */
  static void runBatchFile(String batchFile) {
    String bf = RunConfiguration.getProjectDir() + '/' + batchFile
    comment("Running batch file: " + bf)
    Runtime.runtime.exec(bf)
  }
1 Like

Hi Russ,

thanks for advice but same result :’(

TC
import external.Extbase as Extbase

Extbase.runBatchFile(‘myfile.bat’)

Class
import com.kms.katalon.core.configuration.RunConfiguration

class Extbase {
/**
* Execute a batch file situated in the KS project directory.
* @param batchFile (String) e.g. “myfile.bat”
*/
public static runBatchFile(String batchFile) {
String bf = RunConfiguration.getProjectDir() + ‘/’ + batchFile
println ("Running batch file: " + bf)
Runtime.runtime.exec(bf)
}
}

Console:
04-20-2018 03:38:46 odp. - [START] - Start Test Case : Test Cases/_archive/_TEST/TESTbatch
04-20-2018 03:38:46 odp. - [INFO] - Evaluating variables for test case
04-20-2018 03:38:47 odp. - [START] - Start action : Statement - external.Extbase.runBatchFile(“myfile.bat”)
Running batch file: C:/Bitbucket/JAT/automatic-tests/myfile.bat
04-20-2018 03:38:47 odp. - [END] - End action : Statement - external.Extbase.runBatchFile(“myfile.bat”)
04-20-2018 03:38:47 odp. - [PASSED] - Test Cases/_archive/_TEST/TESTbatch
04-20-2018 03:38:47 odp. - [END] - End Test Case : Test Cases/_archive/_TEST/TESTbatch

:confused:

Wild guess… permissions problem? I’d hope that the subsystem would report it if so, but I can’t think of anything else…

Probably, permission problem. Thank you very much for your interest, i solved my problem other way.

Milan Chalánek said:

Probably, permission problem. Thank you very much for your interest, i solved my problem other way.

Hi Milan,
Do you mind to share your solution in brief and that worked for you? Thanks lots.

Chua PY said:

Milan Chalánek said:

Probably, permission problem. Thank you very much for your interest, i solved my problem other way.

Hi Milan,
Do you mind to share your solution in brief and that worked for you? Thanks lots.

Anyway got it to work already using this. Thanks!

Runtime.getRuntime().exec(“cmd.exe /C cd /D C:\\Katalon && batch.bat”);

Chua PY said:

Chua PY said:

Milan Chalánek said:

Probably, permission problem. Thank you very much for your interest, i solved my problem other way.

Hi Milan,
Do you mind to share your solution in brief and that worked for you? Thanks lots.

Anyway got it to work already using this. Thanks!

Runtime.getRuntime().exec(“cmd.exe /C cd /D C:\\Katalon && batch.bat”);

Hi Chua,

nor did your solution work for me. Finally, I wrote the script in the bat file as Keyword in Katalon.

Milan Chalánek said:

Chua PY said:

Chua PY said:

Milan Chalánek said:

Probably, permission problem. Thank you very much for your interest, i solved my problem other way.

Hi Milan,
Do you mind to share your solution in brief and that worked for you? Thanks lots.

Anyway got it to work already using this. Thanks!

Runtime.getRuntime().exec(“cmd.exe /C cd /D C:\\Katalon && batch.bat”);

Hi Chua,

nor did your solution work for me. Finally, I wrote the script in the bat file as Keyword in Katalon.

Thanks Milan, looks like each of us have different solution/workarounds. Cheers. :slight_smile:

Milan Chalánek said:

Chua PY said:

Chua PY said:

Milan Chalánek said:

Probably, permission problem. Thank you very much for your interest, i solved my problem other way.

Hi Milan,
Do you mind to share your solution in brief and that worked for you? Thanks lots.

Anyway got it to work already using this. Thanks!

Runtime.getRuntime().exec(“cmd.exe /C cd /D C:\\Katalon && batch.bat”);

Hi Chua,

nor did your solution work for me. Finally, I wrote the script in the bat file as Keyword in Katalon.

Hi Chua,

Can you please let me know how did you do this? I mean how you wrote the script as keyword? Little bit more details will be very useful.

Sheetal

This solution is not work for me
Pls give another approach to run bat file from tc

@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”)