简体   繁体   中英

How to get xquery assertion value using groovy script

I have created a teststep in SOAPUI tool with two assertions namely

  1. Valid HTTP Status codes Assertion and
  2. XQuery Match Assertion

Now I need to print the values contained in these assertions using groovyscript. For XQuery Assertion, I need to print the xquery expression . I tried using getToken() method.

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. 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 . 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.

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:

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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