Monday 12 August 2013

How to use class (user defined) in QTP

Here  is an example that shows how we can create a class in QTP.
Once we define the class, we can create its objects and then access its method and properties.


'declare the class viz. Book.

Class Book

 dim bn,bp
                  ' class has 2 variable members bn and bp.

 public Property Get bookname()
                               ' Get bookname property gets the book name of the object of Book
bookname = bn
 End Property
 public Property Let  bookname(x)
bn = x
                              ' Let  bookname property assigns value to the book name of the object of Book
 End Property
 public Property Get price()
price = bp
 End Property
 public Property Let  price(x)
bp = x
 End Property

function  discountedPrice()
print bp-20
                       'We can have functions and procedures inside class to process memeber variables
End function

End Class

Set b1 = new  Book
                'createing the object b2 of the class Book.

b1.bookname = "QTP Tutorials"
b1.price = 220
                'assigning value to the object b1

print b1.bookname()
               'getting the value of the property bookname.

               'accessing the function in class
b1.discountedPrice()

Set b1 = nothing


This is how we can create and use the classes in QTP.

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

Tuesday 6 August 2013

What are the Utility objects in QTP?

QTP provides some of the utility objects as mentioned below. Main objective of the utility objects is that Utility objects help us in performing lot of tasks in QTP easily.

List of the frequently used utility objects with their importance is given below.

  1. Crypt Object                       --> Encrypts/Decrypts the string
  2. DataTable Object                --> All datatable operations 
  3. Description Object               --> We can do dynamic descriptive programming
  4. DotNetFactory Object         --> Allows access to Static Dot Net Classes
  5. Environment Object             -->  We can create and access environment values
  6. Extern Object                      --> Access dll
  7. MercuryTimer Object           --> Timer related operations
  8. RandomNumber Object        --> Create random number
  9. Recovery Object                   --> Recovery Scenario handling
  10. Reporter Object                    --> Used to report the test results
  11. RepositoriesCollection Object--> Dynamic Repository
  12. Repository Object                  --> Access test object repository at runtime.
  13. TextUtil Object                      --> TextUtil is used to process text
  14. XMLUtil Object                    --> xmlUtil is used to process xml data.





Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

Friday 2 August 2013

When do we use Cdate and Cstr in QTP?

Cdate and Cstr are data type conversion functions in QTP.

For Example -

CDATE

DOB = "09-jan-1986"

myDate = CDate(DOB)

myDate will have date data type.


Cstr

a = 10
mystring = cstr (a)
mystring will have data type as String....


Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

Explain cint, cdbl and clng in QTP.

In QTP scripting, many times you need to compare 2 values.
When you compare the values, it is necessary that both variables should have same data type.

Below functions are used to convert the data type of variable.

  1. cint  - converts the expression into integer sub data type 
  2. cdbl -  converts the expression into double sub data type 
  3. clng -  converts the expression into long sub data type 


Example -

str = "10"
print typename(str)

str = cint(str)
'Convert the string into integer data type
print typename(str)
----------------------------------------------------------------------
----------------------------------------------------------------------
str = "10.2"
print typename(str)

str = cdbl(str)
'Convert the string into double data type
print typename(str)

----------------------------------------------------------------------
----------------------------------------------------------------------
str = "108787"
print typename(str)

str = clng(str)
'Convert the string into long data type
print typename(str)

More examples on data type conversions are at date and string conversions 

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

Explain DateDiff function in QTP.

DateDiff function is used to find out the difference between 2 dates.

For example -
Suppose you know the date of birth of someone and want to find out the age of that person, you can use datediff function in this case.

DateDiff (Interval_Type, Date1, Date2)

Here Interval Type can be of below types.
yyyy -  Year
m  - Month
d  - Day
h  - Hour
n  - Minute
s  - Second

Example -

dateofbirth ="09-01-1986"
print datediff("yyyy", dateofbirth ,now)

'This will print your age in years

