How to get today's date and verify/match?

Hi guys,
Kind of struggling with this…but it’s probably simple as heck. So I’m basically trying to verify a data I created has the correct date generated (today’s date). Obviously. I can’t put verify element or element text, because the date changes daily.

Is there a way to get the date from that element and verify/match it to “today’s date”?
Date format is: 02/18/2020


yes you can … you can use parameters in Object definitions (just check documentation)
script for actual date :

def now = new Date().format( 'MM/dd/yyyy' )
println "now = ${now}"

and to use it like:


1 Like

I added the following to the xpath -

I believe the script you shared did work… but not sure if it’s getting the date correctly. I’ll check back tomorrow and see if the date passes.


i have feeling that xpath you are using will return more than 1 object
and use INDEX and DATE as parameters to check date in n-th row
and use just xpath property
also since @class can change based on user actions (often) i always use contains to search in them

After you have the date variable created (as above), you may need to use go through the table row by row to compare the date (if your XPath does not work properly), like:

def driver = DriverFactory.getWebDriver();
// delay until table forms
WebUI.waitForElementVisible(findTestObject('td_firstRowElement'), 10)
'To locate table'
WebElement Table = driver.findElement(By.xpath("//table/tbody"));
'To locate rows of table it will capture all the rows available in the table'
List<WebElement> rows_table = Table.findElements(By.tagName('tr'));
'To calculate no of rows In table'
int rows_count = rows_table.size();

'Loop will execute for all the rows of the table'
for (int irow = 0; irow < rows_count; irow++)
	'To locate columns(cells) of that specific row'
	List<WebElement> Columns_row = rows_table.get(irow).findElements(By.tagName('td'));
	'retrieve date from column 2 (zero based)'
	dateText = Columns_row.get(1).getText();

         WebUI.verifyMatch(dateText, now, false)

That was helpful - but I guess I’m still struggling a bit. I do want to keep the test simple. All I’m looking to do is get the date text from that object and pass it to match it against today’s date. This is what I have so far - but I am unsure of what/how to set the “actual” vs. the expected. As you can see, I have the “Actual” as NULL. I do have a getText from the object but don’t know how to apply it to the “Actual”

I need help with creating the variable for the actual date/text.

def now = new Date().format(‘MM/dd/yyyy’)
println(“now = $now”)

WebUI.waitForElementVisible(findTestObject(‘Page_Asset-Map Financials/td_dateCreated’), 5)
WebUI.getText(findTestObject(‘Page_Asset-Map Financials/td_dateCreated’))
WebUI.verifyMatch(null, now, true, FailureHandling.CONTINUE_ON_FAILURE)


BTW - this isn’t working right - I’m sure I’m doing something wrong:
List Columns_row = rows_table.get(irow).findElements(By.tagName(‘td’));
dateText = Columns_row.get(1).getText();

@ashraf.madina I assume you are using the Script tab to display the text and so are familiar with that.
When I am working with dates, I use the following import statement at the top:

import groovy.time.TimeCategory /* to use Date library */

This gives me all the functionality for using format(), previous(), minus(n) and plus(n).

Not using XPath: The previous post I left counted the rows in the table, and then moved down the table row by row within a for() loop. Each “td” column within the row is stored within a List component that I named, Columns_row. Within each Columns_row, you want the Date Collected field. If the Date Collected field is the second column, then that would be Columns_row.get(1).getText() as the process is zero based ( 0 is column one, 1 is column two, etc). You haven’t shown any HTML, so I am making more assumptions. You can read up on how to move through tables on other KS questions. If you need to compare more than just the date field, then you can GETTEXT() of that field, like:
‘retrieve money from column 1 (zero based)’
moneyText = Columns_row.get(0).getText();

However, if you can get the XPath working, then you don’t need to move through the table.

If you can get the XPath to work, my thinking would be the Actual text would be from the Web page; the Expected text would be the value within the variable, “now”, as you have above. I save the web page text as “todaysDate”, like below:
def now = new Date().format(‘MM/dd/yyyy’);

def todaysDate = WebUI.getText(findTestObject(‘Page_Asset-Map Financials/td_dateCreated’));

WebUI.verifyMatch( todaysDate, now, false, FailureHandling.CONTINUE_ON_FAILURE)

I use this test of dates regularly and save my formatted date as a Global Variable.

1 Like

YES!!! This is exactly what I was looking for. Top of that, you gave me additional knowledge to do more scripting and tests.
Thanks a bunch!! You’re great! :clap:

Hi I tried using the

def now = new Date().format(‘MM/dd/yyyy’);

def todaysDate = WebUI.getText(findTestObject(‘Page_Asset-Map Financials/td_dateCreated’));

WebUI.verifyMatch( todaysDate, now, false, FailureHandling.CONTINUE_ON_FAILURE)

but on the format part, the site that I am testing uses a different format which is like this:

** Wed, 12 Aug 2020**

Using the code provided above, do you think it is possible for me to use that given the different format used?

@mark.jabay You can create your own format for the date. Check out this site for the format elements:

so, you might use:

def now = new Date().format(‘EEE, dd MMM yyyy’)

WebUI.setText(findTestObject(‘yourTO’), now)

Saying that, you might be able to put in any format and the behind javascript may change the format to the “proper” one. That may even be a test you can try.

Hi I tried the same method like this:

def upload_date = WebUI.getText(findTestObject(‘SupplierPortal/Page_My Account/td_upload Date’), FailureHandling.STOP_ON_FAILURE)

def now = new Date().format(‘dd/MM/yyyy’)

WebUI.verifyMatch(upload_date, now, false, failureHandling.CONTINUE_ON_FAILURE)

But I am getting below error:
Test Cases/SupplierPortal/My Documents/Upload file FAILED.
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1601907519607.groovy: 63: Invalid variable name. Must start with a letter but was: ‘dd
. At [63:30] @ line 63, column 30.
def now = new Date().format((‘dd / MM) / yyyy’)

Am I missing any import statements or anything else?

@deepthi1 In your code above, your quotes look like smart quotes–they have a curve or curl to them. That may be a copy and paste artifact of this forum, however, if the quotes are smart quotes, retype them all within KS so they become straight quotes.

Also, in the following statement, the quotes (and parenthesis) are mismatched.
def now = new Date().format(('dd / MM) / yyyy')

change the above to something like:
def now = new Date().format('dd / MM / yyyy')

Lastly, the reference failureHandling in the below method:
WebUI.verifyMatch(upload_date, now, false, failureHandling.CONTINUE_ON_FAILURE)

should be with a capital F same as in your first reference above (the programming language is case-sensitive):
WebUI.verifyMatch(upload_date, now, false, FailureHandling.CONTINUE_ON_FAILURE)

Thanks much for the response