Hi guys.
In my app, I use due-date values for some records. I got a button that should be hidden if due date it’s from past date.
Looking at my object, I got Attribute as text for the date displayed on grid and when I spy the object I got this:
How can I validate that Text value is less than Today?
I try to do this. but I don’t find the way to get the attribute and put it in my code:
Here is the code that I’m trying to set, but I don’t know how I need to set the attribute from my object in object repository .
Any Idea?
I’d do this:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-yy")
LocalDate today = LocalDate.now()
LocalDate dateFromAUT = LocalDate.parse(WebUI.getText(myTestObject), dtf)
if(today.compareTo(dateFromAUT) > 0) {
println "Date from AUT is in the past"
} else if(today.compareTo(dateFromAUT) < 0) {
println "Date from AUT is in the future"
} else {
println "Date from AUT is today"
}
Thank you @Marek_Melocik . Will you create a class for your code or you will add it as a next step to do in the Test Case?
I asked because in my head I want to follow some steps, like:
1.- Verify that Button exist.
2.- If exist, Press button
3.- Else Verify Date in column and compare it with today date.
4.- If date it’s on past, Introduce Db connection and execute query in database to update date to be displayed.
5.- Refresh
6.- Verify That button exist again
7.- Back to step 2.
Regards
Generally, if you use any part of your code at least twice, it is worth to extract it into a separate method. You can create even general method for comparing date from AUT and your date.
As I am a huge fan of putting everything into methods, I’d create such a class with many util methods which you can use anywhere in your project.
public class DateTimeUtils {
static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-yy")
public static boolean isFirstDateGreaterThanSecond(LocalDate d1, LocalDate d2) {
if(d1.compareTo(d2) > 0) {
return true
} else {
return false
}
}
public static boolean isDateFromAUTGreater(LocalDate d1, TestObject to) {
LocalDate dateFromAUT = LocalDate.parse(WebUI.getText(to), dtf)
return isFirstDateGreaterThanSecond(dateFromAUT, d1)
}
public static boolean isDateFromAUTGreaterThanToday(TestObject to) {
return isDateFromAUTGreater(LocalDate.now(), to)
}
}