Please find below more questions and answers on date and time in QTP.

  1. How to extract date part from timestamp in QTP?
  2. How to find the name of month of given date in QTP?
  3. How to extract time part from given date in QTP?
  4. How to extract the second part from given time stamp in QTP?
  5. Explain dateadd function in qtp with Example.
  6. How to find future date in qtp?
  7. How to get the system date in qtp?
  8. How to verify the date format in qtp?
  9. How to find the yesterday's date in qtp
  10. How to find the date difference in qtp
  11. How to get the current system date in qtp?
  12. How to get the current year in qtp?
  13. How to find the name of week day for given date in QTP?
  14. Explain now function in qtp.
  15. Explain the datediff function in qtp.


Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

Explain DateAdd function in QTP with example.

Dateadd function is used to find the future date or old date in QTP.

For example -
Let say you are testing a trading application where you need to calculate the settlement date for a given trade with trade date as say today.

Now the formula for finding the settlement date is -

settlement date = trade date + 3 business days

In such scenarios you will have to add 3 business days to given trade date.

You can make use of dateadd function in QTP.

print dateadd("d",3,now) 

Above statement will add 3 days to current date and return the date.
You can also find any previous date using above syntax.

print dateadd("d",-1,now) 
This will calculate yesterday's date

print dateadd("d",1,now) 
This will calculate tomorrow's date

Syntax of DateAdd function in QTP.

Dateadd(interval_Type, Interval_Units, date)

Interval Type can be of below types.

yyyy -  Year
m  - Month
d  - Day
h  - Hour
n  - Minute
s  - Second

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

How to extract the date part from the timestamp in QTP?


Hello friends - Lot of times you have to find only the date part of given datetime.

For example - If your application displays "09-jan-1987 08:48 PM" timestamp, you need to verify only date portion.

Vbscript provides function called datevalue which will return the date part of given date time stamp.

print datevalue("09-jan-1987 08:48 PM")
'It will print 09-01-1987 depending upon your system settings

To get the current system date you can use below syntax :-

print datevalue(now)

Please find below more questions and answers on date and time in QTP.

  1. How to extract date part from timestamp in QTP?
  2. How to find the name of month of given date in QTP?
  3. How to extract time part from given date in QTP?
  4. How to extract the second part from given time stamp in QTP?
  5. Explain dateadd function in qtp with Example.
  6. How to find future date in qtp?
  7. How to get the system date in qtp?
  8. How to verify the date format in qtp?
  9. How to find the yesterday's date in qtp
  10. How to find the date difference in qtp
  11. How to get the current system date in qtp?
  12. How to get the current year in qtp?
  13. How to find the name of week day for given date in QTP?
  14. Explain now function in qtp.
  15. Explain the datediff function in qtp.


Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

Give the example of instr function in QTP.

Instr function is used to check if String contains the given substring.

For Example -

If I want to check if IND is available in INDIA, I can do it using below code.

If instr(1,"INDIA","IND") > 0 then
   print "INDIA contains IND "
Else
  print "INDIA does not contain IND "
End IF

Syntax of Instr -

InStr(startOfSearch,String1,String2,ComparisonMethod)

startOfSearch -> From what position you have to start the search.
String1 -> String to be scanned
String2 -> String to find in String1
ComparisonMethod -> defaul 0 - means binary comparison - case sensitive

More interview questions and answers on strings in QTP

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

Explain lcase and ucase functions in QTP

lcase and ucase functions are used for converting the cases of the string.

Example -

str = "Sachin and Sehwag play cricket"

print lcase(str)
'This will print the string in lower case
'sachin and sehwag play cricket

print ucase(str)
'This will print the string in upper case
'SACHIN AND SEHWAG PLAY CRICKET

More interview questions and answers on strings in QTP

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

What is Len function in QTP?

Len function is one of the string functions used in QTP for string manipulation.
Len will return the length of the string (total number of characters).

Example -

print len("Katrina Kaif")

'It will print 12

Please note that len will count the spaces also.

