checkIf for jsonPath exists?

1,040 views
Skip to first unread message

Feng Yu

unread,
Mar 5, 2017, 2:30:48 AM3/5/17
to Gatling User Group
Hi.
  Here is my code:
.check(
  status.is(200),
  jsonPath("$.coupons[?(@.picked==false)].id").findAll.saveAs("coupous_id_list")
)

But in some cases, jsonPath("$.coupons[?(@.picked==false)].id") will return null. So gatling will raise error: jsonPath($.coupons[?(@.picked==false)].id).findAll.exists, found nothing.

How do I omit this error if jsonPath("$.coupons[?(@.picked==false)].id") not exist?

I find a checkIf DSL, but I can't add jsonPath("$.coupons[?(@.picked==false)].id") to checkIf(jsonPath("$.coupons[?(@.picked==false)].id").exists)(jsonPath("$.coupons[?(@.picked==false)].id").findAll.saveAs("coupous_id_list"))

Any ideas?
Thanks.

Feng Yu

unread,
Mar 5, 2017, 9:46:13 AM3/5/17
to Gatling User Group
And maybe saveAs could support a default value? Just like java System.getProperty("property", defaultValue).

Maybe jsonPath("jsonPath").findAll.saveAs("var", [])?

在 2017年3月5日星期日 UTC+8下午3:30:48,Feng Yu写道:

adam.a...@pearson.com

unread,
Mar 6, 2017, 8:32:37 AM3/6/17
to Gatling User Group
Check out transformOption as described here: http://gatling.io/docs/current/http/http_check/#transforming

transformOption(function) takes a Option[X] => Validation[Option[X2]] function, meaning that it gives full control over the extracted result, even providing a default value.

And example usage:

transformOption(extract => extract.orElse(Some("default")).success)

Hope that helps :)

Feng Yu

unread,
Mar 6, 2017, 8:49:16 AM3/6/17
to Gatling User Group
Thanks. But I'm unfamiliar with Scala. Would you please show me a simple example? Thanks. :)

在 2017年3月6日星期一 UTC+8下午9:32:37,adam.a...@pearson.com写道:

Artajew, Adam

unread,
Mar 6, 2017, 9:01:16 AM3/6/17
to gat...@googlegroups.com
No problem. Here is an example from live scenario (user_level is set to 1 if attribute "number" is not found in JSON):
.check(jsonPath("$.hierarchy..[?(@.type=='Level')].attributes.number").transformOption(x => x.orElse(Some("1"))).saveAs("user_level"))
Good luck!
Adam

--
You received this message because you are subscribed to a topic in the Google Groups "Gatling User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gatling/Ln8XeVmogrs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gatling+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Adam Artajew
Performance Engineer
 
Pearson IOKI Sp. z o. o.
Dąbrowskiego 77A
60-529 Poznań, Poland

Follow us on FB, in, G+

Learn more at ioki.com.pl
 
 
Pearson

Feng Yu

unread,
Mar 7, 2017, 10:41:28 PM3/7/17
to Gatling User Group
Great! Thanks very much!

For me this solution works very well:

sonPath("$.coupons[?(@.picked==false)].id").findAll.transformOption(x => x.orElse(Some(Array[String]()))).saveAs("coupous_id_list")
...
doIf(// check coupous_id_list exists and not empty ) {
    // steps
}

在 2017年3月6日星期一 UTC+8下午10:01:16,Artajew, Adam写道:
Good luck!
Adam

To unsubscribe from this group and all its topics, send an email to gatling+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages