如何在SOAP UI中获取Request属性以进行REST服务测试

时间:2015-08-20 13:54:32

标签: web-services rest soapui

我创建了一个simple project in SOAP UI,现在正尝试使用安装脚本提取一些测试套件属性。唯一的问题是,RawRequest和Request属性是空的,但我想显示它们。请求确实存在,但请求和原始请求属性为空。这仅适用于REST服务。

   def tcaseList = testSuite.getTestCaseList();
           // for each testCase
  def countTestCases = tcaseList.size();
  for(tc = 0;tc<countTestCases;tc++){
  def countTestSteps = tcaseList[tc].getTestStepList().size();
  for(i=0;i<countTestSteps;i++)
  {// get testStep
    def testStep = tcaseList[tc].getTestStepAt(i);
   runner = tcaseList[tc].run(new com.eviware.soapui.support.types.StringToObjectMap(), false);
  def request = testStep.getPropertyValue("RawRequest");
  log.info(request.toString())

为什么此属性为null,以及如何提取请求并显示它的任何想法。

1 个答案:

答案 0 :(得分:1)

在SOAPUI上的REST请求中,RawRequest属性仅在REST请求中可用,它使用POST而不仅仅发送数据参数,在使用GET的REST请求中,RawRequest为空。如果您想要获取GET参数的值,可以在代码中使用它:

 ...
 def paramValue = testStep.getPropertyValue("parameterName");
 ...

在使用POST并发送数据的REST请求中,您已经使用代码正确地执行了这些操作:

def request = testStep.getPropertyValue("RawRequest");

我添加了以下图片来说明我在解释的内容:

  1. POST不空请求:
  2. enter image description here

    1. GET with empty Request:
    2. enter image description here

      <强>更新

      因此,如果您想从REST请求中获取所有参数,可以将以下groovy代码段添加到您的代码中:

      // your for loop...
      for(i=0;i<countTestSteps;i++){// get testStep
          def testStep = tcaseList[tc].getTestStepAt(i)
          ...
      
          // code to get REST Request parameters and log to the console
      
          // to avoid errors if you've testSteps which doesn't are RestTestSteps (i.e groovy...)
          if(testStep instanceof com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep){
              // ? is groovy operator to avoid NPE
              def parameters = testStep?.getTestRequest()?.getParams()
              // loop through the map parameters
              parameters.each { k,v ->
                  // for each params print the name and the value
                  log.info "$k : ${v.getValue()}"
              }
          }
      
          ...
      

      如果您需要更多信息,请查看RestTestRequestStep API

      希望这有帮助,

相关问题