More interview questions and answers on strings in QTP

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

What are the trim functions in QTP?

Trims functions are used to remove blank spaces from the string.

There are 3 trim functions in QTP as mentioned below.

  1. ltrim     - removes leading (at the beginning) spaces from the string
  2. rtrim     - removes trailing (at the end )spaces from the string
  3. trim       - removes leading and trailing spaces from the string


Example -

print ltrim("  hello Tester   ")
'it will print "hello Tester   "

print rtrim("  hello Tester   ")
'it will print "  hello Tester"

print trim("  hello Tester   ")
'it will print "hello Tester"
'Please note that it does not remove the spaces from the middle of the string.

More interview questions and answers on strings in QTP
Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

What are the different String functions in QTP?

Here is the list of all string functions in QTP.

String functions to extract the part of the string -

  • left  - gets the specified number of characters from the left side of string
  • mid -gets the specified number of characters from the given position of the string
  • right - gets the specified number of characters from the right side of string

String functions to remove the spaces from the string -

  • ltrim - removes the blank spaces from the left side of the string
  • rtrim - removes the blank spaces from the right side of the string
  • trim  - removes the blank spaces from the left and right side of the string
Other String functions - 

String      - Returns a string of the given length with specified character.
Space     - Returns the empty string with given length
strReverse - Reverses the given string
ucase      - Converst the string to upper case
lcase       - Converts the string to lower case
strComp - Compares 2 strings
replace   -  replaces the given string str1 from input string say str with other string say str2
len - gets the number of characters in given string
split - splits the string into array using given delimiter
join - forms the string from given array elements
cstr - converts the data type of the variable into  String
chr - gets the character corresponding to the given ascii value
Asc - gets the ascii value of the given character.
instr - searches for a substring in a given string and returns the position where the match is found.
InStrRev- searches for a substring in a given string and returns the position where the match is found from the end of the string.

More interview questions and answers on strings in QTP
Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

What are the different math functions in QTP?

Here is the list of different maths (mathematical) functions provided in QTP.

Abs Function - Gets absolute value of the number

Fix Function  - returns integer part of the number.
Int Function  - returns the integer part of the number.
round Function - rounds the number

Rnd Function  - gets random number
Sqr Function  - find the square root of the number
Sgn Function  - finds the sign of the number.

Geometrical functions-
Sin Function
Cos Function
Atn Function
Tan Function

Logarithmic functions - 
Log Function
Exp Function

Examples on these functions are given on VBScript blog.

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

How to extract minute and hour part of given timestamp in QTP?

In QTP we have one function called Minute that can be used to find out the Minute part from given timestamp.

Example -

print minute("09:33:44")

In above example script will print 33.

To get the current minutes from the system, You can use below code.

print minute(now)



In QTP we have one function called Hour that can be used to find out the Hour part from given timestamp.

Example -

print Hour("09:33:44")

In above example script will print 9.

To get the current Hour from the system, You can use below code.

print Hour(now)


Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

How to find the name of the month of given date in QTP?

You can find the name of the month like January, February etc from a given date using below code.

There are 2 functions which help you find the name of month.

1. month(anyvalid_date)  -  returns the numeric representation of month .
2. monthname(intX)         - returns human readable month name like January, February etc

print monthname(month(now))

Above statement will print the current month name of the system.

print monthname(month("09-jan-1986"))
'Will print january

To get the month name in abbreviated form use below statement
print monthname(month("09-jan-1986"), true)
'Will print Jan.
Thus when you pass the second parameter as true, you will get the month name in 3 letter format like Jan, Feb etc.

Above statement will print the name of the month for the date 09-jan-1986.

Numeric representation of month is given below


1 - January(default)
2  - February
....and so on......



This is how you can find the name of the month for given date in QTP and vbscript.


