cancel
Showing results for 
Search instead for 
Did you mean: 

How to get cookies in Groovy scripts?

Moderator

How to get cookies in Groovy scripts?

Hi Community,

 

Let me share an answer to the above question with you Smiley Happy

 

If the Session option is enabled, ReadyAPI maintains the HTTP session on the TestCase level automatically, which means that it stores cookies received from the server and sends them back in the subsequent requests to the same endpoint. If you need to get programmatic access to the stored cookies, you can use this Groovy script (it's applicable to ReadyAPI v.1.7.0 and later):

 

import org.apache.http.protocol.HttpContext
import com.eviware.soapui.model.iface.SubmitContext
import org.apache.http.impl.client.BasicCookieStore
import org.apache.http.client.protocol.HttpClientContext

HttpContext httpContext = context.getProperty(SubmitContext.HTTP_STATE_PROPERTY)
BasicCookieStore cookieStore = httpContext.getAttribute(HttpClientContext.COOKIE_STORE)
//iterate through the cookies store and output the name-value pairs to the log
cookieStore.getCookies().each{
	log.info(it.name + "=" + it.value)
}

Example:

2018-09-17_10-43-32.png

Natalie
Customer Care Team

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
New Here?
Join us and watch the welcome video:
Top Kudoed Authors