Groovy脚本-从SOAP UI自动请求并保存响应 [英] Groovy Script - Automatic Request and saving response from 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脚本测试步骤.这将读取数据源,并通过循环执行命令步骤.控制测试运行.
- 第2步-肥皂请求测试步骤.获取订单和保存响应.
- 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)
Step2
更改请求以使用<OrderId>${orderId}</OrderId>
为步骤2的请求添加Script Assertion
.这将检查响应并保存.
Add Script Assertion
for the request of step2. This checks the response and saves it.
第二步的脚本断言
//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)
Step3
步骤3的Groovy脚本:
log.info "Test completed."
这篇关于Groovy脚本-从SOAP UI自动请求并保存响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!