Verify todays date

I’m using Katalon to check the basic functionality of a web page. One of those functions is filtering data by a date (Today, Tomorrow, Last Week, etc.). How exactly would I validate that the filter is working? The website will display the date in a mm/dd format, how do I check that without manually changing the test strings every day?

You can use groovy time category to achieve this. Below is the sample code to help you get started.

import groovy.time.TimeCategory

use(TimeCategory, { 
        today = new Date()
        lastWeek = (today - 1.week)
		NextWeek = (today + 1.week)
		NextMonth = (today + 1.month)
		twodaysAgo = (today- 2.days)		
    })

println ("today -- " + today.format('MM/dd/yyyy'))
println ("lastWeek -- " + lastWeek.format('MM/dd/yyyy'))
println ("NextWeek -- " + NextWeek.format('MM/dd/yyyy'))
println ("NextMonth -- " + NextMonth.format('MM/dd/yyyy'))
println ("twodaysAgo -- " + twodaysAgo.format('MM/dd/yyyy'))
2 Likes

“today” wasn’t accessible outside of the “use” brackets, so I added: today = null, before the “use” statement, and then it worked as expected! Thanks!

Hi Hector, can you please share the test steps you included to verify, am having similar situation but not able to do so. Thanks

@Ashwin said:
Hi Hector, can you please share the test steps you included to verify, am having similar situation but not able to do so. Thanks

You can use the top code from here to in the keyword, it is Java based:

Apologies Ashwin the above is to just get the current date and time.

@Ashwin said:
Hi Hector, can you please share the test steps you included to verify, am having similar situation but not able to do so. Thanks

Hi Ashwin, am having similar situation. my test step is 1. Click search 2. Select filter by start date ( show calendar by current month and i will click curent date ) do you have same problem ? I have this problem and not solved. any one can help me?

@“hardi santosa”, You could try with this sample code:
After click to input date time, the datepicker should be shown. Then you could find the date picker and give a loop to find date value with “td” tag. For example:

(I try from this: https://stackoverflow.com/questions/21398575/select-a-date-from-date-picker-using-selenium-webdriver )


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;///..../////
DateFormat dateFormat = new SimpleDateFormat("dd");
Date date = new Date();
String today = dateFormat.format(date);
//find the calendar
WebElement dateWidget = driver.findElement(By.className('datepicker-days'));
List<WebElement> columns=dateWidget.findElements(By.tagName("td"));
//comparing the text of cell with today's date and clicking it.
for (WebElement cell : columns)
{
   if (cell.getText().equals(today))
   {
      cell.click();
      break;
   }
}
2 Likes

@nhi said:
@hardi santosa, You could try with this sample code:
After click to input date time, the datepicker should be shown. Then you could find the date picker and give a loop to find date value with “td” tag. For example:

(I try from this: testing - Select a date from date picker using Selenium webdriver - Stack Overflow )
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

///…/////
DateFormat dateFormat = new SimpleDateFormat(“dd”);
Date date = new Date();
String today = dateFormat.format(date);
//find the calendar
WebElement dateWidget = driver.findElement(By.className(‘datepicker-days’));
List columns=dateWidget.findElements(By.tagName(“td”));
//comparing the text of cell with today’s date and clicking it.
for (WebElement cell : columns)
{
if (cell.getText().equals(today))
{
cell.click();
break;
}
}
hi nhi thanks,
can you help me to change that code to be custom keyword in katalon?

You can try the following:

public class todayDate

{

@Keyword

def today()

{

DateFormat dateFormat = new SimpleDateFormat(“dd/mm/yyyy”);

Date date = new Date();

String today = dateFormat.format(date);

return today

}

}

Can anyone help me with the same method above… but all I want to do is verify an element == today’s date.

Basically, I’m creating a row which has a createdDate of today - I want to verify everytime i run this test, the (correct date) today’s date gets posted.
image

DateFormat dateFormat = new SimpleDateFormat(“dd/MM/yyyy”);
@ashraf.madina You should use MM in "dd/MM/yyyy” instead of mm
MM returns current Month’s number where mm gives current minute in time…
it’s a late reply but it would be helpful for anyone who need solution.

Thanks
shiva Kadhuluri

1 Like

i have problem with Date (DD/MM/YYYY) , Time (HH/MM/SS) (AM,PM)

it will struck on doing this process, error will occurring ,

please check & clear this issue . .

@prasath1132

Please search this forum with keyword ElementClickInterceptedException. You will find a few previous discussions which look similar to your case. For example,