Forum Discussion

Said's avatar
Said
Contributor
4 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 ...
  • Said's avatar
    4 years ago

    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