Groovy 脚本 - 自动请求并保存来自 SOAP 的响应 UI

Groovy Script - Automatic Request and saving response from SOAP UI

我是 Groovy 脚本的新手。

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

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

注意:我们只能访问 SOAPUI,不能访问 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")
}

我认为问题出在 步骤 2 的 Xml 中的符号:

使用:

<OrderId>${=context.get('textLine')}</OrderId>

而不是:

<OrderId>${context.get('textLine')}</OrderId>

注意 = 字符。

这是实现您所寻找的方法。

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

  • step1 - Groovy 脚本测试步骤。这会读取数据源,通过循环执行订单步骤。控制测试 运行.
  • step2 - 肥皂请求测试步骤。获取订单并保存响应。
  • step3 - Groovy 脚本测试步骤。一种退出测试执行的方法。

第一步

Groovy 第 1 步的脚本:

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)

第二步

更改请求以使用 <OrderId>${orderId}</OrderId>

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

第 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)

第三步

Groovy 第 3 步的脚本:

log.info "Test completed."