[英]How to get xquery assertion value using groovy script
我在SOAPUI工具中創建了一個測試步驟,其中包含兩個斷言
現在,我需要使用groovyscript打印這些斷言中包含的值。 對於XQuery斷言,我需要打印xquery表達式。 我嘗試使用getToken()方法。
下面給出的是我的代碼
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
}
}
但是它引發了以下錯誤。
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: []
請建議我在這里使用哪種方法。
提前致謝。
使用getPath()
方法獲取斷言中的XQuery Expression
。 考慮到此方法特定於XQueryContainsAssertion
類的斷言,因此,如果在其他斷言對象中調用,則會得到groovy.lang.MissingMethodException
。 還要考慮一下代碼,您要將testCase中的每個testStep都轉換為RestTestRequestStep
( RestTestRequestStep rr= tc.getTestStepAt(k);
),而常規的testStep
本身就是testCase的一部分,因此不能轉換為這種類型,所以更好對對象使用def
以避免強制轉換錯誤。
因此,考慮到所有這些因素后,考慮到您只想對此類斷言應用代碼,則可能會出現以下情況:
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
}
}
}
}
根據評論進行編輯
如果您還想獲取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
}
}
}
}
希望這可以幫助,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.