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]}")
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'))
}
}
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;
}
}
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:
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).