Compare date of content to today's date

We use a scraper to populate our product with videos when new content is available.

We want to run a test, on a daily basis, to check that the scraper is working and adding new content.

One way of doing that is to compare the most recent video’s date with today’s date and report an error if they are more than 5 days apart.

Is this possible with Katalon? If so, what format should the date be in the video’s tags?

Hello,

it is possible. Simple guide:

  1. Get a date from video and store it in a variable
  2. Create another variable with today’s date and format it accordingly
  3. Compare those two variables.

POC:

import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit

import com.kms.katalon.core.util.KeywordUtil

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd")
String videoDate = "2018/12/12"
LocalDate todaysDate = LocalDate.now()
LocalDate videoDateParsed = LocalDate.parse(videoDate, dtf)

if(ChronoUnit.DAYS.between(videoDateParsed, todaysDate) > 5) {
	KeywordUtil.markFailed("The date difference is greater than 5 days.")
} else {
	KeywordUtil.logInfo("The video is up to date.")
}

Get videoDate from UI and update date format according to your settings.

3 Likes

That’s brilliant, thank you very much. I’ll give it a go and see what happens. Cheers for the quick response, really appreciate it

Hi Marek,

I’ve tried using the code you posted. I’m having some difficulty understanding its use though.

I am having trouble pulling the date from the video item. There is a tag in the video that reads

data-secondary_time=“2018-12-13”

This is showing the date that the video was published to the site.

How do I use this in the code above and compare it to today’s date please?

Thank you again for the help

Hello Simon,

what is that “data-secondary_time” property? Is it one of HTML attributes of the video? If so, you can get it like this.
Otherwise, please share a part of your source code of the video element.

String videoDate = WebUI.getAttribute(findTestObject('My/Video'), "data-secondary_time")

And the only other change would be in the pattern of the date - as your date contains - instead of /.

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd")

In the below code, I’m assuming that you already have your date from your tag stored in a String called myDateString:

import java.text.SimpleDateFormat

String myDateString = "2018-12-13";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = format.parse(myDateString);
Date todaysDate = new Date();
int comparison = myDate.compareTo(todaysDate);

The “comparison” integer will tell you how the date you grabbed from the page compares to today’s date as follows:

Returns:

the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.

See more here:

https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#compareTo-java.util.Date-