MushyPeas
MushyPeas

Reputation: 2507

How to generate requests and responses for all operations of a WSDL in soapUI using a groovy script?

I have a WSDL which has multiple operations. For each op i want a template .xml with its response and request.

I know how to do this manually in soapUI but I would like to generate them using a groovy script. I googled a lot already, but seems I'm the only one who is looking for this.

My service has 16 Operations, so to do this manual would be too much time. Since the service gets updates every 2 months, an automation using a test step would be perfect.

I managed to do it for the requests already:

right-click on ´services´ in left tree, ´Generate Test Suite´, ´Single Test Case with one Request for each Operation´

then I loop through those Test Step Requests and store them on my disk.

    import com.eviware.soapui.impl.wsdl.teststeps.*

    for( testCase in testRunner.testCase.testSuite.getTestCaseList() ) 
    {
        for( testStep in testCase.getTestStepList() ) 
        {
            if( testStep instanceof WsdlTestRequestStep ) 
            {
                log.info "operation name: " +testStep.getName()

                // create file name
                Date startTime = new Date();
                def cur_Time = startTime.getMonth() + "_" + startTime.getDate();
                cur_Time = cur_Time + "_" + startTime.getHours() + startTime.getMinutes() +startTime.getSeconds()
                def fileName = testStep.getName() + "_" + cur_Time

                def inputFileRequest = new File("T:\\"+ "Request_" + fileName+".txt")
                def inputFileResponse = new File("T:\\"+ "Response_" + fileName+".txt")
                // write request to file
                inputFileRequest.write(testStep.getProperty("request").value)
            }
        }
    }

But I havent figured out a way to do this also for the resposes. If i use getProperty("reponse") it's null of course.

Any hint? :)

Upvotes: 3

Views: 6586

Answers (2)

Hanumant
Hanumant

Reputation: 1

This is great , even I am also working on the same. As of now I am taking xml request from a folder but I just want to get the Request from WSDL itself and want to get it's parameter.

try{

    //Hitting the WSDLs one by one
    wsdlList.each
    {
            wsdl ->
            wsdlToHit=wsdl
            log.info("WSDL To Hit :" + wsdlToHit)


            // Creating an interface
            log.info("Before Interface Creation")
            iface= WsdlInterfaceFactory.importWsdl( project,wsdl, false )[0]
            //iface= WsdlInterfaceFactory.importWsdl( project,WSDLFile, false )[0]
            log.info("After Interface Creation")



                    if(Operation == "xyz")
                    {                   
                    requestXML= requestXML1  
                    responseActual= responseActual1
                    expectedActual=expectedActual1
                    }
                    if(Operation == "abc")
                    {                   
                    requestXML= requestXML2 
                    responseActual= responseActual2
                    expectedActual=expectedActual2
                    }
                        requestXML.each
                    {                       
                    request1 ->
                    def wsdlReqDir=request1
                    log.info("RequestLocation : " + wsdlReqDir)

                    File fl = new File(wsdlReqDir)
                    File[] wsdlDirFiles = fl.listFiles()
                    log.info("XML Files in Request Folder : " + wsdlDirFiles)

                        if(wsdlDirFiles.size()>0)
                        {                           
                            wsdlDirFiles.each
                            {
                                wsdlFile->
                                log.info("Request XML file to Send :" + wsdlFile)

                                //Calling the function to hit the service
                                sendRequest(wsdlFile,iface,Operation,Report_File_LOC,requestXML,responseActual,propData)
                                reportFilewriter.flush()    


                            }   

                        }

                    }   

    //removing Interface created
    removeInterface(wsdl)
    log.info("Removed iface : "  + wsdl)
    reportFilewriter.flush()    





    }

Thanks, Hanumant

Upvotes: 0

MushyPeas
MushyPeas

Reputation: 2507

and the winner is, I figured it out myself:

map = context.testCase.testSuite.project.interfaces["services"].operations

for (entry in map)
{
    opName = entry.getKey()
    inputFileRequest = new File("T:\\" + opName + "Request.xml")
    inputFileResponse = new File("T:\\" + opName + "Response.xml")

    inputFileRequest.write(entry.getValue().createRequest(true))
    inputFileResponse.write(entry.getValue().createResponse(true))
}

Upvotes: 4

Related Questions