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

Verify Tme displayed in website

3 Answers 38 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.
Siva
Top achievements
Rank 1
Siva asked on 01 Aug 2013, 08:43 PM
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

3 Answers, 1 is accepted

Sort by
0
Velin Koychev
Telerik team
answered on 02 Aug 2013, 11:57 AM
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
0
Siva
Top achievements
Rank 1
answered on 02 Aug 2013, 03:31 PM
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


0
Accepted
Velin Koychev
Telerik team
answered on 05 Aug 2013, 01:58 PM
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
Tags
General Discussions
Asked by
Siva
Top achievements
Rank 1
Answers by
Velin Koychev
Telerik team
Siva
Top achievements
Rank 1
Share this question
or