Error Hunter
Error Hunter

Reputation: 1684

how to write all Soap Request available in Project using Soap UI Groovy

I have a soap project with 4 Test Suite,each Test Suite has some Test Case and each Test case has some test steps[Soap Request,Groovy Script] I am able to access all the properties using mentioned code below , HOWEVER,code is writing blank request/response file in local system **

def Project = testRunner.testCase.testSuite.project;
for(def i=0;i<Project.testSuiteCount;i++)
{
log.info Project.getTestSuiteAt(i).name 
def Suite = Project.getTestSuiteAt(i)
  for(def j=0;j<Suite.testCaseCount;j++)
  {  
   log.info Suite.getTestCaseAt(j).name
   def TCase = Suite.getTestCaseAt(j)
    for(def k=0;k < TCase.testStepCount;k++)
    {
    def TStep= TCase.getTestStepAt(k)
    def req =  context.expand('${'+TStep.name+'#Request}')
    new File("D:/Directory/"+Suite.name+"_"+TCase.name+"_"+TStep.name+"_"+k+".txt").write( req )
    }
  }
}

** plz help to solve this, **

Upvotes: 2

Views: 880

Answers (1)

Rao
Rao

Reputation: 21389

Actually there are multiple approaches to achieve this. Below is the one of the approach.

Here is Groovy Script which writes the requests and responses.

This script assumes that user has already run the test suites so that responses are available to be saved.

Create a new test suite -> test case -> add Groovy Script test step and copy the below script into it.

Groovy Script:

/**
* This groovy script saves the request and response
* And this script will be able to write the responses
* into files if and only if there is response available
**/
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep

//Change direcoty path if required
def directoryToSave = 'D:/directory'

//Not required to change the script beyond this point

//date time is appended to the file name
def dt = new Date().format('yyyyMMdd_HHmmss')

//Closure to save the file
def saveToFile(file, content) {
    if (!file.parentFile.exists()) {
         file.parentFile.mkdirs()
         log.info "Directory did not exist, created"
    }
    if (content) {
        log.info "Writing the content into file :${file.name}"
    file.write(content) 
    assert file.exists(), "${file.name} not created"
    } else {
        log.warn "the content is empty, not writing the content into file"
    }
}

//Get the project object
def project = context.testCase.testSuite.project

//Loop thru the project and save the request and responses
project.testSuiteList.each { suite ->
    suite.testCaseList.each { kase ->
                kase.testStepList.each { step ->
            if (step instanceof WsdlTestRequestStep) {
                def reqFilePath = new File("${directoryToSave}/${suite.name}_${kase.name}_${step.name}_request${dt}.xml")   
                def resFilePath = new File("${directoryToSave}/${suite.name}_${kase.name}_${step.name}_response${dt}.xml")  
                saveToFile(reqFilePath, step.testRequest.requestContent)
                saveToFile(resFilePath, step.testRequest.responseContent)
saveToFile(step)
            } else {
                log.info "Ignoring as the step type is not Soap request"
            }
        }
    }
}

Upvotes: 2

Related Questions