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

时间:2016-02-23 08:20:27

标签: groovy soapui

我正在寻找在测试用例中添加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\')'

1 个答案:

答案 0 :(得分:3)

您可以通过项目获取另一个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添加到testCase,您可以使用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的副本,则可以使用以下代码执行此操作。考虑到创建一个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>'

希望它有所帮助,