Forum Discussion

Tambu's avatar
Tambu
New Contributor
2 years ago

Incrementing string value in payload

Hi,

I have a test that POST a .json. One of the .json attributes is 'Response Name' and is a string. I need to make this value unique. so whenever the test case is executed this value should be unique. Can someone please advise how I can achieve?

Many thanks in advance

 

 

1 Reply

  • JoostDG's avatar
    JoostDG
    Frequent Contributor

    Hi Tambu .

    You could provide a random GUID. Use a groovy script test step or setup script with:

    def randomResponseName= UUID.randomUUID().toString()
    log.info randomResponseName

    I personally find it handy to generate a string based on the date of today. That way, the string is more meaningful. Example:

     

    import java.time.LocalDateTime
    import java.time.format.DateTimeFormatter
    
    TimeZone.setDefault(TimeZone.getTimeZone('UTC'))
    LocalDateTime nowLocal = LocalDateTime.now()
    DateTimeFormatter formatCET = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withLocale(Locale.US)
    String randomResponeName = "MyRandomResponseName_" + formatCET.format(nowLocal)
    log.info randomResponeName

    result = "MyRandomResponseName_2022-10-14T11:14:34.424Z", which will be different each time.

     

    Note: You can adjust the default timezone to anything else than "UTC" to match your local time, but then I would remove the TZD value 'Z' from the pattern. (I'd always recommend to use UTC)