Thursday, 16 May 2013

Working with date related functions in QTP with the use of VBScript.


In this post, I will be covering most of the date related functions with examples.

Why we need this date functions?
Assume that you are automating the application in which one of the screen date field accepts the different formats or need to add required days/months before entering the value in the date field.
One more issue with day and month inputs when valid format was MM or DD. If we read the month and day from the system it returns a single digit month or day, but the application does not accept a month 6 (June) instead requires 06. In this kind of situations we require this date functions. All the date related functions are in bold. Copy the bolded items below to QTP test pane and execute it to see the result.

1.     Now : Returns the current system date and time
‘Usage
Sysdate = Now
msgbox Sysdate
Result: QTP shows current system date in a dialog box

2.     Date : Returns the current system date
‘Usage
Mydate = Date
msgbox Mydate
Result: QTP shows current system date in a dialog box

3.     Time : Returns the current system time
‘Usage
Mytime = Time
msgbox Mytime
Result: QTP shows current system time in a dialog box

4.     Year : Returns the current year from the system date
‘Usage
Sysdate = Now
MyYear = Year(Sysdate)
msgbox MyYear
Result: QTP shows current year from the system date in a dialog box

5.     Month : Returns the current month from the system date
‘Usage
Sysdate = Now
MyMonth = Month (Sysdate)
msgbox MyMonth
Result: QTP shows current month from the system date in a dialog box

6.     Day : Returns the current day from the system date
‘Usage
Sysdate = Now
MyDay = Day (Sysdate)
msgbox MyDay
Result: QTP shows current day from the system date in a dialog box

7.     Hour : Return current hour from the system date
‘Usage
Sysdate = Now
MyHour = Hour (Sysdate)
msgbox MyHour
Result: QTP shows current hour from the system date in a dialog box

8.     Minute : Return current minutes from the system date
‘Usage
Sysdate = Now
MyMinute = Minute (Sysdate)
msgbox MyMinute
Result: QTP shows current minutes from the system date in a dialog box

9.     Second : Return current seconds from the system date
‘Usage
Sysdate = Now
MySecond = Second (Sysdate)
msgbox MySecond
Result: QTP shows current seconds from the system date in a dialog box

10.  WeekDay : Returns the weekday from the given date
‘Usage
Sysdate=Now
MyWeekDay=WeekDay(Sysdate)
msgbox MyWeekDay
Result: Returns the weekday from the given date. Note that it returns the integer.
Sunday – 1
Monday – 2
Tuesday – 3
Wednesday – 4
Thursday – 5
Friday – 6
Saturday – 7

11.  WeekDayName : Returns the weekday name for the specified day of the week
‘Usage
Sysdate=Now
MyWeekDay=WeekDay(Sysdate)
WeekDayName(MyWeekDay)
msgbox MyWeekDayName
Result: Returns the weekday name for the specified day of the week.

12.  DateAdd : Returns the date after adding the specified interval
Interval can be YYYY- Year, q-Quarter,m-month ,d-day
‘Usage
Adddays=DateAdd(“d”,30,Now)
msgbox Adddays             ‘ here added 30 days to the current date
Result: Returns the date by adding the specified interval

13.  DateDiff : Returns the interval difference between two dates
Interval can be YYYY- Year, q-Quarter,m-month ,d-day
‘Usage
DiffDays=DateDiff(“m”,now,”31/12/2012”)          ‘ Here getting the month difference between the mentioned dates
Result: Returns the interval difference between the dates

14.  DatePart : Returns the specified part of a given date
Interval can be YYYY- Year, q-Quarter,m-month ,d-day
‘Usage
Mypart = DatePart(“d”,now)
msgbox Mypart                                                ‘ Here getting the day part of the current date
Result : Returns the specified part of a given date

15.  Here I wanted to explain a scenario with the script. For example your application date object allows the format dd-mm-yyyy format only. How to handle this? Below is the solution.
‘Usage
Sysdate = Now
Sysyear = Year(Sysdate)                                ‘Year is in yyyy format so need of any changes
Sysmonth=Month(Sysdate)        ‘ Here month can be one or two digits
Mm=Len(Sysmonth)                      ‘ getting the length of the string
If mm < 2 then
Retmonth=”0”&mm                      ‘ If the month is one digit then we can concatenate 0 to month
Else
Retmonth=mm
End if
Sysday=Day(Sysdate)                    ‘ Here day can be one or two digits
dd=Len(Sysday)                               ‘ getting the length of the string
If dd < 2 then
Retday=”0”&dd                               ‘ If the day is one digit then we can concatenate 0 to day
Else
Retday=dd
End if
‘Now concatenate the strings to required format dd-mm-yyyy
reqFormat = Retday&”-“&Retmonth&”-“&SysYear
msgbox reqFormat

Enjoy working with automation testing :-).

7 comments:

  1. Your information about qtp is really interesting. Also I want to know the latest new techniques which are implemented in qtp. Can you update it in your website?
    Qtp training Chennai

    ReplyDelete

  2. Thanks for sharing this information. Java is one of the popular object oriented programming language used for many of the multinational corporation. So learning Java Training in Chennai is really helpful to make a bright future.

    ReplyDelete
  3. Oracle DBA Training in Chennai
    Thanks for sharing this informative blog. I did Oracle DBA Certification in Greens Technology at Adyar. This is really useful for me to make a bright career..

    ReplyDelete
  4. Nice article i was really impressed by seeing this article, it was very interesting and it is very useful for me. I get a lot of great information from this blog. Thank you for your sharing this informative blog.
    Informatica Training in chennai
    QTP Training in Chennai
    Hadoop Training in Chennai
    Best Big Data & Hadoop Training in Chennai
    Best Selenium Training In Chennai

    ReplyDelete
  5. Oracle Training in chennai | Oracle D2K Training In chennai
    This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..

    ReplyDelete
  6. Best SQL Query Tuning Training Center In Chennai This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..

    ReplyDelete
  7. Your post about technology was very helpful to me. Very clear step-by-step instructions. I appreciate your hard work and thanks for sharing.
    QTP Training in Chennai
    QTP Training

    ReplyDelete

Search This Blog

There was an error in this gadget