Forum Discussion

ibekacyril's avatar
ibekacyril
Occasional Visitor
6 years ago
Solved

Create array of JSON objects using groovy in soapui

I am trying to create an array of JSON objects when I run my script.

So I decided to store the errors or no error for each assertion created within each teststep. Each of the test steps are then stored in its test case.

The issue I am having is that only the last test step is created. Really appreciate some help.

Here is my groovy script:

class ErrorClass {
                def message

                public String toString() {
                                return this.message;
                }
}

class AssertClass {
                def assertionName
                def assertionStatus
                Object message;

                public String toString() {
                                return this.assertionName + this.assertionStatus + this.message;
                }
}

class TestStepClass {
                def testStepName
                def testStepStatus
                Object assertList

                public String toString() {
                                return this.testStepName + this.testStepStatus + this.assertList;
                }
}

class TestCaseClass {
                def testCaseName
                Object testStepList

                public String toString() {
                                return this.testCaseName + this.testStepList;
                }
}

List<String> errorList

List<String> assertList

List<String> testStepList

List<String> testCase = new ArrayList<String>();

//## Get TestSuite and TestCase names ##//
def TSName = testRunner.testCase.testSuite.name
def TCName = testRunner.testCase.name

for( r in testRunner.results ) {
                if ( r.testStep instanceof com.eviware.soapui.model.testsuite.Assertable) {
                                testStepList = new ArrayList<String>();
                                for(assertion in r.testStep.assertionList ) {
                                                assertList = new ArrayList<String>();
                                                errorList = new ArrayList<String>();
                                for(e in assertion.errors) {
                                                errorList.add(new ErrorClass(message: e.message))
                                }
                                assertList.add(new AssertClass(assertionName: assertion.label, assertionStatus: assertion.status, message: errorList));
                                }
                                testStepList.add(new TestStepClass(testStepName: r.testStep.name, testStepStatus: r.status, assertList: assertList))
               }
}
testCase.add(new TestCaseClass(testCaseName: TCName, testStepList: testStepList))

def json = new JsonBuilder(testCase)
log.info(json)

 

  •  

     

    This bit:

    for(assertion in r.testStep.assertionList ) {
        assertList = new ArrayList<String>();
        errorList = new ArrayList<String>();
        for(e in assertion.errors) {
            errorList.add(new ErrorClass(message: e.message))
        }
        assertList.add(new AssertClass(assertionName: assertion.label, assertionStatus: assertion.status, message: errorList));
    }

    should be like this:

    assertList = new ArrayList<String>();
    for(assertion in r.testStep.assertionList ) { 
    errorList = new ArrayList<String>();
    for(e in assertion.errors) {
    errorList.add(new ErrorClass(message: e.message))
    }
    assertList.add(new AssertClass(assertionName: assertion.label, assertionStatus: assertion.status, message: errorList));
    }

     

     

     

     

2 Replies

  • JHunt's avatar
    JHunt
    Community Hero

    (deleted - sorry, I responded too quickly)

    • JHunt's avatar
      JHunt
      Community Hero

       

       

      This bit:

      for(assertion in r.testStep.assertionList ) {
          assertList = new ArrayList<String>();
          errorList = new ArrayList<String>();
          for(e in assertion.errors) {
              errorList.add(new ErrorClass(message: e.message))
          }
          assertList.add(new AssertClass(assertionName: assertion.label, assertionStatus: assertion.status, message: errorList));
      }

      should be like this:

      assertList = new ArrayList<String>();
      for(assertion in r.testStep.assertionList ) { 
      errorList = new ArrayList<String>();
      for(e in assertion.errors) {
      errorList.add(new ErrorClass(message: e.message))
      }
      assertList.add(new AssertClass(assertionName: assertion.label, assertionStatus: assertion.status, message: errorList));
      }