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

How to get Request property in SOAP UI for REST service test

我创建了一个 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())

知道为什么这个 属性 是空的,以及如何提取请求并显示它。

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

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

在使用 POST 并发送数据的 REST 请求中,您已经正确地使用代码:

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

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

  1. POST 不为空请求:

  1. GET 请求为空:

更新

因此,如果您想从 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

希望这对您有所帮助,