dva1946
14 years agoOccasional Contributor
Custom Logging in Groovy Script
Objective: Simple General File Logging
Use: General in Groovy Scripts
What Will This Logging Do? Capture the REQUEST & RESPONSE of each Web Service
//**TestCase Properties **//
logDirectoryName = C:\Users\Dave\Documents\SoapUI-Logs\
testCaseLogFileName = createPurchaseStreamingURL
//** LOG FILE DEFINITION - Place somewhere at the top of the script **//
def logDirectoryName = context.expand( '${#TestCase#logDirectoryName}' )
def testCaseLogFileName = context.expand( '${#TestCase#testCaseLogFileName}' )
def filext = ".log";
def fn = "$logDirectoryName$testCaseLogFileName$filext"
def FileName = new PrintWriter( new FileWriter(fn));
// Add for optional timestamp logging
def sdf = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
def myTime = "";
//** GENERIC LOGGING FOR STEPS RUN **//
def numberOfSteps = testRunner.testCase.getTestStepCount(); //just a bonus piece of our logging to show step count
if (dEBUG != 0 ) {log.info "Step Count = $numberOfSteps"} // Print out to review it
def eaStep = "";
testRunner.testCase.getTestStepList().each() {
eaStep = it.getName()
if (dEBUG != 0 ) {log.info " This step = $eaStep"} //Print out so you can see the step names
// You want to skip STEPS that are not Web Service Requests
if ((eaStep =~ 'Groovy') || (eaStep =~ 'DataGen') ||(eaStep =~ 'Property Transfer')) {
// Enhance as neede to skip more types of steps
// We just skip this area
//log.info(it.getName())
}
else {
//Here we generate just a list of WEB SERVICE STEPS.
log.info(it.getName()) // can print to screen if U want. Comment out when done testing it.
def request = testRunner.testCase.getTestStepByName( "$eaStep" ); // Get the WS
def req = request.getProperty( "Request" ); // Get the request of the WS
def resp = request.getProperty( "Response" ); // Get the response of the WS
FileName.println( "$eaStep - Request:" ); // Print WS Name & Request:
FileName.println( req.value ); // Print WS request
FileName.println( "$eaStep - Response:" ); // Print WS Name & Response:
FileName.println( resp.value ); // Print WS respone
}
}
//** END OF GENERIC LOGGING **//
//** Close Everything **//
FileName.flush();
FileName.close();
//** Your Output **//
Has now been written to a file.
//** Option Timestamp Logging around a STEP **//
myTime = sdf.format( new Date());FileName.println( "$myTime Start getOffering" ); //timestamp
testRunner.runTestStepByName("getOffering"); // step
myTime = sdf.format( new Date());FileName.println( "$myTime End getOffering" ); //timestamp
Use: General in Groovy Scripts
What Will This Logging Do? Capture the REQUEST & RESPONSE of each Web Service
//**TestCase Properties **//
logDirectoryName = C:\Users\Dave\Documents\SoapUI-Logs\
testCaseLogFileName = createPurchaseStreamingURL
//** LOG FILE DEFINITION - Place somewhere at the top of the script **//
def logDirectoryName = context.expand( '${#TestCase#logDirectoryName}' )
def testCaseLogFileName = context.expand( '${#TestCase#testCaseLogFileName}' )
def filext = ".log";
def fn = "$logDirectoryName$testCaseLogFileName$filext"
def FileName = new PrintWriter( new FileWriter(fn));
// Add for optional timestamp logging
def sdf = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
def myTime = "";
//** GENERIC LOGGING FOR STEPS RUN **//
def numberOfSteps = testRunner.testCase.getTestStepCount(); //just a bonus piece of our logging to show step count
if (dEBUG != 0 ) {log.info "Step Count = $numberOfSteps"} // Print out to review it
def eaStep = "";
testRunner.testCase.getTestStepList().each() {
eaStep = it.getName()
if (dEBUG != 0 ) {log.info " This step = $eaStep"} //Print out so you can see the step names
// You want to skip STEPS that are not Web Service Requests
if ((eaStep =~ 'Groovy') || (eaStep =~ 'DataGen') ||(eaStep =~ 'Property Transfer')) {
// Enhance as neede to skip more types of steps
// We just skip this area
//log.info(it.getName())
}
else {
//Here we generate just a list of WEB SERVICE STEPS.
log.info(it.getName()) // can print to screen if U want. Comment out when done testing it.
def request = testRunner.testCase.getTestStepByName( "$eaStep" ); // Get the WS
def req = request.getProperty( "Request" ); // Get the request of the WS
def resp = request.getProperty( "Response" ); // Get the response of the WS
FileName.println( "$eaStep - Request:" ); // Print WS Name & Request:
FileName.println( req.value ); // Print WS request
FileName.println( "$eaStep - Response:" ); // Print WS Name & Response:
FileName.println( resp.value ); // Print WS respone
}
}
//** END OF GENERIC LOGGING **//
//** Close Everything **//
FileName.flush();
FileName.close();
//** Your Output **//
Has now been written to a file.
//** Option Timestamp Logging around a STEP **//
myTime = sdf.format( new Date());FileName.println( "$myTime Start getOffering" ); //timestamp
testRunner.runTestStepByName("getOffering"); // step
myTime = sdf.format( new Date());FileName.println( "$myTime End getOffering" ); //timestamp