How to input date subtraction up to 30 days?

I want to search data for the past 30 days.
This is my script, but i think not efficient.

currentMonth = new Date()

Today = currentMonth.format('dd/MM/yyyy')

TodayMin1 = currentMonth.plus(-1).format('dd/MM/yyyy')

TodayMin2 = currentMonth.plus(-2).format('dd/MM/yyyy')

TodayMin3 = currentMonth.plus(-3).format('dd/MM/yyyy')

TodayMin4 = currentMonth.plus(-4).format('dd/MM/yyyy')

TodayMin5 = currentMonth.plus(-5).format('dd/MM/yyyy')

WebUI.setText(findTestObject('Input_Calendar'), 
    Today)

WebUI.click(findTestObject('Button_Search'))

if (WebUI.verifyElementNotPresent(findTestObject('TestObject'), 5, FailureHandling.OPTIONAL))
{
	WebUI.setText(findTestObject('Input_Calendar'),
		TodayMin1)
	WebUI.click(findTestObject('Button_Search'))
	
	if (WebUI.verifyElementNotPresent(findTestObject('TestObject'), 5, FailureHandling.OPTIONAL))
	
		{
			WebUI.setText(findTestObject('Input_Calendar'),
				TodayMin2)
			WebUI.click(findTestObject('Button_Search'))
		}
		
		if (WebUI.verifyElementNotPresent(findTestObject('TestObject'), 5, FailureHandling.OPTIONAL))
			
				{
					WebUI.setText(findTestObject('Input_Calendar'),
						TodayMin3)
					WebUI.click(findTestObject('Button_Search'))
				}
				
				if (WebUI.verifyElementNotPresent(findTestObject('TestObject'), 5, FailureHandling.OPTIONAL))
					
						{
							WebUI.setText(findTestObject('Input_Calendar'),
								TodayMin4)
							WebUI.click(findTestObject('Button_Search'))
						}
						
						if (WebUI.verifyElementNotPresent(findTestObject('TestObject'), 5, FailureHandling.OPTIONAL))
							
								{
									WebUI.setText(findTestObject('Input_Calendar'),
										TodayMin5)
									WebUI.click(findTestObject('Button_Search'))
								}

}

Any suggestion please. Thank you in advance.

No. This code will not compile as java.util.Date does not implement plus(int) method.


You should not use the java.util.Date class. It is old. It is poorly designed.

You should use the Java8 Date/Time API instead. You should go through some tutorial.

You want to subtract days? See

While you are reading about the new date libraries, also do some reading about Lists. You can create one String List (Dates that have been formatted to String) so you do not have to have 30 variables.

Maybe like:
import java.time.LocalDate
import java.time.format.DateTimeFormatter

LocalDate localDate = LocalDate.now()
List<String> monthDates = new ArrayList<String>();

DateTimeFormatter fstandard = DateTimeFormatter.ofPattern("dd/MM/yyyy")

for (int cnt = 0; cnt <= 30; cnt++) {
	monthDates.add(localDate.minusDays(cnt).format(fstandard))
}
println("today is ${monthDates[0]}")
WebUI.comment("today  is ${monthDates[0]}")

println("20 day ago is ${monthDates[20]}")
WebUI.comment("20 day ago is ${monthDates[20]}")

or using the “old” library, could be:

Date todaysDate = new Date();
List<String> months = new ArrayList<String>();

for (int cnt = 0; cnt <= 30; cnt++) {
	months.add(todaysDate.plus(-1 * cnt).format("MM/dd/yyyy"))
}

println("today is ${months[0]}")
WebUI.comment("today  is ${months[0]}")

println("20 day ago is ${months[20]}")
WebUI.comment("20 day ago is ${months[20]}")
1 Like

You’ve got nesting hell going on here…

May I suggest the guard clause technique…

I think you need to format your nest of if statements properly. You have them looking like you are 5 levels down, when code-wise, you are only 2. Perhaps the jungle that you have is also confusing you to what you want.

Maybe like:

I have formatted your code based upon the if statement braces. Is this what you wanted? Personally, I don’t think it is, but you can check it out for us.

if (WebUI.verifyElementNotPresent(findTestObject('TestObject'), 5, FailureHandling.OPTIONAL))
{
	WebUI.setText(findTestObject('Input_Calendar'), TodayMin1)
	WebUI.click(findTestObject('Button_Search'))
	
	if (WebUI.verifyElementNotPresent(findTestObject('TestObject'), 5, FailureHandling.OPTIONAL))
	{
		WebUI.setText(findTestObject('Input_Calendar'), TodayMin2)
		WebUI.click(findTestObject('Button_Search'))
	}
	if (WebUI.verifyElementNotPresent(findTestObject('TestObject'), 5, FailureHandling.OPTIONAL))
	{
		WebUI.setText(findTestObject('Input_Calendar'), TodayMin3)
		WebUI.click(findTestObject('Button_Search'))
	}
	if (WebUI.verifyElementNotPresent(findTestObject('TestObject'), 5, FailureHandling.OPTIONAL))
	{
		WebUI.setText(findTestObject('Input_Calendar'), TodayMin4)
		WebUI.click(findTestObject('Button_Search'))
	}
	if (WebUI.verifyElementNotPresent(findTestObject('TestObject'), 5, FailureHandling.OPTIONAL))
	{
		WebUI.setText(findTestObject('Input_Calendar'), TodayMin5)
		WebUI.click(findTestObject('Button_Search'))
	}
}

Thank you.
I can used this code

for (int i = 0; i <= 30; i++) {
	currentDate = new Date().plus(-i).format('dd/MM/yyyy');
	
	WebUI.setText(findTestObject('Input_Calendar'), currentDate);
	WebUI.click(findTestObject('Button_Search'));

	if (WebUI.verifyElementPresent(findTestObject('TestObject'), 5, FailureHandling.OPTIONAL)) {
		break; 
	}
}

Ah, I was wrong.

The java.util.Date in the native Java language does not implement plus(n) method, but , now I have found, Groovy provides the extended version of java.util.Date which implements the plus(n) method:

https://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Date.html#plus(int)

So his code would work in Katalon Studio.

Also, why the fuck are we writing different string to the same input field 30 times?!

calm bro, actually this problem was solved, and solution already marked. Do you have any advice?

Ya, YAGNI and KISS. Two similar principles.

YAGNI

Avoid doing anything extra. Do only what is essential requirement for the system. Avoid spending time on extra features, for example. If You Ain’t Gonna Need It, don’t do it!

KISS

Keep It Short and Simple. Write clean code, avoid complexity, …

In the case of your code in question…

What are you really trying to do here? Are you trying to test each previous valid date string, to see if the date calendar widget pops up with that date selected?

It’s not clear from your code whether or not that is the intention. If it is, you should put assertion in your loop, or capture the return value of that WebUI.verifyElementPresent() and log it to spreadsheet (or something similar). Else, delete the loop (it is YAGNI), and simply backdate by 30 days (KISS).