Forum Discussion
Hi Rao, please note that below script works for all other testcases where REST teststep is executed independently. Issue is when REST step is executed from a groovy step.
import net.sf.*
import net.sf.json.*
import net.sf.json.groovy.*
import groovy.sql.Sql
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
import java.text.DateFormat
import com.eviware.soapui.model.testsuite.Assertable
//Get Some info
def TestSource = context.expand('${#TestSuite#TSNAME}')
def FilePath = context.expand('${#TestSuite#ResponsePath}')
def TestExec = context.expand('${#TestCase#RUNTIME}')
def TestCase = context.getTestCase().name
def TestStep = context.getCurrentStep()
def RestStepName = TestStep.getLabel()
def date
def URL
def ResTime
def httpStatusCode
def Response
def FileName
def DataVal
// Append TestStep name to testcase if more than one REST step is in the testcase //
if(context.getTestCase().getTestSteps().size() > 1 && TestStep.config.type == 'restrequest'){
TestCase = TestCase + ' - ' + RestStepName
}else{
TestCase = TestCase
}
//Perform below actions only if TestStep is a REST call //
if (TestStep.config.type == 'restrequest'){
log.info "-------------------------------------------------------"
log.info "Running ResultLogger..."
date = new Date().format("MM/dd/yyyy hh:mm a") //Get current date//
URL = context.httpMethod.getURI() //Get REST URL//
ResTime = context.httpMethod.getTimeTaken() //Get response Time//
// Get REST response //
try{
Response = context.expand('${'+RestStepName+'#Response}').toString()
def json = new JsonSlurper().parseText Response
Response = new JsonBuilder(json).toPrettyString()
Response = groovy.json.StringEscapeUtils.unescapeJava(Response)
}catch(e){
log.info "Exception occured while parsing"
Response = context.expand('${'+RestStepName+'#Response}').toString()
}
//Get Http Status Code //
try{
def httpResponseHeaders = context.testCase.testSteps[RestStepName].testRequest.response.responseHeaders
def httpStatus = httpResponseHeaders["#status#"]
httpStatusCode = (httpStatus =~ "[1-5]\\d\\d")[0]
}catch(e){
httpStatusCode = "ERROR"
}
//## Generate dynamic file name to store responses ##//
FileName = RestStepName + ".txt"
FileName = FilePath + FileName
def jsonfile = new File(FileName)
jsonfile.delete()
log.info "Writing response to $FileName"
jsonfile << Response
//## Capture Assertion Status ##//
def Assertion, ErrorLevel, TestStatus
try{TestStep.getAssertionList().any{
if(it.status.toString() != 'VALID'){
Assertion = 'Fail'
}else {
Assertion = 'Pass'
return
}
}
}catch(e){
log.info "Assertion script didn't run"
}
//##Get TestStatus ##//
if(httpStatusCode == '200' && Assertion == 'Pass' ){
TestStatus = 'PASS'
}else{
TestStatus = 'FAIL'
}
//## Get Error Level ##//
if(httpStatusCode != '200' && TestStatus == 'FAIL'){
ErrorLevel = 'Error at Request Level'
}else if(Assertion == 'Fail' && TestStatus == 'FAIL'){
ErrorLevel = 'Error at Assertion Level'
}else{
ErrorLevel = ''
}
//######################################//
//## Establish SQL Server Connection ##//
//####################################//
def sql =Sql.newInstance("jdbc:sqlserver://DATABASE_SERVER_NAME;Database=DB_Name;integratedSecurity=true","com.microsoft.sqlserver.jdbc.SQLServerDriver")
log.info "Connected to SQL server..."
//## Insert Test Results to SQL Server ##//
SQLQry = ""
SQLQry += "insert into DB_Name.DBO.TEST_RESULTs values ("
SQLQry += "'" + TestSource + "', "
SQLQry += "'" + TestCase + "', "
SQLQry += "'" + URL + "', "
SQLQry += "'" + httpStatusCode + "', "
SQLQry += "'" + ResTime + "', "
SQLQry += "'" + TestExec + "', "
SQLQry += "'" + FileName + "', "
SQLQry += "'" + TestStatus + "', "
SQLQry += "'" + ErrorLevel + "', "
SQLQry += "'" + date + "' "
SQLQry += ")"
log.info "Inserting record in SQL server..."
log.info "SQL statemement is... $SQLQry"
sql.execute SQLQry //Execute SQL statement//
log.info "Closing SQL connection..."
//## Close SQL connections ##//
sql.close() //## SQL Server Connection ##//
log.info "Finished ReportLogger..."
log.info "-------------------------------------------------------"
}
- nmrao7 years agoChampion Level 3Was it a groovy script step? or script from Event handler? If not please provide the same.
- New2API7 years agoFrequent Contributor
This is EventHandler script.
Groovy Step script is:
import com.eviware.soapui.support.XmlHolder
import net.sf.*
import net.sf.json.*
import net.sf.json.groovy.*
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder//## Get test step name //
def currentStepInd = context.currentStepIndex
def testStep = testRunner.testCase.getTestStepAt(currentStepInd).name
def TestStepName = testRunner.testCase.getTestStepAt(currentStepInd - 1).name
def RESTTestStep = testRunner.testCase.getTestStepAt(currentStepInd + 1).namelog.info "Running $testStep..."
log.info "------------------------------------------------"log.info "Collect TestData from Testsuite Properties..."
def USER = context.expand('${#TestSuite#USER}')
//Parse JSON response
log.info "Parsing JSON response from $TestStepName..."
def SECNO = []
def JsonPayload
def i = 0def response = context.expand('${'+TestStepName+'#Response}')
def json = new JsonSlurper().parseText response
json.each{
SECNO += it.SecurityNumber
JsonPayload = it
i++
}
//##Update JSON Nodes ##//
def j = 0
def jsonbuilder = new JsonBuilder(JsonPayload)SECNO.each{
//Update JSON nodes//
jsonbuilder.content.SecurityNumber = SECNO[j]
jsonbuilder.content.UpdatedByUser = USER
//##Update TestCase Property to contain parsed json ##//
testRunner.testCase.setPropertyValue("SECNO", SECNO[j])
testRunner.testCase.setPropertyValue("JSONPayload", "${jsonbuilder}")
log.info "Updating JSON payload to TestCase property: JSONPayload"
log.info "JSON Payload is : " + context.expand('${#TestCase#JSONPayload}')
log.info "------------------------------------------------"
log.info "------------------------------------------------"
testRunner.runTestStepByName(RESTTestStep) // Execute RestStep Here //
j++
}
log.info "Finished $testStep..."
log.info "------------------------------------------------"- nmrao7 years agoChampion Level 3This is lot of code, can you precisely explain what it is intended to do?