Please find below more questions and answers on date and time in QTP.

  1. How to extract date part from timestamp in QTP?
  2. How to find the name of month of given date in QTP?
  3. How to extract time part from given date in QTP?
  4. How to extract the second part from given time stamp in QTP?
  5. Explain dateadd function in qtp with Example.
  6. How to find future date in qtp?
  7. How to get the system date in qtp?
  8. How to verify the date format in qtp?
  9. How to find the yesterday's date in qtp
  10. How to find the date difference in qtp
  11. How to get the current system date in qtp?
  12. How to get the current year in qtp?
  13. How to find the name of week day for given date in QTP?
  14. Explain now function in qtp.
  15. Explain the datediff function in qtp.



Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

Explain msgbox function in QTP with example.


When you want to display some information to user, you can use msgbox function. This is useful when you are debugging your QTP script.

Msgbox function has below syntax.

MsgBox(Message_To_Display,buttons_to_display,titletodisplay , helpfile, context)


msgbox now
'Above line will display current system time with ok button.


str = msgbox ("hi",4)
'Above line will display hi with yes and no button.

If str = 6 Then
print "yes:"
else
         print "no"
End If
'If user presses yes button, it will print yes else it will print no.

Second Parameter -> buttons_to_display - determines which buttons to display.
third parameter -> titletodisplay  -> is the title of window.

Last 2 parameters are used for showing help to the user.






Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

Explain Now function in QTP.

Now function is a date and time related function in QTP.

You can use this function to get the current system time stamp.


print now

This will print something like 02-08-2013 12:39:05 depending upon your system's date and time.

You can use now function as a parameter in other date time functions  to extract current year, month, day, hour, second, minute etc.

For Example -

To extract year , Use below code.
print year(now)


Please find below more questions and answers on date and time in QTP.

  1. How to extract date part from timestamp in QTP?
  2. How to find the name of month of given date in QTP?
  3. How to extract time part from given date in QTP?
  4. How to extract the second part from given time stamp in QTP?
  5. Explain dateadd function in qtp with Example.
  6. How to find future date in qtp?
  7. How to get the system date in qtp?
  8. How to verify the date format in qtp?
  9. How to find the yesterday's date in qtp
  10. How to find the date difference in qtp
  11. How to get the current system date in qtp?
  12. How to get the current year in qtp?
  13. How to find the name of week day for given date in QTP?
  14. Explain now function in qtp.
  15. Explain the datediff function in qtp.


Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

Thursday 1 August 2013

Explain replace function in QTP with Example.

Replace function is used to replace the part of the given string with other sub string in QTP.

Example -


myString = "Sachin plays cricket"


print replace(myString,"Sachin","Dhoni")
'Will print Dhoni plays cricket

print replace(myString,"sachin","Dhoni")
'Will print Sachin plays cricket. Because we have used sachin in small case letter

print replace(myString,"sachin","Dhoni",1,-1,0)
'Will print Sachin plays cricket .....Same as above

print replace(myString,"sachin","Dhoni",1,-1,1)

'Will print Dhoni plays cricket....
'The last parameter of replace determines whether the replacement is case-sensitive or case-insensitive.
'If last parameter is 0, replacement is case-sensitive
'If last parameter is 1, replacement is case-insensitive

Syntax -

 replace(myString,"replacethis","replacewith",startat,count,comparisonmehtod)

Parameter1 - myString - > String to be scanned and modified
Parameter2 - replacethis- > String to be searched and replaced for
Parameter3 - replacewith- > String to be replaced with
Parameter4 - startat- > from what position the myString should be searched...Default is 1.
Parameter5 - count- > How many replacements should be done. Default is -1 - All occurrences
Parameter6 - comparisonmehtod- > Is comparison case-sensitive?..Default is 0 - binary comparison.

More interview questions and answers on strings in QTP
Please give your inputs, suggestions, feedback to Us about above QTP article. We value your thoughts.

Explain right, mid, left functions in QTP with Examples.

QTP supports lot of string functions.
Some of the commonly used string manipulation functions are given below.

  • right
  • mid
  • left
