Groovy 如何在soapui中运行外部文件并获取输出并将其设置为标头

Groovy 如何在soapui中运行外部文件并获取输出并将其设置为标头,groovy,soapui,Groovy,Soapui,我想在soapUI中使用groovy脚本运行一个外部.bat文件。还希望使用从外部文件生成的输出作为标头的值 下面是我用来运行bat文件的脚本 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.in

我想在soapUI中使用groovy脚本运行一个外部.bat文件。还希望使用从外部文件生成的输出作为标头的值

下面是我用来运行bat文件的脚本

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}
java -jar SignatureGen.jar -pRESOURCE -nRandomString -mGET -d/api/discussion-streams/metadata -teyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJjbGllbnQiOiIxIiwicm9sZSI6IllGQURNSU4iLCJleHAiOjI3NTgzMjU2MDIsInRpIjo3MjAwNiwiaWF0IjoxNTU4MzI1NjAyLCJwZXJzb24iOiI1In0.bbci7ZBWmPsANN34Ris9H0-mosKF2JLTZ-530Rex2ut1kjCwprZr_196N-K1alFBH_A9pbG0MPspaDOnvOKOjA
下面是bat文件的内容

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}
java -jar SignatureGen.jar -pRESOURCE -nRandomString -mGET -d/api/discussion-streams/metadata -teyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJjbGllbnQiOiIxIiwicm9sZSI6IllGQURNSU4iLCJleHAiOjI3NTgzMjU2MDIsInRpIjo3MjAwNiwiaWF0IjoxNTU4MzI1NjAyLCJwZXJzb24iOiI1In0.bbci7ZBWmPsANN34Ris9H0-mosKF2JLTZ-530Rex2ut1kjCwprZr_196N-K1alFBH_A9pbG0MPspaDOnvOKOjA
以下代码:

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"
}
基于linux,但转换为windows时只需将
ls-la
替换为bat文件调用
cmd/c:\\Script\\S1.bat

这将执行进程,调用以确保进程不会阻塞,并且我们正在保存进程的stdout和stderr流,然后等待进程完成使用

执行
waitForOrKill
操作后,该过程要么因为时间过长而终止,要么正常完成。无论何种情况,
out
变量将包含命令的输出。要确定bat文件执行期间是否存在错误,可以检查
ret
err
变量


我随机选择了
waitForOrKill
超时,根据需要进行调整。您也可以使用
waitFor
而不使用超时,该超时将等待进程完成,但通常最好设置一些超时,以确保您的命令不会无限期执行

感谢您的回复,但是我如何捕获输出并将其分配给变量上述代码中的变量
out
包含输出。它是StringBuffer类型,如果您需要字符串实例中的变量,您可以执行以下操作:
def strOut=out.toString()
,这将为您提供类型为
java.lang.String
的变量
strOut
,以及bat文件执行的输出。