将多个XML文件导入SoapUI

时间:2015-01-23 13:49:16

标签: xml soap groovy soapui

几个月前开始了一项新工作,需要基本的日常使用SoapUI来执行需要进行视觉验证的批量测试(我们还没有自动化视觉部分)。

但是,我现在有一大堆新的xmls文件需要导入到项目中,我想知道是否有人有一个groovy脚本或其他东西,导入文件,并使用文件中的内容作为要传递的xml文本。通过HTTP请求。

基本上,我想要albciff所做的here。但是将我的xml文件转换为HTTP步骤。

我已经尝试修改他的脚本以包含正确的HTTP类,但是我得到了一个我无法解决的异常

更新28/01

我只是使用SoapUI的免费/标准版。我的编码/脚本知识有限,因为我只是一名功能测试员:(

我当前的groovy脚本是

import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory
import com.eviware.soapui.impl.wsdl.teststeps.registry.HttpRequestStepFactory
import groovy.io.FileType

// 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")
// create the factory to create testSteps
def testStepFactory = new HttpRequestStepFactory()

def requestDir = new File("C://directory//..//final_dir")
// for each xml file in the directory
requestDir.eachFileRecurse (FileType.FILES) { file ->
  def newTestStepName = file.getName()
  // create the config
  def testStepConfig = testStepFactory.createConfig( tsTemplate.getOperation(), newTestStepName )
  // add the new testStep to current testCase
  def newTestStep = tc.insertTestStep( testStepConfig, -1 )
  // set the request which just create with the file content
  newTestStep.getTestRequest().setRequestContent(file.getText())
}

运行时遇到的异常是

  

groovy.lang.MissingMethodException:没有方法签名:   com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStep.getOperation()   适用于参数类型:()值:[]可能的解决方案:   第一行的getAssertions()错误:14

1 个答案:

答案 0 :(得分:0)

你差不多:),尝试这种方式(使用HttpRequestStepFactory.createNewTestStep代替HttpRequestStepFactory.createConfig):

import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory
import com.eviware.soapui.impl.wsdl.teststeps.registry.HttpRequestStepFactory
import groovy.io.FileType

// 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")
// create the factory to create testSteps
def testStepFactory = new HttpRequestStepFactory()
def requestDir = new File("C://directory//..//final_dir")
// for each xml file in the directory
requestDir.eachFileRecurse(FileType.FILES){ file ->
  def newTestStepName = file.getName()
  // get the template endpoint
  def endpoint = tsTemplate.getPropertyValue('Endpoint')
  // create the config using endpoint and your method (post in your case)
  def testStepConfig  = testStepFactory.createNewTestStep(tc,newTestStepName,endpoint,'POST')
  // add the new testStep to current testCase
  def newTestStep = tc.insertTestStep( testStepConfig, -1 )
  // set the request which just create with the file content
  def testRequest = newTestStep.getTestRequest()
  testRequest.setRequestContent(file.getText())

  // UPDATED CODE BELOW BASED ON COMMENT

  // you can use in this case HttpRequestConfig (due the type of your
  // testStep you can use this class) to set some additional properties 
  // in your TestStep
  def httpReqCfg = testRequest.getConfig();
  // for example for mediaType as you said in your comment
  httpReqCfg.setMediaType('application/json');

}

请注意,使用HttpTestRequestStep.clone method看起来更方便。但是当你在groovy中调用它时,testStep被创建但是groovy抛出了跟随异常(我认为这里有一些SOAPUI API有问题):

  

java.lang.ClassCastException:com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStep无法强制转换为com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep

但是,由于我认为您只对使用文件内容进行设置感兴趣 请求上面提出的代码就足够了。

基于评论的编辑:

您还可以使用HttpRequestConfig设置mediaType或在评论中添加其他参数,查看HttpRequestConfig API,我还会更新上面的代码以显示你举了一个关于如何获得这个对象的例子,并setMediaType看一下它。

希望这有帮助,