Forum Discussion

AutoBear's avatar
AutoBear
Occasional Contributor
10 months ago

ReadyAPI - Data Source - Minimum value as today´s date - Possible?

Hi everyone! I am trying to generate a list of dates to be used in a test case step in my ReadyAPI project. My idea is to have a list of 14 dates to be generated by the Data Source step, starting from today´s date and incrementing the date value by 1 day for each subsequent date to be generated. 

This is how the setup looks like for now, generating the list of dates from today for the next 14 days.

 

The issue as you might understand if that I cannot dynamically set the start date as today´s date, but manually set it myself when I run the test. Is there a way around it via Data Source --> Data Generator --> Date and Time?  

I have also tried to have a Groovy Script instead of a Data Generator (so Data Source --> Groovy Script), but I am having troubles with the outcome. This is how it looks like right now:

 

import java.text.SimpleDateFormat
import java.util.Calendar

// Get the current date
def currentDate = Calendar.getInstance()
def dateFormat = new SimpleDateFormat("yyyy-MM-dd")

// Calculate and return the next 14 days, one at a time
for (int i = 0; i < 14; i++) {
def date = dateFormat.format(currentDate.time)
log.info("date: $date")

// Return each date individually
result["date"] = date

// Increment the date by 1 day for the next iteration
currentDate.add(Calendar.DAY_OF_MONTH, 1)
}

 

Issue is however that I am getting a list of dates with always the same date, which is the date counted from today´s date:

 

Appreciate if anyone has a solution either for the Data Source --> Data Generator --> Date and Time and/or for the Data Source --> Groovy Script!

3 Replies

  • JoostDG's avatar
    JoostDG
    Frequent Contributor

    Hi AutoBear ,

     

    In your script, it looks like you forgot to use your i variable. 

    In stead of 

    currentDate.add(Calendar.DAY_OF_MONTH, 1)

    try

    currentDate.add(Calendar.DAY_OF_MONTH, i)

  • ChrisAdams's avatar
    ChrisAdams
    Champion Level 3

    Hi,

    A lot of fields in ReadpyAPI have a right-mouse click context menu where you can access a Get Data option.  Get data allows you to then pull in values from anywhere within the current workspace.

     

    Try creating some new API request with has a query parameter and in the form view for your request, right-click in the field and check you can access Get Data.  If so, try right clicking in the min value of your generator.  With it being a dropdown, it's not likely there, but deffo worth giving a go.

     

     

    • AutoBear's avatar
      AutoBear
      Occasional Contributor

      Hi ChrisAdams, thanks for the reply! I already have a the "date" value set as an input value for a parameter of another API request in my project (as "${Data Source#date}"). So I can fetch the data there. The issue is upstream, meaning the data generated by the Data Source is incorrect. Or better said, it is incorrect when I try to generate the dates via the Groovy Script I pasted above; it is technically correct, but not what I want to have generated, when set as Data Generator (as I would like to be able to set the initial date dynamically as today's date).

       

      Your suggestion about trying to right clicking in the min value of the Data Generator does not seem to work unfortunately.

       

      Maybe someone has some more input on the possibility to implement today´s date as a start value when Data Generator is used, or someone has input on what is wrong with the Groovy Script above generating always the same date as result?