首页 > 解决方案 > SoapUI 从文件中读取请求参数(免费版)

问题描述

我有一个包含以下内容的文件

987656   
987534

在我的测试之前,我有一个 Groovy 脚本作为测试步骤:

def myTestCase = context.testCase
new File("filepath/data1.txt").eachLine { line ->
  myTestCase.setPropertyValue("inputValue", line)
  inputValue = context.expand( '${#TestCase#inputValue}' )
  log.info inputValue
}

脚本的日志输出为我提供了文件中的两个值:

987656
987534

我有一个 Testcase 自定义属性设置为“inputValue”,在我的测试中我将参数称为

 <enr:Id>${#TestCase#inputValue}</enr:Id>

在执行期间,测试始终针对最后一个值“987534”运行,而不是针对两个输入。

我应该怎么做才能对文本文件中存在的所有值执行测试?

谢谢

标签: groovysoapui

解决方案


像这样循环遍历值的方法是,在eachLine循环中调用 SOAP 步骤,如下所示:

def myTestCase = context.testCase
new File("filepath/data1.txt").eachLine { line ->
    myTestCase.setPropertyValue("inputValue", line)
    inputValue = context.expand( "${#TestCase#inputValue}" )
    log.info inputValue

    //define the step
    def soapTestStep = testRunner.testCase.getTestStepByName("YourSOAPRequestName").name
    
    //call the step
    testRunner.runTestStepByName(soapTestStep)

    //if you want to do something with the response XML
    def responseSOAP = context.expand("${YourSOAPRequestName#Response}")

    //if you want to check a value in the response XML
    def responseSection = responseSOAP =~ /someNode>(.*)<\/someNode/   
    def responseValue = responseSection[0][1]
    log.info "response: ${responseValue}"
}

推荐阅读