How can I validate that the date displayed on a web page is the current / today's date?
Thanks!
Today's Date Validation?
Re: Today's Date Validation?
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...
Re: Today's Date Validation?
Just be careful not to run your tests around midnight (e.g. from an automated overnight build system)
Re: Today's Date Validation?
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.
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...