How do you compare selector attributes in Testcafe? - testing

I'm trying to compare the date of videos on a webpage to today's date. If the difference between the two dates is more than X days, report back as false.
The videos on the webpage have a tag in them which uses the format yyyy-mm-dd
I've got a selector set up to find the videos const videoDate = Selector('OPTA-video').withAttribute('data-secondary-time')
Now how do I set a variable to today's date and compare the two? I'm completely stuck!
I was using Katalon Studio before and here's the groovy script that did the same job:
String videoDate = WebUI.getAttribute(findTestObject('OPTA-video'), 'data-secondary_time')
LocalDate todaysDate = LocalDate.now()
LocalDate videoDateParsed = LocalDate.parse(videoDate, dtf)
if (ChronoUnit.DAYS.between(videoDateParsed, todaysDate) > 1) {
KeywordUtil.markFailed('The videos are 2+ days old.')
} else {
KeywordUtil.logInfo('The videos are up to date.')
}

You can use the getAttribute TestCafe method to access an attribute value. Then, parse the attribute value into the JavaScript Date object:
String videoDate = Selector('OPTA-video').getAttribute('data-secondary-time');
Date videoDateParsed = Date.parse(videoDate);
Date todaysDate = Date.now()
...
In the following thread you can find how to compare Date objects.

This is one of the scripts that I am using.
//getting your XPath test value into a string
String ann_time =
WebUI.getText(findTestObject("ObjectRepository/navigateTOElement/announcements_date"))
//converting time to simple date format
SimpleDateFormat sdf = new SimpleDateFormat('HH:mm')
Date sdf_anntime = sdf.parse(new String(ann_time))
//getting Current time
SimpleDateFormat dateFormatGmt = new SimpleDateFormat('HH:mm')
dateFormatGmt.setTimeZone(TimeZone.getTimeZone('GMT'))
SimpleDateFormat dateFormatLocal = new SimpleDateFormat('HH:mm')
currDate = dateFormatLocal.parse(dateFormatGmt.format(new Date()))
// time gap in long format
long duration = currDate.getTime() - sdf_anntime.getTime()
//time gap to mins
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration)
//compare time gap with globale variable
if (diffInMinutes < GlobalVariable.News_updated_time) {
log.logInfo("system is getting updated,last updated "+ diffInMinutes + "min ago")
} else {
CustomKeywords.'errorMessage.logFailed.markStepFailed'('from 1 h, system was not updated')
log.logInfo('from '+ diffInMinutes+ 'h, system was not updated')
}

Related

Kotlin: How to detect 'date change'?

I will use Timer() to execute function by 5 minutes in Kotlin.
And when I execute function by 5m, if a day passed,I want count var to be 0.
So my idea was
declare two vars
var todayDate = LocalDate.now() // 2019-09-23
var todayCount:Int = 0
After that I will check this vars in 5 minutes by using Timer()
Then todayDate value differs from previous todayDate, then I can detect date change.
However, I don't know how to compare current todayDate and previous todayDate.
Any idea? or is there any other way to know day change?
For your specific question about comparing dates you can use the isEqual() method on your LocalDate instance (docs). Something like the following would likely do what you want:
// initial state
var todayDate = LocalDate.now()
var todayCount = 0
// in each timer iteration:
val now = LocalDate.now()
if (!todayDate.isEqual(now)) {
// it's a new day
todayCount = 0
todayDate = now
} else {
// it's the same day
++todayCount
}
However if you're talking about Android and using its Timer class, you need to be aware that that runs on a background thread and you will need to persist your todayDate and todayCount values somewhere (which could be app preferences, your app DB, etc.).

Cucumber-java-Startdate shouldn't be todays date in Java script

