radicarl
radicarl

Reputation: 327

Changing the request XML in Load Script

I have a soapui project for a big IT-System which offers hundreds of Webservice, only some of the webservice are in my project. Everytime I add a new WSDL to my project, I have to do a lot of monkey work to adjust the xml for the request (like adding placeholder for properties, removing comments, replace questionmarks....).

I thought I could easily write a "Load Script" for the project in which I register some Listener, and everytime a new request is added the script will fetch the xml-request from the new request-object, change it and save it back. But I failed, because I have no clue how to even get the xml from the request.

This is how far I got:

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.
        }
    }
}

I looked in every property of the WsdlRequest-Instance and could not find any XML. I tried to create a context (load Script does not provide a context instance) and somehow get the XmlHolder from it, but failed (Unexpected element: CDATA). All examples I found, where run in a TestCase/Step and I was not able to transfer them to the Load Script of the project.

Can someone help me?

Edit:

I want to change the request-data of new request (4. in the picture) to the example above (3. in the picture). I thought, I could solve it by using the load script of the project (2. in the picture). My approach was to register a InterfaceListener in every Interface and react to the creation of new Requests (method requestAdded). This works, but I did not found a way to retrieve the request-data from the request-object. enter image description here

Upvotes: 0

Views: 554

Answers (2)

radicarl
radicarl

Reputation: 327

Because of the help and effort from Rao (thanks again) I did the following workaorund:

  • Register listener for new request in the load script
  • on request creation, write the interface- and the request-name as custom properties to the project (operation-name is in my case always the same)
  • the customazing is done with a special testcase which i run afterwards

The load script from the project:

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())
    }
  }
}

The setup script of the empty testcase which I run manually after request creation:

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", "")

Upvotes: 0

Rao
Rao

Reputation: 21379

Neither it is possible to access request in requestAdded() method, nor it is possible to assign a request by user.

It is learnt after debugging into SoapUI's code, it is not possible to read the request in the implementation of requestAdded method of interface InterfaceListener.

requestAdded() method is fired as soon as user click on New Request and at that time, the request content is empty / null.

Later, SoapUI will assign the request by creating a new one and then the request is being shown to the user.

However, you may try to write an individual groovy script to achieve the same once you create the requests.

Upvotes: 1

Related Questions