Forum Discussion

richie's avatar
richie
Community Hero
6 years ago

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

 

  • richie 

    How about below which works any time:

     

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

     

  • nmrao's avatar
    nmrao
    Champion Level 3

    richie 

    How about below which works any time:

     

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

     

    • richie's avatar
      richie
      Community Hero

      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

      • nmrao's avatar
        nmrao
        Champion Level 3
        I an sure the solution is not tried.
  • 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?