在加载脚本中更改请求XML [英] Changing the request XML in Load Script
本文介绍了在加载脚本中更改请求XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个提供数百个Web服务的大型IT系统的Soapui项目,只有一些Web服务在我的项目中。每次我向我的项目添加一个新的wsdl时,我必须做很多简单的工作来调整请求的XML(比如为属性添加占位符、删除注释、替换问号……)。
我认为我可以很容易地为项目编写一个"加载脚本",在其中注册一些侦听器,每次添加新的请求时,脚本都会从新的请求对象中获取XML请求,对其进行更改并保存回来。但我失败了,因为我甚至不知道如何从请求中获取XML。
这是我得到的结果:
import com.eviware.soapui.SoapUI
import com.eviware.soapui.model.support.ProjectListenerAdapter
import com.eviware.soapui.model.support.InterfaceListenerAdapter
import com.eviware.soapui.model.iface.*
import com.eviware.soapui.impl.wsdl.WsdlRequest
import com.eviware.soapui.impl.wsdl.WsdlContentPart
import com.eviware.soapui.model.propertyexpansion.DefaultPropertyExpansionContext
import com.eviware.soapui.support.XmlHolder
final logger = log
SoapUI.log.info("Load Script executet")
for(Interface i : project.getInterfaceList()){
i.addInterfaceListener(new RequestAddListener())
}
project.addProjectListener(new ProjectListenerAdapter(){
public void interfaceAdded(Interface i){
SoapUI.log.info("Interface added: "+ i.getName())
i.addInterfaceListener(new RequestAddListener())
}
})
public class RequestAddListener extends InterfaceListenerAdapter {
public void requestAdded(Request r) {
if(r instanceof WsdlRequest){
WsdlRequest request = (WsdlRequest) r
SoapUI.log.info("Request added: "+request.getName())
// here i want to retrieve the xml-request from the request-object,
// do something with it, and set it back to the request-object.
}
}
}
我查看了WsdlRequest实例的每个属性,但没有找到任何XML。我试图创建一个上下文(加载脚本不提供上下文实例),并以某种方式从其中获取XmlHolder,但失败了(意外元素:CDATA)。我找到的所有示例,其中在TestCase/步骤中运行,并且我无法将它们传输到项目的加载脚本。
有人能帮帮我吗?
编辑:
我想将新请求的请求数据(图片中的4.)更改为上面的示例(图片中的3.)。我想,我可以通过使用项目的加载脚本(图片中的2.)来解决它。我的方法是在每个接口中注册一个InterfaceListener,并对新请求的创建做出反应(方法requestAdded)。这是可行的,但是我没有找到从请求对象中检索请求数据的方法。
推荐答案
由于help and effort from Rao(再次感谢),我做了以下工作:
- 在加载脚本中为新请求注册侦听器
- 在创建请求时,将接口和请求名称作为自定义属性写入项目(在我的例子中,操作名称总是相同的)
- 定制是通过一个特殊的测试用例完成的,之后我会运行该测试用例
项目中的加载脚本:
import com.eviware.soapui.model.support.ProjectListenerAdapter
import com.eviware.soapui.model.support.InterfaceListenerAdapter
import com.eviware.soapui.model.iface.*
import com.eviware.soapui.model.project.Project
import com.eviware.soapui.impl.wsdl.WsdlRequest
// Request-Listener for all existing interfaces
for(Interface i : project.getInterfaceList()){
i.addInterfaceListener(new RequestAddListener())
}
// this Project-Listener adds a Request-Listener to all new Interfaces
project.addProjectListener(new ProjectListenerAdapter(){
public void interfaceAdded(Interface i){
i.addInterfaceListener(new RequestAddListener())
}
})
public class RequestAddListener extends InterfaceListenerAdapter {
public void requestAdded(Request r) {
if(r instanceof WsdlRequest){
Operation o = r.getOperation()
Interface i = o.getInterface()
Project p = i.getProject()
// save name of request and interface as project-property
p.setPropertyValue("lastAddedRequest.Request", r.getName())
p.setPropertyValue("lastAddedRequest.Interface", i.getName())
}
}
}
创建请求后手动运行的空测试用例的安装脚本:
import com.eviware.soapui.model.iface.*
import com.eviware.soapui.model.testsuite.TestCase
import com.eviware.soapui.model.project.Project
def project = testCase.getProject()
def i = project.getInterfaceByName(project.getPropertyValue("lastAddedRequest.Interface"))
// in my case all operations are named execute
def request = i.getOperationByName("execute").getRequestByName(project.getPropertyValue("lastAddedRequest.Request"))
def requestData = request.getRequestContent()
// change the xml with some regexs
// put the xml back
request.setRequestContent(requestData)
project.setPropertyValue("lastAddedRequest.Interface", "")
project.setPropertyValue("lastAddedRequest.Request", "")
这篇关于在加载脚本中更改请求XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文