首页 文章

用于SoapUI中的测试步骤的Groovy脚本

提问于
浏览
-1

在SoapUI(专业版)中,我有12个测试步骤:

  • GET 请求

  • Groovy脚本从上一步的响应中获取 total

  • 属性

  • 属性转移

  • POST 请求1

  • POST 请求2

  • POST 请求3

  • POST 请求4

  • POST 请求5

  • POST 请求6

  • POST 请求7

  • POST 请求8

所有 POST 请求都有 ID 个参数 .

Question:

如何编写这样的Groovy脚本或者可能是另一种决策方式:

if total = 8 then set ID parameters of POST 1 = 1,POST 2 = 1,POST 3 = 1,POST 4 = 1,POST 5 = 1,POST 6 = 1,POST 7 = 1,POST 8 = 1
if total = 7 then set ID parameters of POST 1 = 2,POST 2 = 1,POST 3 = 1,POST 4 = 1,POST 5 = 1,POST 6 = 1,POST 7 = 1
if total = 6 then set ID parameters of POST 1 = 2,POST 2 = 2,POST 3 = 1,POST 4 = 1,POST 5 = 1,POST 6 = 1
if total = 5 then set ID parameters of POST 1 = 2,POST 2 = 2,POST 3 = 2,POST 4 = 1,POST 5 = 1
if total = 4 then set ID parameters of POST 1 = 2,POST 2 = 2,POST 3 = 2,POST 4 = 2
if total = 3 then set ID parameters of POST 1 = 3,POST 2 = 3,POST 3 = 2
if total = 2 then set ID parameters of POST 1 = 4,POST 2 = 4
if total = 1 then set ID parameters of POST 1 = 8

1 回答

  • 0

    您可以在groovy中使用以下内容[步骤2]

    def totalPostSteps = 8
    def totalFromResponse = 7 //map the one you got from prev response
    def postIdArray = new int[totalPostSteps]
    def index = 0
    1.upto (totalPostSteps,
    {
        postIdArray[index]+=1;
        index = index==totalFromResponse-1?0:index+1
    })
    //log.info postIdArray.toString()
    postIdArray.eachWithIndex{num, idx ->
         def numString = num!=0?num.toString():""
         //The next step will create/update properties in TestCase custom properties and u can map these directly to your POST requests
         //OR You can set these directly to step3 and continue with step 4
         testRunner.testCase.setPropertyValue("IdForPost_"+(idx+1), numString)
       }
    

相关问题