All of the above functions are frequently used when performing any string operations in QTP.
All of the above functions extract the part of the string / Sub string.

Right function returns the fixed number of characters from right side of the string.
Left function returns the fixed number of characters from left side of the string.
Mid function can be used to get the characters/ sub string from the left, right or middle part of the string.


Examples -

myString = "Sachin Plays Cricket"

print right(myString,7)    
'will return the 7 characters from the right side of myString      
'Cricket
print left(myString,6)
'will return the 6 characters from the left side of myString
'Sachin
print mid(myString,8,5)
'will return the 5 characters from the 8th position of myString
'Plays

Syntax -
Second parameter in left and right function tells how many characters to return from the string.
In mid functions there are 2 parameters. First parameter tells from which position of the string we have to get the characters and second parameter tells how many characters to return.

More interview questions and answers on strings in QTP

Please give your inputs, suggestions, feedback to Us. We value your thoughts.

How to extract the second part from given timestamp / date in QTP?

In QTP we have one function called second that can be used to find out the second part from given timestamp.

Example -

print second("09:33:44")

In above example script will print 44.

To get the current seconds from the system, You can use below code.

print second(now)


Please find below more questions and answers on date and time in QTP.

  1. How to extract date part from timestamp in QTP?
  2. How to find the name of month of given date in QTP?
  3. How to extract time part from given date in QTP?
  4. How to extract the second part from given time stamp in QTP?
  5. Explain dateadd function in qtp with Example.
  6. How to find future date in qtp?
  7. How to get the system date in qtp?
  8. How to verify the date format in qtp?
  9. How to find the yesterday's date in qtp
  10. How to find the date difference in qtp
  11. How to get the current system date in qtp?
  12. How to get the current year in qtp?
  13. How to find the name of week day for given date in QTP?
  14. Explain now function in qtp.
  15. Explain the datediff function in qtp.


Please give your inputs, suggestions, feedback to Us. We value your thoughts.

Explain split function in QTP with example.

We can use split function to break the string using a delimiter.

For example -

Consider a string "pune*chennai*delhi*mumbai"

Now if you want to break the string you can do it using split function.

cities = "pune*chennai*delhi*mumbai"
arrayOfCities = split(cities,"*")

'Split function returns the sub strings in the form of array - (here arrayOfCities ).

For i=0 to ubound(arrayOfCities)

Print arrayOfCities(i)

next



The syntax of Split function is given below -

Split(Stringtosplit ,delimiter, occurence_Count, comparison_method)

In this function last 3 parameters are optional.

Default delimiter is space.
occurence_Count ->how many substrings should be returned in the array.

comparison_method -> This parameter will tell what kind of comparison should be used. i.e binary(case-sensitive) or textual (case-insensitive).

More interview questions and answers on strings in QTP

  1. What are the different string functions in QTP?
  2. How to extract the digits from given string in QTP?
  3. How to sort the array of strings in qtp
  4. How to get the first character in string in QTP?
  5. How to convert the string into upper case in QTP?
  6. How to get the last character from the string in QTP?
  7. How to replace the string in qtp?
  8. How to check if substring exists in QTP?
  9. Explain right, mid and left functions in qtp?
  10. How to remove the spaces from string in qtp?
  11. What are the trim functions in qtp?
  12. Explain replace function in qtp with example.
  13. Explain the split function in qtp with example.
  14. Explain lcase and ucase functions in qtp.
  15. What is the len function in qtp?
  16. Give the example of instr function in qtp
More interview questions and answers on arrays in QTP are given below.
  1. What is the difference between dictionary and array in QTP?
  2. How to create dynamic 2 dimensional array in QTP?
  3. How to sort array of strings in qtp?
  4. How to create array of dictionary in qtp?
  5. How to define or declare array in qtp?
  6. How to return array from function in qtp?
  7. How to find the length of array in qtp?
  8. How to find the size of array in qtp?
  9. Explain the ubound function in qtp.
  10. Explain the split function in qtp with example.
  11. What is dotnetfactory object in qtp?


