Forum Discussion

Said's avatar
Said
Contributor
3 years ago

how to collect assertion message from Assertion Teststep

Hi,

 

For reporting I have a test step that calls below method in script library. This method collects all assertion messages from al test steps in the running test case.

 

public static String getTestCaseRunAssertionMessages( def testRunner, def context, def log ) {
	log.debug "*** Method getTestCaseRunAssertionMessages ***"
	def testStepList = testRunner.getTestCase().getTestStepList()
	String assertErrors = ""
	testStepList.each {
		if ( it.isDisabled() == false ) {
			if ( it.metaClass.hasProperty( it, 'assertionStatus' ) ) {
				log.debug "Collecting assertion messages in: " + it.getName();
				for( assertion in it.assertionList ) {
					for( e in assertion.errors ) {
						log.debug "Failed assertions found in: " + it.getName();
						if ( assertErrors.equals("") ) {
							assertErrors = it.name + ": " + e.message;
						} else {
							assertErrors = assertErrors + "\n" + it.name + ": " + e.message;
						}
					}
				}
			}
		}
	}
	return assertErrors
}

 

My issue: the statement for( e in assertion.errors ) { does not go into the loop when the teststep is an "Assertion" test step. This assertion step has failed assertion. Is there is work around for this?

  • With help from Smartbear support I found the solution. For assertion test step the code to collect assertion messages is slightly different: see below...

     

     

    public static String getTestCaseRunAssertionMessages( def testRunner, def context, def log ) {
    	log.debug "*** Method getTestCaseRunAssertionMessages ***"
    	def testStepList = testRunner.getTestCase().getTestStepList()
    	String assertErrors = ""
    	if (context.requestAttempt >= 3 ) {
    		assertErrors = "Stop processing after " + context.requestAttempt + " request attempts";
    	}
    	testStepList.each {
    		if ( it.isDisabled() == false ) {
    			log.debug it.getClass()
    			// Collecting assertion messages differs from AssertionTestStep and wsdl/rest steps
    			if ( it.getClass().toString().contains( "AssertionTestStep") ) {
    				log.debug "*** AssertionTestStep ***"
    				def list = it.getAssertionEntryList()
    				for( assertion in list) {
    					for (err in assertion.getAssertion().getErrors()) {
    						log.debug "Failed assertions found in: " + it.getName();
    						if ( assertErrors.equals("") ) {
    							assertErrors = it.name + ": " + err.getMessage();
    						} else {
    							assertErrors = assertErrors + "\n" + it.name + ": " + err.getMessage();
    						}
    					}
    				}
    			} else if ( it.metaClass.hasProperty( it, 'assertionStatus' ) ) {
    				log.debug "Collecting assertion messages in: " + it.getName();
    				for( assertion in it.assertionList ) {
    					for( e in assertion.errors ) {
    						log.debug "Failed assertions found in: " + it.getName();
    						if ( assertErrors.equals("") ) {
    							assertErrors = it.name + ": " + e.message;
    						} else {
    							assertErrors = assertErrors + "\n" + it.name + ": " + e.message;
    						}
    					}
    				}
    			}
    		}
    	}
    	return assertErrors

     

     

    • Said's avatar
      Said
      Contributor

      I checked ready-api-errors.log: no stacktrace.

  • Said's avatar
    Said
    Contributor

    With help from Smartbear support I found the solution. For assertion test step the code to collect assertion messages is slightly different: see below...

     

     

    public static String getTestCaseRunAssertionMessages( def testRunner, def context, def log ) {
    	log.debug "*** Method getTestCaseRunAssertionMessages ***"
    	def testStepList = testRunner.getTestCase().getTestStepList()
    	String assertErrors = ""
    	if (context.requestAttempt >= 3 ) {
    		assertErrors = "Stop processing after " + context.requestAttempt + " request attempts";
    	}
    	testStepList.each {
    		if ( it.isDisabled() == false ) {
    			log.debug it.getClass()
    			// Collecting assertion messages differs from AssertionTestStep and wsdl/rest steps
    			if ( it.getClass().toString().contains( "AssertionTestStep") ) {
    				log.debug "*** AssertionTestStep ***"
    				def list = it.getAssertionEntryList()
    				for( assertion in list) {
    					for (err in assertion.getAssertion().getErrors()) {
    						log.debug "Failed assertions found in: " + it.getName();
    						if ( assertErrors.equals("") ) {
    							assertErrors = it.name + ": " + err.getMessage();
    						} else {
    							assertErrors = assertErrors + "\n" + it.name + ": " + err.getMessage();
    						}
    					}
    				}
    			} else if ( it.metaClass.hasProperty( it, 'assertionStatus' ) ) {
    				log.debug "Collecting assertion messages in: " + it.getName();
    				for( assertion in it.assertionList ) {
    					for( e in assertion.errors ) {
    						log.debug "Failed assertions found in: " + it.getName();
    						if ( assertErrors.equals("") ) {
    							assertErrors = it.name + ": " + e.message;
    						} else {
    							assertErrors = assertErrors + "\n" + it.name + ": " + e.message;
    						}
    					}
    				}
    			}
    		}
    	}
    	return assertErrors