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 :-).

2 comments:


  1. 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
  2. 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

Search This Blog