cancel
Showing results for 
Search instead for 
Did you mean: 

Inline Scripting - To increment a unique value by +1 each submission?

SOLVED
Highlighted
Community Hero

Inline Scripting - To increment a unique value by +1 each submission?

Hey!

 

I have some tests that POST a .json to MS's Dynamics CRM.  One of the .json attributes is a unique identifier (submissionid) - it's a 13 digit number treated as a string (so wrapped in quote marks)

 

Before now - when I've needed to make a unique identifier like this change each submission - I've parameterized the value - I've used say the following:

TestCase1_${=(new Date().format('dd-MM-yyyy'))}

So if the test is run once a day - the unique identifier is always unique - or before now I've added the time as well the date - so the test could be executed multiple times a day.

 

Now the submissionid value is a 13 digit numeric string (e.g. "1559999320553"), so I can't use the inline scripting option I used before to make it unique.

 

The .json payload is as below:

{
   "whatevs_submissionid" : "1559999320553",
   "whatevs_exportdate" : "2019-10-12",
   "whatevs_DestinationCountry@odata.bind" : "/whatevs_countries(1eb03e02-e32d-e911-a975-000d3a2cb90c)",
   "whatevs_CommodityGroup@odata.bind" : "/whatevs_commoditygroups(1e115957-7b2f-e911-a969-000d3a29ba60)",
   "whatevs_CommodityTypeid@odata.bind" : "/whatevs_commoditytypes(befa3a40-902f-e911-a96d-000d3a2cbd60)",
   "whatevs_DeliveryMethod@odata.bind" : "/whatevs_deliverymethods(8ef00b91-6228-e911-a969-000d3a28d891)",
   "whatevs_EHCTemplate@odata.bind" : "/whatevs_ehctemplates(1625bacc-ba2f-e911-a969-000d3a29ba60)",
   "whatevs_applicationchannel" : "814250002",
   "whatevs_appliedehcversion" : "1.4",
   "whatevs_exportertrader" : "Trader",
   "whatevs_applicationurl" : "https://whatevs.azurewebsites.net/protected/application/1559999320553/review",
   "whatevs_ehcurl" : "https://whatevs.azurewebsites.net/protected/application/1559999320553/ehc",
   "whatevs_ukcountryofexport" : "814250000",
   "whatevs_Organisation@odata.bind" : "/accounts(a528f577-1bc7-e811-a95c-000d3a28da35)",
   "whatevs_ApplicantId@odata.bind" : "/contacts(603d0039-a019-e911-a96d-000d3a28da35)",
   "whatevs_numberofcertificates" : 1,
   "whatevs_applicantreference" : "ABC6666"
}

Typically I'd parameterize these to something like as follows:

{
   "whatevs_submissionid" : "${#TestCase#whatevs_submissionid}",
   "whatevs_exportdate" : "2019-10-12",
   "whatevs_DestinationCountry@odata.bind" : "/whatevs_countries(1eb03e02-e32d-e911-a975-000d3a2cb90c)",
   "whatevs_CommodityGroup@odata.bind" : "/whatevs_commoditygroups(1e115957-7b2f-e911-a969-000d3a29ba60)",
   "whatevs_CommodityTypeid@odata.bind" : "/whatevs_commoditytypes(befa3a40-902f-e911-a96d-000d3a2cbd60)",
   "whatevs_DeliveryMethod@odata.bind" : "/whatevs_deliverymethods(8ef00b91-6228-e911-a969-000d3a28d891)",
   "whatevs_EHCTemplate@odata.bind" : "/whatevs_ehctemplates(1625bacc-ba2f-e911-a969-000d3a29ba60)",
   "whatevs_applicationchannel" : "814250002",
   "whatevs_appliedehcversion" : "1.4",
   "whatevs_exportertrader" : "Trader",
   "whatevs_applicationurl" : "https://whatevs.azurewebsites.net/protected/application/${#TestCase#whatevs_submissionid}/review",
   "whatevs_ehcurl" : "https://whatevs.azurewebsites.net/protected/application/${#TestCase#whatevs_submissionid}/ehc",
   "whatevs_ukcountryofexport" : "814250000",
   "whatevs_Organisation@odata.bind" : "/accounts(a528f577-1bc7-e811-a95c-000d3a28da35)",
   "whatevs_ApplicantId@odata.bind" : "/contacts(603d0039-a019-e911-a96d-000d3a28da35)",
   "whatevs_numberofcertificates" : 1,
   "whatevs_applicantreference" : "ABC6666"
}