I am new to this code.
While running automation script, the date is automatically picked todays date.
actually the date starts from 01/01/2022.
How to change this code?
public void validateStartDateDefaultValueNew() throws Exception{
Date date = Calendar.getInstance().getTime();
DateFormat date formatter = new SimpleDateFormat( pattern: "dd/MM/yyyy");
String today = dateformatter.format(date);
System.out.println("today is "+today);
System.out.println("formatted today is "+date formatter.parse(today))
String [] actual1 = searchStartDateInput.getAttribute("value").split(regor " ");
String actual actuall[0]; =
System.out.println("today actual app is [+actual);
Assert.assertEquals(today, actual);
In your code, you mentioned -
'Date date = Calendar.getInstance().getTime();'.
This line of code takes today's date and current time.
If you want the start date to be '01/01/2022', then hard-code it in your code.

Nest Js Typeorm Query

I am trying to run a query at the repository level of my nestjs application.
The date format of the column (runDateTime) in my DB is isoString. I want to find all the records that have runDateTime of today's date.
I have a function called Parsebackenddate that essentially converts iso string to YYYY-MM-DD. how do i use this function with "schedule.runDateTime" in order to compare both dates in the format YYYY-MM-DD?
Or is there an alternative?
if (getToday) {
const todayDate = dayjs().format('YYYY-MM-DD');
query.where('schedule.runDateTime = :todayDate', {
todayDate,
});
``
Much appreciated.
first, you have to get a date from the database and hold it in a class variable then compare.
something like that may this could help.
var _orderBean = new date_beans();
_orderBean.dueDate = _response[a].dueDate.toISOString().slice(0,10); //YYYY-MM-DD
then compare
var d = new Date(); //todaydate
if(d.toISOString().slice(0,10)>_orderBean.dueDate)
// TodayDate > DueDate

Compare ISO time in JMETER assertion

I am reading the measurements and I have 2 ISO time stamps.
Now using the JSON extractor, I have parsed the time into 2 variables namely "time1" and "time2"
Now I want to compare the time and decide which one is greater.
Sample time that I had parsed to the variables are like below,
time1: 2021-07-01T00:00:03Z
time2: 2021-07-01T00:00:02Z
Now I want to compare and print the value saying time1 is greater than time2 and the returned response is in descending order.
I tried the below snippet in JSR223:
String time1 = vars.get("time1");
String time2 = vars.get("time2");
OffsetDateTime created = OffsetDateTime.parse(time1);
OffsetDateTime updated = OffsetDateTime.parse(time2);
if (updated.isAfter(created)) {
System.out.println("PASSED");
} else {
System.out.println("FAILED");
}
Working example with import and using log.info
import java.time.OffsetDateTime;
String time1 = vars.get("time1");
String time2 = vars.get("time2");
OffsetDateTime created = OffsetDateTime.parse(time1);
OffsetDateTime updated = OffsetDateTime.parse(time2);
if (updated.isAfter(created)) {
log.info("PASSED");
} else {
log.info("FAILED");
}

Kendo DateTimePicker: Need to return dates in UTC

In my MVC application I am storing dates in UTC. In my view I am doing this to show the dates in date time pickers in local time
<div class="datetimepicker">#(Html.Kendo().DateTimePickerFor(m => m.StartDateTime).Value(Model.StartDateTime.ToLocalTime()))</div>
The problem is that when the datetimes arereturned in the post methods back to the server they are returned in local time. Is there a way from the view to return the datetimes in UTC?
Hi datetimepicker in kendo i dono abt it,but just a thought if u can get the value from datetimepicker and pass through a function as below which converts to UTC and then send it to server , i believe ur task is complete..sry if i am wrong...
function convertToUtc(str) {
var date = new Date(str);
var year = date.getUTCFullYear();
var month = date.getUTCMonth()+1;
var dd = dategetUTCDate();
var hh = date.getUTCHours();
var mi = date.getUTCMinutes();
var sec = date.getUTCSeconds();
// 2010-11-12T13:14:15Z
theDate = year + "-" + (month [1] ? month : "0" + month [0]) + "-" +
(dd[1] ? dd : "0" + dd[0]);
theTime = (hh[1] ? hh : "0" + hh[0]) + ":" + (mi[1] ? mi : "0" + mi[0]);
return [ theDate, theTime ].join("T");
}
Your question is a good one and the approach that we suggest is covered in this code library article.