dongzeao5047
2017-04-07 16:23
浏览 190
已采纳

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

In SoapUI (Pro version) I have 12 test steps:

  1. GET request
  2. Groovy script witch get total value from response from the previous step
  3. Properties
  4. Properties transfer
  5. POST request 1
  6. POST request 2
  7. POST request 3
  8. POST request 4
  9. POST request 5
  10. POST request 6
  11. POST request 7
  12. POST request 8

All POST requests have ID parameters.

Question:

How to write a Groovy script like this or maybe another decision way:

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

图片转代码服务由CSDN问答提供 功能建议

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

    \ n
  1. GET request
  2. Groovy脚本从上一步的响应中获取 total
  3. 属性
  4. 属性传输
  5. POST 请求1
  6. POST request 2
  7. POST request 3
  8. POST request 4
  9. POST request 5
  10. POST request 6
  11. POST request 7 \ n
  12. POST request 8

    所有 POST 请求都有 ID 参数。

    问题:

    如何编写这样的Groovy脚本或者其他决策方式:

     如果total = 8则设置POST 1 = 1,POST 2 = 1,POST 3 = 1,POST 4 = 1,POST 5 = 1,POST 6 = 1的ID参数,  POST 7 = 1,POST 8 = 1 
    if total = 7然后设置POST 1 = 2,POST 2 = 1,PO的ID参数 ST 3 = 1,POST 4 = 1,POST 5 = 1,POST 6 = 1,POST 7 = 1 
    if total = 6然后设置POST 1 = 2,POST 2 = 2,POST 3 = 1,POST的ID参数 4 = 1,POST 5 = 1,POST 6 = 1 
    if total = 5然后设置POST参数1 = 2,POST 2 = 2,POST 3 = 2,POST 4 = 1,POST 5 = 1 
    if total  = 4然后设置POST 1 = 2,POST 2 = 2,POST 3 = 2,POST 4 = 2 
    if total = 3的ID参数然后设置POST 1 = 3,POST 2 = 3,POST 3 = 2的ID参数 
    if total = 2然后设置POST 1 = 4的ID参数,POST 2 = 4 
    if total = 1然后设置POST 1 = 8 
       
     的ID参数
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanputian5341 2017-04-10 00:00
    已采纳

    You could use the following in the groovy [step 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)
       }
    
    打赏 评论

相关推荐 更多相似问题