This is a migrated thread and some comments may be shown as answers.

Verifying current date in a field

1 Answer 164 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Bryan
Top achievements
Rank 1
Bryan asked on 18 Nov 2010, 05:58 PM
Is there a built-in way to have the test verify that a field contains the current date?

1 Answer, 1 is accepted

Sort by
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>
<p id="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:

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: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 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 http://www.timeanddate.com/worldclock/. You can then use this variable for your verfication. Here's some info on Element Extraction:http://tv.telerik.com/watch/automated-testing-tools/using-element-extraction-variables-webui-test-studio
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.

 Regards,
Stoich
the Telerik team

 

See What’s New in WebUI Test Studio’s Q3 Release 24/7 on Telerik TV
Tags
General Discussions
Asked by
Bryan
Top achievements
Rank 1
Answers by
Stoich
Telerik team
Share this question
or