在 Java 中的 SoapUI API 中向 TestStep 添加断言

Adding an assertion to a TestStep in SoapUI API in Java

我正在尝试使用 SoapUI API 自动执行 Restful 测试。据此了解,我不想打开 SoapUI GUI 来启动我的项目,尽管可以通过 GUI 导入和执行 WsdlProject 文件中保存的 xml 文件。

这是我所拥有的:

我创建了一个 SoapUI 项目:

WsdlProject wadlProject = new WsdlProject();

然后我添加一个测试套件:

WsdlTestSuite ts =  wadlProject.addNewTestSuite("HelloTestSuite");

然后我向这个测试套件添加一个或多个测试用例:

WsdlTestCase tc = ts.addNewTestCase("HelloTestCase1");

最后我将测试步骤添加到测试用例中:

WsdlTestStep testStep = tc.addTestStep(HTTP_RQST,"HelloTestStep1",ip_addr+":"+port,"POST");

private final String HTTP_RQST = HttpRequestStepFactory.HTTPREQUEST_TYPE;

现在,如果我想在我的 POST 测试步骤中添加有效载荷,我会这样做:

testStep.setPropertyValue("request", "My Payload to send with the test");
##########################现在我的问题是: ##########################

如果我想添加一个简单的 contains assertion 到这个测试步骤,它是如何完成的?

我试过了:testStep.setPropertyValue("Contains", "success");

但这不是它的工作原理。希望任何人都可以帮助我。

谢谢

您可以使用以下代码完成:

import com.eviware.soapui.impl.wsdl.*
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep
import com.eviware.soapui.model.testsuite.TestAssertion
import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.SimpleContainsAssertion
import com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStep

WsdlProject wadlProject = new WsdlProject();    
WsdlTestSuite ts =  wadlProject.addNewTestSuite("HelloTestSuite");
WsdlTestCase tc = ts.addNewTestCase("HelloTestCase1");
WsdlTestStep testStep = tc.addTestStep("httprequest","HelloTestStep1",ip_addr+":"+port,"POST");
TestAssertion assertion = ((HttpTestRequestStep)testStep).addAssertion("Contains");
((SimpleContainsAssertion)assertion).setToken("success");

此代码只是添加 contains 断言以在响应中查找字符串 success 的示例。也可以添加另一种断言,但要考虑 com.eviware.soapui.model.testsuite.TestAssertion.TestAssertion interface returned by addAssertion methods because each one has it's own methods to setup the assertion, in the contains case the implementation is com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.SimpleContainsAssertion 的实现。

这与 addTestStep() 方法的工作方式类似,returns 是 com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep interface, however since your type is "httprequest" the implementation is com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStep 的一个实例...在简历中,如果没有正确的转换,您将无法调用添加断言的必要方法(至少使用 java... 和 groovy 完全没有问题 :)

希望对您有所帮助,