Execute a .jar in a Groovy script

MikeMapanare :

I'm creating a test case with Katalon Studio using the script mode which is a groovy script. I need that groovy script to execute a .jar that will be inside the Katalon project folder.

For testing purposes I created a .jar that creates a file named "the-file-name" and prints a message in the console.

I found a way to execute a command in Groovy:

def command = "git --version"

def proc = command.execute()

proc.waitFor()

println proc.in.text

This prints git's version in the Katalon console. So I guessed that putting "java -jar test.jar" would be enough but even though the execution seems to end correctly it also seems that the .jar didn't do anything. Just to be sure, I executed the same .jar using de Windows command line and it works perfectly. The file is created and the message written in the console.

When executing, Katalon console acts as if it was correctly executed. There are no error messages and execution is marked as successful yet the test file "the-file-name" is nowhere to be found and I'm not getting the .jar's console output shown in the Katalon console as in the git command.

MikeMapanare :

Found a way to do it.

public class CustomKeywords {

    @Keyword
    def runBatch(String path) {
        def cmd = "cmd /c \"java -jar \"" + path + "\"\"";
        runCmd(cmd)
    }

    def runCmd(String cmd) {
        KeywordUtil.logInfo("cmd: ${cmd}")

        def proc = cmd.execute();
        def outputStream = new StringBuffer();
        def errStream = new StringBuffer()
        proc.waitForProcessOutput(outputStream, errStream);
        println(outputStream.toString());
        println(errStream.toString())

        if(proc.exitValue() != 0){
            KeywordUtil.markFailed("Out:" + outputStream.toString() + ", Err: " + errStream.toString())
        }
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=147553&siteId=1