Bob Jones
Bob Jones

Reputation: 171

How do I run an external file in soapui and take the output and set it as header

I would like to run an external .bat file using groovy script in soapUI. also would like to use the output generated from the external file as the value for the header

here is the script that I am using to run the bat file

String line
def p = "cmd /c C:\\Script\\S1.bat".execute()
def bri = new BufferedReader (new InputStreamReader(p.getInputStream()))
while ((line = bri.readLine()) != null) {log.info line}

here is the content of the bat file

java -jar SignatureGen.jar -pRESOURCE -nRandomString -mGET -d/api/discussion-streams/metadata -teyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJjbGllbnQiOiIxIiwicm9sZSI6IllGQURNSU4iLCJleHAiOjI3NTgzMjU2MDIsInRpIjo3MjAwNiwiaWF0IjoxNTU4MzI1NjAyLCJwZXJzb24iOiI1In0.bbci7ZBWmPsANN34Ris9H0-mosKF2JLTZ-530Rex2ut1kjCwprZr_196N-K1alFBH_A9pbG0MPspaDOnvOKOjA

Upvotes: 0

Views: 517

Answers (1)

Matias Bjarland
Matias Bjarland

Reputation: 4482

The following code:

def p = "ls -la".execute()

def err = new StringBuffer()
def out = new StringBuffer()
p.waitForProcessOutput(out, err)

p.waitForOrKill(5000)
int ret = p.exitValue()

// optionally check the exit value and err for errors 

println "ERR: $err"
println "OUT: $out"

// if you want to do something line based with the output
out.readLines().each { line -> 
  println "LINE: $line"
}

is based on linux, but translates to windows by just replacing the ls -la with your bat file invocation cmd /c C:\\Script\\S1.bat.

This executes the process, calls waitForProcessOutput to make sure the process doesn't block and that we are saving away the stdout and stderr streams of the process, and then waits for the process to finish using waitForOrKill.

After the waitForOrKill the process has either been terminated because it took too long, or it has completed normally. Whatever the case, the out variable will contain the output of the command. To figure out whether or not there was an error during bat file execution, you can inspect the ret and err variables.

I chose the waitForOrKill timeout at random, adjust to fit your needs. You can also use waitFor without a timeout which will wait until the process completes, but it is generally better to set some timeout to make sure your command doesn't execute indefinitely.

Upvotes: 1

Related Questions