How do I select a random future date from the date picker every time in Katalon studio?
2 Answers
The following solution can be used to select a random future date from a date picker in Katalon Studio:
- Get the current date and store it in a variable.
- Calculate the maximum range of future dates (for example, 3 months from the current date) and store it in a variable. 3 .Generate a random number within the range of future dates.
- Add the random number to the current date to get the selected future date.
- Use the "Set Text" keyword to enter the selected future date in the date picker.
Example:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Calendar calendar = Calendar.getInstance();
String currentDate = dateFormat.format(calendar.getTime());
calendar.add(Calendar.MONTH, 3);
String futureDate = dateFormat.format(calendar.getTime());
long diff = dateFormat.parse(futureDate).getTime() - dateFormat.parse(currentDate).getTime();
int randomFutureDays = (int) (Math.random() * diff / (1000 * 60 * 60 * 24));
calendar.setTime(dateFormat.parse(currentDate));
calendar.add(Calendar.DATE, randomFutureDays);
String selectedFutureDate = dateFormat.format(calendar.getTime());
WebUI.setText(findTestObject('Object Repository/Page_Date Picker/input_Date Picker'), selectedFutureDate);
This solution uses the java.util.Calendar
and java.text.SimpleDateFormat
classes to manipulate and format dates, respectively. The Math.random()
method is used to generate a random number within the range of future dates.
You can have date picker use this: Date todaysDate = new Date()
def screenFormattedDate = todaysDate.format('dd/MM/yyyy')
//Used to enter today's date WebUI.setText(findTestObject('Object Repository/AAAAa/Start_date'), screenFormattedDate)