首页 > 解决方案 > 通过 Groovy 脚本在 SoapUI 中进行自动数据驱动测试

问题描述

正如在soapUI免费版本中讨论的那样,我试图称这个WS:

    <soap:Body>
<RequestParameters>
    <Parameter name="key" value="SomeorderId"/>
</RequestParameters>
    </soap:Body>

我想从我桌面上的本地 data.txt 文件中获取“key”的值。该文件的内容:

9999999991
9999999992
9999999993

为此,我创建了一个包含以下三个测试步骤的测试套件:

1.第一步:一个groovy脚本:

def data = new File('C:/Users/Polarick/Desktop/data.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)
}
//all the orders got executed,jump to step2, index is 2
testRunner.gotoStep(2)

2.第二步:修改请求:

<Parameter name="MSISDN" value="${orderId}"/>

并向其声明此脚本:

//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 response = context.expand( '${Step2#Response}' )
def f = new File("C:/Users/Polarick/Desktop/${context.orderId}_Response.xml")
f.write(response, "UTF-8")
saveToFile(f, context.response)

3.第三步:一个groovy脚本:

log.info "Test completed."

一切正常,并按顺序为 data.txt 文件中存在的所有行调用 WS,但我希望每次执行都能找到一个响应 .xml 文件,例如:

C:/Users/Polarick/Desktop/9999999991_Response.xml
C:/Users/Polarick/Desktop/9999999992_Response.xml
C:/Users/Polarick/Desktop/9999999993_Response.xml

但是没有生成响应文件,你能帮忙吗?

标签: groovysoapui

解决方案


推荐阅读