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

时间:2019-05-20 05:17:01

标签: 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.info line}

这是蝙蝠文件的内容

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

1 个答案:

答案 0 :(得分:1)

以下代码:

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

这将执行该过程,调用waitForProcessOutput以确保该过程不会阻塞,并且我们正在保存该过程的stdout和stderr流,然后等待使用{{3 }}。

waitForOrKill之后,该过程要么因为花费了太长时间而终止,要么已经正常完成。无论如何,out变量将包含命令的输出。要确定bat文件执行期间是否存在错误,可以检查reterr变量。

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