Is there a built-in way to have the test verify that a field contains the current date?
1 Answer, 1 is accepted
0
Stoich
Telerik team
answered on 22 Nov 2010, 05:43 PM
Hi Bryan,
there's no shortcut around this but it's really easy: Here a little example:
Consider this simple HTML Code:
<div>
<p> Current time: </p>
<pid="time"> Monday, 22 November</p>
</div>
You want to verify that <p id="time"> contains the current date. You can do that in a coded step, here's the code for a simple solution:
publicvoidAssert_CodedStep()
{
Element el = Find.ByCustom(e => e.TextContent.Equals("time")); //Find Logic
DateTime current = DateTime.Now; //We get the current time
DateTime fromHtml = DateTime.Parse(el.InnerText);
//We convert the text field into a DateTime Object
Assert.IsTrue(current.CompareTo(fromHtml) == 0); //Fails the step if match isn't true
}
The method DateTime.Parse() lets you convert a String (i.e. text field) to a DateTime object. You can then compare this object against the current date (DateTime.Now). Keep in mind that DateTime.Parse() won't work on any random date representation - it needs to follow one of the specified standards. Check out this page for more info:http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
DateTime.CompareTo(someDate) is a straighforward comparisson method. It will return an int:
-1 if someDate is "bigger" (i.e. more recent)
0 if they're equal
1 if the Date you're invoking the method on is "bigger"
Assert is the "Return" statement for you coded step, it will fail the step if necessary.
DateTime.Now returns the time up to the second. If you compare it, say, Monday, 22 November 2010 C# assumes you mean Monday, 22 November 2010 at midnight and your comparison will only return true at midnight on the same day (up to the second). You'll probably need to play around with theDateTime current object to make it suitable for your needs.
Something to keep in mind: I recommend that you write the Find Logic for the Date Element yourself. For example: the dynamically-generated element <p id="time"> Monday, 22 November 7:59:27</p>.
WebUI Test Studio might generate Find Logic based on the Element's innerText (Monday, 22 November 7:59:27). However, since time changes every second , so does the innerText of the Element.
Consequently, trying to find the element based on it's innerText will invariablly result in a failure. If you do use the auto-generated Find Logic make sure it's correct.
I realize that you probably know this stuff already but I still hope you will find this info helpful. Please don't hesitate to contact us if you have more questions.