在SOAP UI开源中读取外部XML并将其用作请求

问题描述 投票:0回答:2

我正在使用SOAPUI FREE。我的项目要求是从某个位置选择Request XML(可能有数百个),并将其作为请求使用。是否可以使用任何功能或免费版的Groovy脚本

groovy soapui
2个回答
2
投票

如果您在某个目录中有SOAP xml请求,并且想要从那里选择每个文件并为每个文件创建一个新的TestStep,则可以执行以下操作:

创建一个新的TestCase并在其中添加一个新的SOAP TestStep,它将用作模板轻松创建新的SOAP,然后添加一个groovy TestStep,并使用下一个代码在其中创建新的测试步骤。相同的TestCase(我将注释放入代码中以解释其工作原理):

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

// get the current testCase to add testSteps later
def tc = testRunner.testCase;
// get the SOAP TestStep as template to create the other requests
def tsTemplate = tc.getTestStepByName("MyRequest");
// create the test step factory to use later
def testStepFactory = new WsdlTestRequestStepFactory();

// now get all the request from a specific location...

// your location
def directory = new File("C:/Temp/myRequests/")
// for each file in the directory
directory.eachFile{ file -> 
    // use file name as test step name 
    def testStepName = file.getName()
    // create the config
    def testStepConfig = testStepFactory.createConfig( tsTemplate.getTestRequest(), testStepName)
    // add the new testStep to TestCase
    def newTestStep = tc.insertTestStep( testStepConfig, -1 ) 
    // set the request from the file content
    newTestStep.getTestRequest().setRequestContent(file.getText())   
};

[我认为您是在询问SOAP TestStep,但是请注意,此代码是创建SOAP TestStep请求,创建REST TestStep请求或其他类型的TestStep的过程,您必须更改与testStepFactory(WsdlTestRequestStepFactory)。

此外,对于我来说,如果您要为每个请求创建测试步骤,还是希望从常规脚本运行所有请求而不创建测试步骤,如果第二个是您的意图,则不清楚您可以在groovy脚本中使用SOAPUI中包含的apache-http-client类从您的目录发送请求。

希望这会有所帮助,


0
投票

您的帖子很有帮助,但是您能告诉我如何针对REST请求执行同样的操作吗?我尝试了几种方法,但无法获得适当的解决方案。.您能指导我如何将其用于REST吗?

© www.soinside.com 2019 - 2024. All rights reserved.