Verify Tme displayed in website

4 posts, 1 answers
  1. Siva
    Siva avatar
    28 posts
    Member since:
    Apr 2012

    Posted 01 Aug 2013 Link to this post

    Hi Team

     I want to verify the time displayed in the website, it  supposed to have the localtime displayed.

    Could you give me a c# code for verifying time

    Thanks

    Siva
  2. Velin Koychev
    Admin
    Velin Koychev avatar
    88 posts

    Posted 02 Aug 2013 Link to this post

    Hello Siva,

    You might find useful to read these articles about 
    the .NET DateTime object and its ToString function.
    DateTime now = DateTime.Now;
       
    DateTime tomorrowDate = now.AddDays(1);
    DateTime in2hours = now.AddHours(2);
       
    string time = now.ToString("hh:mm:ss");
    string ampm = now.ToString("tt");
       
    Log.WriteLine("Current date and time: " + now.ToString());
    Log.WriteLine("Tomorrow is: " + tomorrowDate.ToString("MM/dd/yyyy"));
    Log.WriteLine("In 2 hours the time will be: " + in2hours.ToString("hh:mm:ss tt"));
    Log.WriteLine("Current time is: " + time);
    Log.WriteLine("Is it AM or PM: " + ampm);
    The above code produces this output:

    '8/2/2013 2:51:17 PM' - LOG: Current date and time: 8/2/2013 2:51:17 PM
    '8/2/2013 2:51:17 PM' - LOG: Tomorrow is: 08/03/2013
    '8/2/2013 2:51:17 PM' - LOG: In 2 hours the time will be: 04:51:17 PM
    '8/2/2013 2:51:17 PM' - LOG: Current time is: 02:51:17
    '8/2/2013 2:51:17 PM' - LOG: Is it AM or PM: PM

    When you make sure that you have the current date and time formatted in the right way, you can set it to an extraction variable:
    DateTime now = DateTime.Now;
    string dateTime= now.ToString("d MMM yyyy");
    string time = now.ToString("HH:mm");
    Log.WriteLine(dateTime);
    Log.WriteLine(time);
    SetExtractedValue("currentDate", dateTime);
    SetExtractedValue("currentTime", time);

    On your website you can use a Quick Verification for the number and use the Verify - text contains option. Select the test step and click the (Bindings) drop-down in the Properties pane. Set ExpectedString to the currentDate  or currentTime variable.

    I have recorded a short video to show you how you can do this. 

    I hope this helps.
     
    Regards,
    Velin Koychev
    Telerik
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
  3. Siva
    Siva avatar
    28 posts
    Member since:
    Apr 2012

    Posted 02 Aug 2013 Link to this post

    Hi Velkin

     Thanks for you response, i have another issue when my variable calculated the time as "11:00:01 am"
    next step will verify text  contains ahead 1 second "11:00:02 am" so i'm getting failure.
    appreciate your feed back


  4. Answer
    Velin Koychev
    Admin
    Velin Koychev avatar
    88 posts

    Posted 05 Aug 2013 Link to this post

    Hello Siva,

    If you need to verify using the time from the previous or the next second, you can use the following code:
    DateTime now = DateTime.Now;
    Log.WriteLine(now.ToString());
    DateTime nextSecond = now.AddSeconds(1);
    Log.WriteLine(nextSecond.ToString());
    DateTime lastSecond = now.AddSeconds(-1);
    Log.WriteLine(lastSecond.ToString());

    Looking forward to hearing from you.
     

    Regards,
    Velin Koychev
    Telerik
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
Back to Top
  Cast Your Vote at DevPro Connections! Cast Your Vote at  Win IT Pro Connections!