2014-09-18 70 views
0

我有一个groovy脚本,我添加了代码来检查在SoapUI中获取的XML响应中是否存在值。我已经处理了几天,可以使用一些帮助。addAssertion()无法识别“包含”类型的字符串值

下面是编号:

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) 

}

在上面称为“//添加断言这里”,那我有一个问题代码行的部分是:

def assertion = tc.getTestStepByName(newTestStep.toString()).addAssertion("Contains") 

的错误状态:

Thu Sep 18 14:27:57 CDT 2014:ERROR:An error occurred [Cannot invoke method addAssertion() on null object], see error log for details 

我的理解是,我必须传递一个包含在SoapUI GUI中的断言类型,并将其作为字符串值放入,以便检查我检查的值是否被声明。

任何建议/方向,将不胜感激。我不知道我在做什么错。谢谢。

+0

我的第一个答案是不正确的,我认为现在它更有帮助。 – albciff 2014-09-18 21:45:36

回答

0

我认为这个问题是你在呼唤一个错误的名称的功能tc.getTestStepByName(String name),因为newTestStep.toString()不返回测试步骤名称,而不是它返回的类名(它真的返回'classname' + '@' + 'Integer.toHexString(hashCode())'如果它没有覆盖它,请参阅Object.toString() )。

反正你想要的是添加断言为这是刚刚创建,以便直接添加断言给它一个测试步骤,而不是按名称来找到它,使用:

def assertion = newTestStep.addAssertion("Contains")

代替:

def assertion = tc.getTestStepByName(newTestStep.toString()).addAssertion("Contains")

你的第二个问题是,你正在使用的assertion不正确。该assertion对象,你的情况是SimpleContainsAssertion和方法的实例在这个类中不存在,设置要检查的字符串中包含断言使用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”,以及第二个命名为“含有2个”和检查响应是否包含字符串“Challenge5”。

希望这有助于,

+0

再次感谢您的帮助。 – Melinda 2014-09-19 12:52:55

+0

所以最后的作品?别客气!。 – albciff 2014-09-19 12:58:03

+0

是的。谢谢。我想我无法确定的是来自SoapUI的API我没有看到哪些类最适合使用以及如何选择它们。我从谷歌搜索得到的代码,它似乎为他们工作,但显然我有错误的信息。任何建议,将不胜感激。再次感谢您的所有时间和帮助。问候。 – Melinda 2014-09-19 15:27:29