【问题标题】:How change in loop paramter in URL SoapUI Rest如何更改 URL SoapUI Rest 中的循环参数
【发布时间】:2018-04-05 17:55:30
【问题描述】:

例如,我有地址:https://stackoverflow.com/1234?action=update?status=active,我想在循环中动态更改“1234”,所以我这样做:

  1. 我创建 REST 项目并在 SoapUI 5.3.0 中添加 url https://stackoverflow.com/{id}?action=update?status=active
  2. 我添加了像参数STYLE = Template, Value = ${id}这样的id
  3. 我使用 REST 请求和 Groovy 脚本创建了 TestCase
  4. 我添加了相同的 Rest 请求(URL https://stackoverflow.com/{id}?action=update?status=active 并添加 id 参数 STYLE = Template, Value = ${id})
  5. 在 Groovy Scipt 中,我想更改 &{id} 的值并转到 rest 请求,所以我写:
def ids= [1,2,3,4]
for(i=0;ids.size();i++){

   context.testCase.getProperty('id') as Integer 
   //How I com back to Rest Request? 
}

你有什么想法吗?有可能吗?

【问题讨论】:

    标签: rest groovy soapui


    【解决方案1】:

    使用属性有一种更简单的方法

    将网址命名为

      ${#TestCase#url}
    

    禁用名为“request1”的请求

    添加一个 groovy 脚本,您将通过该脚本多次运行请求

    参考这一步

    String id[]=["Value1forid","value2","value3","value4","value5")  
    for(int i=0;i<5;i++)
     {  
        def temp=" https://stackoverflow.com/{" + id[i] + "}?action=update?status=active "
    
        testRunner.testCase.setProertyValue("url",temp)
        tstep=testRunner.testCase.gettestStepbyName("request1")
        tstep.run(testRunner,context)
     }
    
    // This way you can run the request 5 times with 5 times different URL where we have put the value we want
    
    // This can be used for any number of properties. Since the request is disabled it will not run and will be run via Groovy step
    

    【讨论】:

    • 我通过在循环中仅更改 'id' 进行修改很有帮助。
    • 感谢您接受解决方案并发布您自己的解决方案
    【解决方案2】:

    编辑: 有可能,我把项目上传到google drive。我想它可能会对你有所帮助。 我从 2012 年开始遵循 this 伙计们的教程。 例如,我使用了 swagger 的 petstore,但您应该能够将该部分替换为您需要的任何 API。无论是 TEMPLATE 还是 QUERY 参数都没有关系 - 方法保持不变。

    P.S 在创建时宠物 1、2、3、5 和 6 都在场。这就是为什么您可以在输入中看到 1,2,3,5,6。

    假设这是您项目的结构:

    1.DataSource(Groovy 步骤)

    2.属性

    3.请求(API)

    4.DataLoop(Groovy步骤)

    这是DataSource(Groovy step)的内容

    import com.eviware.soapui.support.XmlHolder
    import com.eviware.soapui.support.GroovyUtils
    
    def myTestCase = context.testCase
    def counter,next,previous,size
    def projectDir = new GroovyUtils(context).projectPath
    
    File tickerEnumFile = new File(projectDir + "/input.txt") //make sure input.txt file already exists and contains different set of values sepearted by new line (CR).
    List lines = tickerEnumFile.readLines()
    size = lines.size.toInteger()
    propTestStep = myTestCase.getTestStepByName("Properties") // get the Property TestStep
    propTestStep.setPropertyValue("Total", size.toString())
    counter = propTestStep.getProperty("Count").value
    if (counter == null || counter == ""){
        counter = 0
    }
    counter= counter.toInteger()
    next = (counter > size-2? 0: counter+1)
    tempValue = lines[counter]
    propTestStep.setPropertyValue("Value", tempValue)
    propTestStep.setPropertyValue("Count", next.toString())
    next++
    log.info "Reading line : ${(counter+1)} / $lines.size"
    propTestStep.setPropertyValue("Next", next.toString())
    log.info "Value '$tempValue' -- updated in $propTestStep.name"
    if (counter == size-1){
    propTestStep.setPropertyValue("StopLoop", "T")
    log.info "Setting the stoploop property now..."
    } else if (counter==0){
    def runner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(testRunner.testCase, null)
    propTestStep.setPropertyValue("StopLoop", "F")
    } else{
    propTestStep.setPropertyValue("StopLoop", "F")
    }
    

    属性:开头应该为空

    请求:

    最后是 DataLoop

    import com.eviware.soapui.support.types.StringToObjectMap
    
    def myTestCase = context.testCase
    
    def runner
    propTestStep = myTestCase.getTestStepByName("Properties") // get the Property TestStep
    endLoop = propTestStep.getPropertyValue("StopLoop").toString()
    
    if (endLoop.toString() == "T" || endLoop.toString()=="True" || endLoop.toString()=="true"){
    log.info ("Exit from the loop")
    assert true
    } else {
    testRunner.gotoStep(0)
    }
    

    旧评论: 您使用的是免费版的 SoapUI 还是 SoapUI Pro?也许是ReadyAPI?如果您使用的是专业版,您可能需要考虑使用数据源循环。让我知道是否是这种情况。谢谢。

    【讨论】:

    • 我创建了一个您需要的 SoapUI 项目,但不确定是否可以将其附在此处。
    • @Gregory 谢谢。我使用 ${#TestCase#id} 解决了我的问题,并在 TestCase 中添加了通过 testRunner.runTestStepByName("REST_REQUEST") 调用 restRequest 的常规脚本
    【解决方案3】:

    我通过在 groovy 脚本中添加值 ${#TestCase#id} 来解决我的问题:

    def ids= [1,2,3,4] 
    for(i=0;ids.size();i++){
     String id = ids[i]  
     context.testCase.setPropertyValue("id", ids[i]) 
     testRunner.runTestStepByName( "REST_REQUEST")  
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-09
      • 1970-01-01
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      相关资源
      最近更新 更多