我有一个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)
}
在上面标有&#34; //在此处添加断言&#34;的部分中,我遇到问题的代码行是:
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中包含的Assertion Type,并将其作为字符串值放入,以便检查我检查的值是否被断言。
任何建议/方向将不胜感激。我不知道我做错了什么。感谢。
答案 0 :(得分: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的实例,并且此类中不存在contains()
方法,要设置要签入的字符串包含断言使用{{1}方法,我认为你需要这个来添加两个断言:
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")
此代码将导致创建这两个断言,一个名为&#34; CONTAINS 1&#34;并检查响应是否包含字符串&#34; Champion5&#34;以及第二个名为&#34; CONTAINS 2&#34;并检查响应是否包含字符串&#34; Challenge5&#34;。
希望这有帮助,