我如何 运行 soapui 中的外部文件并将输出设置为 header

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

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

的值

这是我用来 运行 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}

这里是bat文件的内容

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,但只需将 ls -la 替换为您的 bat 文件调用 cmd /c C:\Script\S1.bat 即可转换为 windows。

这会执行进程,调用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

waitForOrKill 之后,进程要么因为耗时太长而终止,要么已经正常完成。不管怎样,out 变量将包含命令的输出。要确定 bat 文件执行期间是否有错误,您可以检查 reterr 变量。

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