Forum Discussion
Thanks for your response Rao,
I am using below groovy code as an assertion script for a test step.
//get the path of the project root
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def projectPath = groovyUtils.projectPath
log.info(" projectPath : "+projectPath)
//get the test suite name
def testSuiteName_appName = messageExchange.modelItem.testStep.testCase.testSuite.getName()
log.info(" testSuiteName_appName : "+testSuiteName_appName)
//get the test case name
def tcaseName_dashboardName = messageExchange.modelItem.testCase.getName()
log.info(" tcaseName_dashboardName : "+tcaseName_dashboardName)
//get the test step name
def testStepName = messageExchange.modelItem.testStep.getName()
log.info(" testStepName : "+testStepName)
if(execution_mode == "testExecution" ) {
File file = new File("/tmp/Assertion_Files/"+testSuiteName_appName+"/"+tcaseName_dashboardName+"/"+testStepName+".txt")
BufferedReader br = new BufferedReader(new FileReader(file));
String fileStatement;
def newString = "";
while ((fileStatement = br.readLine()) != null) {
newString = newString + fileStatement.toString();
}
def expected_data_in_file = newString.trim().replace(' ', '');
expected_data_in_file = expected_data_in_file.replace("\n", "").replace("\r", "");
def splunkRestApiResponse = context.response.toString().trim().replace(' ', '');
splunkRestApiResponse = splunkRestApiResponse.replace("\n", "").replace("\r", "");
log.info(" splunkRestApiResponse "+splunkRestApiResponse);
log.info(" fileResponse trimmed "+expected_data_in_file) ;
assert(splunkRestApiResponse.equals(expected_data_in_file));
log.info(" Assertion Passed ") ;
}
It is working fine. As this is a generic code and all of my teststeps have same groovy assertion script. Is there a way to put this script at one place and then call it from each assertion script. This will help me to reduce my effort whenever there is a change in groovy script.
Just tried converting it into class / methods to achieve the same (equivalent of your script assertion)
The work is divided into two parts.
1. logging various details
2. comparing the response with a file
Hence, two methods created. and call the same in the script assertion.
So configure your script library as shown in the previous reply's documentation link.
And here comes the script assertion part.
//optional if you want to show the log statements Utility.showTestDetails(context, log) //Replace the filepath absolute path; this compares current test step response with given absolute file assert Utility.compare(context.response, '/file/path'), 'error message comparison failure'
If you don't want to trim new lines, then use as below to compare
//Pass 3rd optional argugemtn value as false; assert Utility.compare(context.response, '/file/path', false), 'error message comparison failure'
And here is the Utility class
class Utility { static def sanitize = { text -> text.trim().replace('\n', '').replace('\r', '') } static def showTestDetails(context, log) { log.with{ info "Project Path : ${context.testCase.testSuite.project.path}" info "Suite : ${context.testCase.testSuite.name}" info "Case : ${context.testCase.name}" info "Step : ${context.currentStep.name}" } } static def compare(response, fileName, isReplaceNewLine = true) { def fileContent = new File(fileName).text if (isReplaceNewLine) { sanitize(response) == sanitize(fileContent) } else { ressponse == fileContent } } }
- TanyaYatskovska5 years agoSmartBear Alumni (Retired)
Thanks for such great assistance, Rao, Richie!
jayantjayant, do the suggestions given here help you resolve the issue?
Related Content
- 5 years agoSiKing
- 2 years agoM_Schofer_1111
- 8 months agoalinabond