Please give your inputs, suggestions, feedback to Us. We value your thoughts.

How to sort the array of string in QTP ?

Sometimes when you are testing a application, you need to check that the values are in sorted order.

Here is a sample program that sorts the array of string using strComp function in QTP.

Example -

a = array("bangalore","surat","Pune",1)

For i=0 to ubound(a)

For j=0 to ubound(a)
If strComp(a(i),a(j),1) < 0  Then
temp = a(i)
a(i) = a(j)
a(j) = temp
End If
Next

Next

For i=0 to  ubound(a)

   print a(i)
  'It will print 1,bangalore, Pune, surat

Next


Please note that above example will sort the array in ascending order. To sort the array in descending order, you have to use below Code.

a = array("bangalore","surat","Pune",1)

For i=0 to ubound(a)

For j=0 to ubound(a)
If strComp(a(i),a(j),1) > 0  Then
temp = a(i)
a(i) = a(j)
a(j) = temp
End If
Next

Next

For i=0 to  ubound(a)

   print a(i)
  'It will print  Pune, surat,bangalore,1

Next

More interview questions and answers on strings in QTP
More interview questions and answers on arrays in QTP are given below.
  1. What is the difference between dictionary and array in QTP?
  2. How to create dynamic 2 dimensional array in QTP?
  3. How to sort array of strings in qtp?
  4. How to create array of dictionary in qtp?
  5. How to define or declare array in qtp?
  6. How to return array from function in qtp?
  7. How to find the length of array in qtp?
  8. How to find the size of array in qtp?
  9. Explain the ubound function in qtp.
  10. Explain the split function in qtp with example.
  11. What is dotnetfactory object in qtp?

Please give your inputs, suggestions, feedback to Us. We value your thoughts.

Explain the Timer Function with Example in QTP.

Timer function is used to get the number of seconds that have passed since midnight.

You can use this to calculate execution time of the QTP script.

Example -

ExecutionStarted = timer

'your script code goes here

ExecutionEnded = timer

ExecutionTime =  ExecutionEnded  - ExecutionStarted

Print ExecutionTime

'This will print the execution time in seconds.

To get the time in minutes use below code.

Print ExecutionTime / 60


Please give your inputs, suggestions, feedback to Us. We value your thoughts.

How to extract Time part from given timestamp (date+time) in QTP


Hello friends - Lot of times you have to find only the time part of given datetime.

For example - If your application displays "09-jan-1987 08:48 PM" timestamp, you need to verify only time portion.

So You need to get only time from the timestamp.

Vbscript provides function called timevalue which will return the time part of given date.

Please note that it will return the time in 24 hour format.


print timevalue("09-jan-1987 08:48 PM")
'It will print 20:48:00

To get the current system time you can use below syntax :-

print timevalue(now)


Please find below more questions and answers on date and time in QTP.

  1. How to extract date part from timestamp in QTP?
  2. How to find the name of month of given date in QTP?
  3. How to extract time part from given date in QTP?
  4. How to extract the second part from given time stamp in QTP?
  5. Explain dateadd function in qtp with Example.
  6. How to find future date in qtp?
  7. How to get the system date in qtp?
  8. How to verify the date format in qtp?
  9. How to find the yesterday's date in qtp
  10. How to find the date difference in qtp
  11. How to get the current system date in qtp?
  12. How to get the current year in qtp?
  13. How to find the name of week day for given date in QTP?
  14. Explain now function in qtp.
  15. Explain the datediff function in qtp.



Please give your inputs, suggestions, feedback to Us. We value your thoughts.

Best QTP Books

Everything About QTP

Hello Friends,
You can find QTP study material, Multiple choice questions (mcq), QTP question bank, QTP question papers, QTP notes, QTP questionnaire, scenario based QTP interview questions, QTP tutorial and QTP training on this site.

If you are a fresher or experienced QTP professional with (1/2/3/4) years of experience, this blog is just for you.