Verifying current date in a field

2 posts, 0 answers
  1. Bryan
    Bryan avatar
    1 posts
    Member since:
    Jul 2010

    Posted 18 Nov 2010 Link to this post

    Is there a built-in way to have the test verify that a field contains the current date?
  2. Stoich
    Stoich avatar
    1 posts

    Posted 22 Nov 2010 Link to this post

    Hi Bryan,
       there's no shortcut around this but it's really easy: Here a little example:
    Consider this simple HTML Code:
    <p> Current time: </p>
    <p id="time"> Monday, 22 November</p

    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:

    public void Assert_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:

    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 the DateTime 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. 

    There's also a way to verify a date without using a coded step. You can use the Element Extraction option to the extract the current date as a variable from a site such as You can then use this variable for your verfication. Here's some info on Element Extraction:
    I only really recommend using this option if you absolutely need to avoid using a coded step. 

    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.

    the Telerik team


    See What’s New in WebUI Test Studio’s Q3 Release 24/7 on Telerik TV
Back to Top