Ruwi
Ruwi

Reputation: 11

How to run script assertion while running the test step?

I am new to groovy script and soap UI. i added script assertion to a test step. how ever when i run the test case, script assertions are not running. i have to manually run it to verify my response is correct. can anyone help me on this please? My groovy script test assertion:

import groovy.json.JsonSlurper

//grab response
def response = messageExchange.response.responseContent

//Convert to JsonSluper to access data
def list = new JsonSlurper().parseText(response)


//check delegates are in one session per timeslot
for (i = 0; i < list.size(); i++) {

    // find all items for this delegate in this timeslot
    def itemsForThisDelegateInThisTimeslot = list.findAll {element -> element.delegateId == list[i].delegateId && element.agendaItemId== list[i].agendaItemId}

    log.info(list[i].delegateId)

    // there should not be more than 1
    if(itemsForThisDelegateInThisTimeslot.size() > 1) {
        log.info(list[i].delegateId + "Delegate already assigned to a workshop at same time");

     //Assert fail in execution
       throw new Error ('Fail')
    }
}

Upvotes: 0

Views: 323

Answers (1)

Chris Adams
Chris Adams

Reputation: 1454

Firstly, there are no assertions in this Script Assertion. Look up Groovy assert.

If you're 'asserting' that the script is Pass or Fail, you need something like...

assert (response.contains('Something from the response'));

or

assert (someBooleanVar == true);

If it passes, the step goes Green. If it fails, it goes Red.

IMHO, it looks you're throwing an exception when the step fails. I would not use an exception in this way. An exception is there to catch and report code issues. Not a test failure.

Regarding exceptions, look up Groovy Try and Catch.

As for this running (or not) when you run a test case. I suspect it is running, but as you're not asserting anything, you can't see the result.

Ever noticed the Script Log tab at the bottom of the screen? All of your log.info statements will be in here when the test step runs. I'd suggest clearing this log (right-click in the Script Log window...), and then running the test case again and have a look in the Script Log for some of your logging messages.

Upvotes: 1

Related Questions