But as I state above - I want to get the value to increment by +1 each time the test is executed.

 

I hope I've been clearer than I usually am! 🙂

 

Can anyone advise?

 

Cheers,

 

richie

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: Inline Scripting - To increment a unique value by +1 each submission?

@richie 

How about below which works any time:

 

"whatevs_submissionid" : "${= System.currentTimeMillis()}"

 



Regards,
Rao.

View solution in original post

7 REPLIES 7
Highlighted
Community Hero

Re: Inline Scripting - To increment a unique value by +1 each submission?

@richie 

How about below which works any time:

 

"whatevs_submissionid" : "${= System.currentTimeMillis()}"

 



Regards,
Rao.

View solution in original post

Highlighted
Community Hero

Re: Inline Scripting - To increment a unique value by +1 each submission?

Hey @nmrao 

 

its not just that I need it to work anytime - the problematic bit is the fact the value has to be a 13 digit numeric text value - see required value format below 

 

"whatevs_submissionid" : "1234567890123"

Its got to be in the above format to get passed the validation.

 

This has actually crossed my mind before when I was looking at inline scripting - but I didn't actually need it then so i didnt do any research.

 

I did have the idea of trying to use the Datasource's object's DataGenerator option - wasn't sure whether it would work - but I had a quick play around with it - but the max number of digits is 10 - I can't go beyond that - so I can't try that option (admittedly I hadn't thought out completely how I was going to use this - but I was hoping inspiration would strike!)

 

If it can't be done via inline scripting and I can't use the DataGenerator Datasource option - - I'm wondering if I could setup a Excel Datasource (say a 10000 row spreadsheet - incrementing a 13 digit value by +1 for each row - this would be a 10second job). 

 

This would generate the values I need - but I'd need the test to pick up a value from the spreadsheet for each of the tests and for each execution, mark up the value as 'used' and then for the next execution pick up the next unused value from the spreadsheet as a submissionid - but thats going to take more groovy than I got - so I was hoping there's an inline option I cant find!

 

Essentially if I hard code this defraexp_submissionid I can't add the ReadyAPI! tests into our pipeline - cos it would require someone changing the values on a daily basis! 😞

 

Struggling......

 

Cheers!

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Hero

Re: Inline Scripting - To increment a unique value by +1 each submission?

I an sure the solution is not tried.


Regards,
Rao.
Highlighted
Community Hero

Re: Inline Scripting - To increment a unique value by +1 each submission?

Hey @nmrao 

 

I'm sorry - I didnt understand your last message fella

 

cheers,

 

richie

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Hero

Re: Inline Scripting - To increment a unique value by +1 each submission?

@richie 

It was mentioned in the question that the value needs to be a length of 13 characters with numbers and solution is provided accordingly.
It always generates unique( and is the main criteria) value of length 13. So you won't need to add +1 (as that is not the main criteria).

You would have come to know that if tried ( that's what I said in last message)

 

Wrote a script(you can run it online) which does the testing of 1000 sample values and value is of 13 digit length

 

https://ideone.com/SHakVe

Since all the values passes the test, scripts ends with Success (without any assertion error).



Regards,
Rao.
Highlighted
Contributor

Re: Inline Scripting - To increment a unique value by +1 each submission?

You can extract the value from the JSON payload, cast it to an Integer, increment it, then re-set it in your JSON payload:

 

import groovy.json.*

String json // Assuming this your JSON payload, wherever you get it from
def map = new JsonSlurper().parseText(json)
Integer submissionId = Integer.parseInt(map.getAt('whatevs_submissionid')) + 1 // maybe wrap with try/catch here? map.putAt('whatevs_submissionid', submissionId.toString()) // Put it back in the map
json = new JsonBuilder(map).toPrettyString() // Map back to JSON

Is this what you're trying to accomplish?

Highlighted
Community Hero

Re: Inline Scripting - To increment a unique value by +1 each submission?

@nmrao 

 

I'm an idiot - sorry - I saw the word 'time' in the code snippet and thought  - "that's not what I need!"

 

Apologies - I've been doing 20hours days since Wednesday and I'm making stupid mistakes

 

Kudos awarded :), with some extra assigned too for your patience! 🙂

 

Cheers,

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors