简体   繁体   English

如何使用Groovy脚本获取XQuery断言值

[英]How to get xquery assertion value using groovy script

I have created a teststep in SOAPUI tool with two assertions namely 我在SOAPUI工具中创建了一个测试步骤,其中包含两个断言

  1. Valid HTTP Status codes Assertion and 有效的HTTP状态代码断言和
  2. XQuery Match Assertion XQuery匹配声明

Now I need to print the values contained in these assertions using groovyscript. 现在,我需要使用groovyscript打印这些断言中包含的值。 For XQuery Assertion, I need to print the xquery expression . 对于XQuery断言,我需要打印xquery表达式。 I tried using getToken() method. 我尝试使用getToken()方法。

Given below is my code 下面给出的是我的代码

for(int k=0;k<tc.getTestStepCount();k++)
{
  RestTestRequestStep rr= tc.getTestStepAt(k);
  RestTestRequest rrprop=rr.getTestRequest();
  int astcount=rr.getAssertionCount();
  for(int z=0;z<astcount;z++)
    {
      WsdlMessageAssertion tassert=rr.getAssertionAt(z);
      String assertname=tassert.getName();
      log.info "Assertion Name= "+assertname
      String assertvalue=rr.getAssertionAt(z).getToken();
      log.info "Assertion Value= "+assertvalue
    }
}  

But it is throwing the following error. 但是它引发了以下错误。

Fri May 22 11:11:58 IST 2015:INFO:groovy.lang.MissingMethodException: No signature of method: com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XQueryContainsAssertion.getToken() is applicable for argument types: () values: []

Please suggest me which method should be used here.. 请建议我在这里使用哪种方法。

Thanks in Advance. 提前致谢。

Use getPath() method to get the XQuery Expression on your assertion. 使用getPath()方法获取断言中的XQuery Expression Take into account that this method is specific for assertions of XQueryContainsAssertion class, so if you invoke in other assertion object you'll get a groovy.lang.MissingMethodException . 考虑到此方法特定于XQueryContainsAssertion类的断言,因此,如果在其他断言对象中调用,则会得到groovy.lang.MissingMethodException Also think about that with your code you're casting each testStep in your testCase to RestTestRequestStep ( RestTestRequestStep rr= tc.getTestStepAt(k); ) and the groovy testStep itself its part of the testCase and cannot be cast to this type, so better use def for the object to avoid cast errors. 还要考虑一下代码,您要将testCase中的每个testStep都转换为RestTestRequestStepRestTestRequestStep rr= tc.getTestStepAt(k); ),而常规的testStep本身就是testCase的一部分,因此不能转换为这种类型,所以更好对对象使用def以避免强制转换错误。

So taking all this stuff in account considering that you only want to apply your code for this kind of assertions you could have something as: 因此,考虑到所有这些因素后,考虑到您只想对此类断言应用代码,则可能会出现以下情况:

import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XQueryContainsAssertion

def tc = testRunner.testCase
for(int k=0;k<tc.getTestStepCount();k++)
{
  def rr= tc.getTestStepAt(k)
  // check that the testStep really contains assertions (it's not a groovy TestStep for example)
  if(rr.metaClass.respondsTo(rr,"getAssertionCount")){
      int astcount=rr.getAssertionCount()
      for(int z=0;z<astcount;z++)
        {
          def tassert=rr.getAssertionAt(z)
          String assertname=tassert.getName()
          log.info "Assertion Name= "+assertname
          // check the assert it's a XqueryContainsAssertion
          if(tassert instanceof XQueryContainsAssertion){
            String assertvalue=tassert.getPath()
            log.info "Assertion Value= "+assertvalue
          }
        }
  }
}

EDIT BASED ON COMMENT 根据评论进行编辑

If you want to get also the http status codes you can use the property codes in your assertion of type ValidHttpStatusCodesAssertion , I modify the previous code to take in account the both types of assertions: 如果您还想获取http status codes ,则可以在类型ValidHttpStatusCodesAssertion的断言中使用属性codes ,我修改了先前的代码以考虑这两种断言:

import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XQueryContainsAssertion
import com.eviware.soapui.security.assertion.ValidHttpStatusCodesAssertion

def tc = testRunner.testCase
for(int k=0;k<tc.getTestStepCount();k++)
{
  def rr= tc.getTestStepAt(k)
  // check that the testStep really contains assertions (it's not a groovy TestStep for example)
  if(rr.metaClass.respondsTo(rr,"getAssertionCount")){
      int astcount=rr.getAssertionCount()
      for(int z=0;z<astcount;z++)
        {
          def tassert=rr.getAssertionAt(z)
          String assertname=tassert.getName()
          log.info "Assertion Name= "+assertname
          // check the assert it's a XqueryContainsAssertion
          if(tassert instanceof XQueryContainsAssertion){
            String assertvalue=tassert.getPath()
            log.info "Assertion Value= "+assertvalue
          }else if(tassert instanceof ValidHttpStatusCodesAssertion){
            String assertvalue=tassert.codes
            log.info "Http status codes= "+assertvalue
          }
        }
  }
}

Hope this helps, 希望这可以帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM