我们如何使用 groovy 脚本在测试用例中添加 SOAP 请求测试步骤

How can we add SOAP request Test step in a Test Case using groovy scripts

我正在寻找在测试用例中添加 SOAP 请求测试步骤,来自不同的 TestSuite 和测试用例,我已经编码该部分以添加 Groovy 脚本以满足相同的要求但无法添加SOAP 请求测试步骤。有帮助吗?

以下是我的代码:

import com.eviware.soapui.impl.wsdl.teststeps.registry.GroovyScriptStepFactory

suite = context.testCase.testSuite.project.addNewTestSuite("AutomatedTestSuite")
tc = suite.addNewTestCase("automatedTestCase")
gs = tc.addTestStep( GroovyScriptStepFactory.GROOVY_TYPE, "GroovyScript1" )
gs2 = tc.addTestStep( GroovyScriptStepFactory.GROOVY_TYPE, "GroovyScript3" )
gs.properties["script"].value = 'log.info(\'hello world\')'

您可以通过项目的名称获取另一个testSuite、testCase和testStep,如下所示:

def project = context.testCase.testSuite.project
def testStep = project.testSuites['TestSuiteName'].testCases['TestCaseName'].testSteps['testStepName']

或者使用 getXXXXbyName 方法代替数组方法:

def testStep = project.getTestSuiteByName('TestSuiteName').getTestCaseByName('TestCaseName').getTestStepByName('testStepName')

然后要将此 testStep 添加到您的测试用例中,您可以使用 cloneStep(WsdlTestStep testStep, String name) 方法。

全部在你的脚本中:

def suite = context.testCase.testSuite.project.addNewTestSuite("AutomatedTestSuite")
def tc = suite.addNewTestCase("automatedTestCase")

// get desired testStep
def project = context.testCase.testSuite.project
def testStep = project.testSuites['TestSuiteName'].testCases['TestCaseName'].testSteps['testStepName']
// add it to your new generated testCase
tc.cloneStep(testStep,testStep.name + "_Copy")

根据评论进行编辑

如果您想创建一个新的而不是另一个 SOAP testStep 的副本,您可以使用以下代码来完成。考虑到创建 SOAP 类型的 testStep 比创建 groovy 时需要更多信息,因为需要 wsdl 操作信息(在示例中我们采用第一个,但如果您有超过一个照顾你拿什么)。

IMO 第一种方法更简单,你可以复制另一个 testStep 并更改你想要的属性......无论如何,如果你想这样做,你是:

import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory
def suite = context.testCase.testSuite.project.addNewTestSuite("AutomatedTestSuite")
def tc = suite.addNewTestCase("automatedTestCase")

// get the WSDL operation... for the example we take the first one
// however if you've more get the correct one
def operation = testRunner.testCase.testSuite.project.getInterfaceAt(0).getOperationList()[0]
// factory to create the testStepConfig
def factory = new WsdlTestRequestStepFactory()
def config = factory.createConfig(operation,'stepName') 
// create the testStep
def testStep = tc.addTestStep(config)
// change the request
testStep.properties['Request'].value = '<request>someData</request>'

希望对您有所帮助,