Today's Date Validation?

Class library usage, coding and language questions.
Fergal
Certified Professional
Certified Professional
Posts: 298
Joined: Tue Feb 18, 2014 2:14 pm
Location: Co Louth, Ireland
Contact:

Today's Date Validation?

Post by Fergal » Wed Nov 26, 2014 12:17 pm

How can I validate that the date displayed on a web page is the current / today's date?

Thanks!

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Today's Date Validation?

Post by krstcs » Wed Nov 26, 2014 2:49 pm

Code: Select all

//Get the value from the element - assuming the data is stored in the innertext attribute

DateTime onPageDateTime = DateTime.Parse(repo.MyWebPage.MyDateTime.InnerText);

Validate.AreEqual(onPageDataTime, DateTime.Now);
Shortcuts usually aren't...

JSH_QA
Posts: 56
Joined: Thu Apr 05, 2012 9:03 am

Re: Today's Date Validation?

Post by JSH_QA » Wed Nov 26, 2014 4:52 pm

Just be careful not to run your tests around midnight (e.g. from an automated overnight build system) :)

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Today's Date Validation?

Post by krstcs » Wed Nov 26, 2014 6:00 pm

The problem isn't really when you run it, but the fact that the datetime from the page may be retrieved a bit ahead of the "actual". I was just putting up a simple bit of logic to demonstrate the Datetime structure.

If you really want to do this right, you need to have a way to account for the time between the retrieval of the displayed and actual times. I would suggest having a "flex" or delta time, like a number of seconds of difference that is acceptable. Of course, this assumes that the time displayed on a web page is not static. If it IS static, then you need to make the flex time longer.

Code: Select all

public bool IsTimeWithinDelta(System.DateTime timeToTest, int deltaInMilliseconds) {
    return System.DateTime.Now.AddMilliseconds(-deltaInMilliseconds) <= timeToTest && System.DateTime.Now >= timeToTest;
}
Shortcuts usually aren't...