Abheek Das
Abheek Das

Reputation: 53

How to get assertion value using groovy script

I have one test step which contains two assertion.

Now I have one groovy script, from where I am executing this test step. Using this groovy script I need to print assertion name, Value and Status. Below is the code I have written:

testStepSrc = testCase.getTestStepByName(testName)
Assertioncounter = testStepSrc.getAssertionList().size()
for (AssertionCount in 0..Assertioncounter-1)
{
log.info("Assertion :" + testStepSrc.getAssertionAt(AssertionCount).getName() + " :: " + testStepSrc.getAssertionAt(AssertionCount).getStatus())

error = testStepSrc.getAssertionAt(AssertionCount).getErrors()
if (error != null)
   {
    log.error(error[0].getMessage())
   }
 }

but in output it is displaying like:

Wed Sep 04 17:21:11 IST 2013:INFO:Assertion :Not SOAP Fault :: VALID
Wed Sep 04 17:21:11 IST 2013:INFO:Assertion :Contains :: VALID

As you can see, I am able to print assertion name and status but not the value of 'Contains' assertion. Please help me how to get the value of a particular assertion.

Thanks in advance.

Upvotes: 1

Views: 20229

Answers (2)

Roglesby
Roglesby

Reputation: 59

Abhishek's response did contain you answer I believe but just not in the format you were looking for.

I was looking for the same info for custom reporting and after digging through The SoapUI forms I stumbled upon this.

The piece of code that I believe you are looking for is:

log.info e.getToken()

however this is an example of how to retrieve it only when an error occurs but you can get it in a valid scenario using something similar to:

def iAssertionName = assertionNameList[j]
def iAssertionStatus = testStep.getAssertionAt(j).getStatus().toString()
def tstep = testStep.getName()
def gStatus =  testStep.getAssertionAt(j).status
def expect = testStep.getAssertionAt(j).getToken()
log.info "Expected Content: " + expect

This is a subset of my code but produces the log message:

Fri Sep 20 11:04:09 CDT 2013:INFO:Expected Content: success

My SoapUI script assertion was checking to see if my response contained the string "success".

Thanks Abhishek for your response!

Upvotes: 0

Abhishek Asthana
Abhishek Asthana

Reputation: 1855

So here is some things for you to read

and what i tried

def assertionsList = testRunner.getTestCase().getTestStepByName("Test Step Name").getAssertionList()
for( e in assertionsList){
    log.info e.getToken() //gives the value of the content to search for
    log.info e.DESCRIPTION
    log.info e.ID
    log.info e.LABEL
    log.info e.toString()
}

This gives the following output

Wed Sep 04 15:12:19 ADT 2013:INFO:Abhishek //the contains assertion was checking for the word "Abhishek" in the response of my test step where the assertion was applied.
Wed Sep 04 15:12:19 ADT 2013:INFO:Searches for the existence of a string token in the property value, supports regular expressions. Applicable to any property. 
Wed Sep 04 15:12:19 ADT 2013:INFO:Simple Contains
Wed Sep 04 15:12:19 ADT 2013:INFO:Contains
Wed Sep 04 15:12:19 ADT 2013:INFO:com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.SimpleContainsAssertion@c4115f0

Upvotes: 1

Related Questions