Groovy addAssertion()无法识别字符串值";包括「;类型

Groovy addAssertion()无法识别字符串值";包括「;类型,groovy,soapui,Groovy,Soapui,我有一个groovy脚本,我在其中添加了代码,以检查在SoapUI中得到的XML响应中是否存在值。我已经处理这件事好几天了,需要一些帮助 代码如下: import com.eviware.soapui.support.XmlHolder import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory // read your request template def requestFile = new

我有一个groovy脚本,我在其中添加了代码,以检查在SoapUI中得到的XML响应中是否存在值。我已经处理这件事好几天了,需要一些帮助

代码如下:

import com.eviware.soapui.support.XmlHolder
import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory

// read your request template
def requestFile = new File("C:/XMLRequestScript/file.xml");

// parse it as xml
def requestXml = new XmlHolder(requestFile.text)

// get the current testCase to add testSteps later
def tc = testRunner.testCase;

// get the testStep as template to create the other requests
def tsTemplate = tc.getTestStepByName("Template");

// loop to create # of testSteps for each application
for(int i = 1; i < 3; i++) {
// xpath expression to get applicationNumber attribute in root node
def xpathNodeAttr = "/*/@ApplicationNumber";

// get the root node attribute applicationNumber throught an XPATH
int appId = Integer.parseInt(requestXml.getNodeValue(xpathNodeAttr));

// add 1 to appId
appId++;

// set the value again in the attribute
requestXml.setNodeValue(xpathNodeAttr,appId);

// create next testStepName for new Application
def testStepName = "TestStep_ApplicationNumber_" + String.valueOf(appId)
log.info testStepName;
log.info testStepName.getClass().getName()
log.info tc.getClass().getName()

// create a new testStepConfig
def testStepFactory = new RestRequestStepFactory();
def testStepConfig = testStepFactory.createConfig( tsTemplate.getTestRequest(), testStepName )

// add the new testStep to TestCase
def newTestStep = tc.insertTestStep( testStepConfig, -1 ) 

// set the request
newTestStep.getTestRequest().setRequestContent(requestXml.getXml())   

// add Assertions here
def assertion = tc.getTestStepByName(newTestStep.toString()).addAssertion("Contains")
if (assertion.contains("Champion5")) {
    newTestStep.addAssertion("Champion5")
    log.info("REST response assertion created into: " + newTestStep)
} else {
    log.info("REST response assertion not found in " + newTestStep)
}
if (assertion.contains("Challenger5")) {
    newTestStep.addAssertion("Challenger5")
    log.info("REST response assertion created into: " + newTestStep)
} else {
log.info("REST response assertion not found in " + newTestStep)
}

// execute the request 
newTestStep.run(testRunner, context)
错误状态为:

Thu Sep 18 14:27:57 CDT 2014:ERROR:An error occurred [Cannot invoke method addAssertion() on null object], see error log for details
我的理解是,我必须传递SOAPUIGUI中包含的断言类型,并将其作为字符串值放入,以便检查我正在检查的值是否被断言


如有任何建议/指示,将不胜感激。我不知道我做错了什么。谢谢。

我认为问题在于您使用错误的名称调用函数
tc.getTestStepByName(字符串名称)
,因为
newTestStep.toString()
不返回测试步骤名称,而是返回类名(它实际上返回
'classname'+'+'Integer.tohextString(hashCode())“
如果未覆盖它,请参见)

无论如何,您希望为刚刚创建的测试步骤添加断言,因此直接将断言添加到测试步骤中,而不是按名称查找,请使用:

def assertion=newTestStep.addAssertion(“包含”)

而不是:

def assertion=tc.getTestStepByName(newTestStep.toString()).addAssertion(“包含”)

第二个问题是您使用的
断言不正确。您案例中的
assertion
对象是的一个实例,并且
contains()
方法在此类中不存在,要设置字符串以签入contains asserte,请使用
setToken()
方法,我想您需要这样来添加两个断言:

def assertion = newTestStep.addAssertion("Contains")
// add token to check
assertion.setToken("Champion5") 
// change assert name in order to avoid the popup
// when we create the second one.
assertion.setName("CONTAINS 1")
// create a second assertion
assertion = newTestStep.addAssertion("Contains")
assertion.setToken("Challenger5")
assertion.setName("CONTAINS 2")
而不是:

// add Assertions here
def assertion = tc.getTestStepByName(newTestStep.toString()).addAssertion("Contains")
if (assertion.contains("Champion5")) {
    newTestStep.addAssertion("Champion5")
    log.info("REST response assertion created into: " + newTestStep)
} else {
    log.info("REST response assertion not found in " + newTestStep)
}
if (assertion.contains("Challenger5")) {
    newTestStep.addAssertion("Challenger5")
    log.info("REST response assertion created into: " + newTestStep)
} else {
    log.info("REST response assertion not found in " + newTestStep)
}
此代码将导致创建这两个断言,一个名为“CONTAINS 1”并检查响应是否包含字符串“Champion5”,另一个名为“CONTAINS 2”并检查响应是否包含字符串“Challenge5”


希望这有帮助,

我的第一个答案不正确,我认为现在它更有帮助。再次感谢您的帮助。是的。非常感谢。我想我不确定的是在SoapUI的API中,我没有看到什么类最适合使用,以及如何选择它们。我从谷歌上得到了代码,似乎对他们有用,但显然我有错误的信息。如有任何建议,将不胜感激。再次感谢您的时间和帮助。是的,一开始可能不是很直观,但是你可以在web上查看文档和示例:在api上查看方法、类。。。当然,也要像您一样进行编码:)
// add Assertions here
def assertion = tc.getTestStepByName(newTestStep.toString()).addAssertion("Contains")
if (assertion.contains("Champion5")) {
    newTestStep.addAssertion("Champion5")
    log.info("REST response assertion created into: " + newTestStep)
} else {
    log.info("REST response assertion not found in " + newTestStep)
}
if (assertion.contains("Challenger5")) {
    newTestStep.addAssertion("Challenger5")
    log.info("REST response assertion created into: " + newTestStep)
} else {
    log.info("REST response assertion not found in " + newTestStep)
}