Groovy 脚本 - 从 SOAP UI 自动请求和保存响应 [英] Groovy Script - Automatic Request and saving response from SOAP UI

查看:35
本文介绍了Groovy 脚本 - 从 SOAP UI 自动请求和保存响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Groovy 脚本的新手.

I am new to Groovy scripting.

要求从文本文件中读取请求值并将其传递给soap请求xml并保存输出.

Requirement To read the request values from a text file and pass it to the soap request xml and save the output.

面临的问题:我无法读取从第 1 步到第 2 步的数据.但是我也在上下文变量中设置了值.请帮助我解决问题,以便我能够自动化整个过程.

Issue facing: I am not able to read the data from step 1 to step 2. However I am setting the values in context variable as well. Kindly help me to fix the issue so that I can able to automate the entire process.

注意:我们只能访问 SOAPUI,不能访问 SOAPUI Pro

Note: We have only access to SOAPUI not SOAPUI Pro

第 1 步:

File file1 = new File("C:\Users\Groovy Test\requests\orders.txt") 
List textLine = file1.readLines() 
log.info textLine 
context.put('textLine', textLine)  
log.info textLine

第 2 步:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
</soapenv:Header>
<soapenv:Body>
<OrderId>${context.get('textLine' )}</OrderId>
</soapenv:Body>
</soapenv:Envelope>

第 3 步:

def fileList = context.get('textLine')
def fileName = fileList.pop()
def newname = fileName[0..-5]
def response = context.expand( '${Step2#Response}' )
def f = new File("C:\Users\Groovy Test\responses\${fileName}_Response.xml")
f.write(response, "UTF-8")
if(fileList.size() >0)
{
testRunner.gotoStepByName("Step2")
}

推荐答案

这是实现您所寻找的目标的方法.

Here is the approach to achieve what you are looking for.

测试用例包含 3 个步骤,如下所示:

The test case contains 3 steps as shown below:

  • step1 - Groovy 脚本测试步骤.这将读取数据源,通过循环执行订单步骤.控制测试运行.
  • step2 - Soap Request 测试步骤.获取订单 &保存响应.
  • step3 - Groovy 脚本测试步骤.一种退出测试执行的方法.

第 1 步的 Groovy 脚本:

def data = new File('C:/Users/Groovy Test/requests/orders.txt') 
data.eachLine { orderId ->
   context.orderId = orderId
   //Get the step2, index of the step is 1
   def step = context.testCase.getTestStepAt(1)
   //Run the step2
   step.run(testRunner, context)
}
//By now all the orders got executed, now need to exit the step without additionally running step2
//So, jump to step2, index is 2
testRunner.gotoStep(2)

步骤 2

更改请求以使用 ${orderId}

为step2的请求添加Script Assertion.这会检查响应并保存它.

Add Script Assertion for the request of step2. This checks the response and saves it.

步骤 2 的脚本断言

//Check if there is response
assert context.request, "Request is empty or null"

//Save the contents to a file
def saveToFile(file, content) {
    if (!file.parentFile.exists()) {
         file.parentFile.mkdirs()
         log.info "Directory did not exist, created"
    }
    file.write(content) 
    assert file.exists(), "${file.name} not created"
}

def f = new File("C:/Users/Groovy Test/responses/${context.orderId}_Response.xml")
saveToFile(f, context.response)

步骤 3

第 3 步的 Groovy 脚本:

log.info "Test completed."

这篇关于Groovy 脚本 - 从 SOAP UI 自动请求和保存响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