Sunday, January 11, 2009

How to use Classes in QTP

Here i am giving an useful information about how to use classes in QTP.

Before going to that we will discuss about using functions in QTP.

If there is a function in our script like this

'*********** ********* ********* ********* ********* ********* **
Function Demo(a,b)
demo=a+b
End Function
'*********** ********* ********* ********* ********* ********* **

This is a function to add two numbers.
To call this function we write
'*********** ********* *****
val=demo(2,3)
msgbox val
'*********** ********* *****
We can store the functions in library files (.vbs) and to use this function, Library should be associated to the QTP Test or library file should be executed by QTP Test using ExecuteFile statement.
This means we can use the functions which are written in QTP Script or in Library files by associating it or by executing it.

But this is not possible when using classes in our QTP Test. If we want to use a class which is there in the libraryfile its mandotory that the library should be executed using Executefile statement.
Classes will not work which are there in associated libraries. Here is an example...

'*********** ********* ********* ********* ********* **
Class Maths
Function Add1(a,b)
add1=a+b
End Function

Function sub1(a,b)
sub1=a-b
End Function

End Class
'*********** ********* ********* ********* ********* **
If the above code is there with in the test script then the usage of this class will be like this
'*********** ********* ********* ********* ********* **
Set mat=New Maths
val=mat.add1( 2,3)
msgbox val
'*********** ********* ********* ********* ********* **
If the class code is there with in the library file then the usage of this class will be like this
'*********** ********* ********* ********* ********* **
ExecuteFile (Library Path)
Set mat=New Maths
val=mat.add1( 2,3)
msgbox val
'*********** ********* ********* ********* ********* **
This class will not work If you just associate the library with in the
File--> Settings --> Resources
But there is a way of using the classes even though if it is in associated library.

First Method:
Where ever the class is declared in the library it self create the class instance and use it in your qtp script.
'*********** ********* ********* ********* ********* **
Set mat=New Maths

Class Maths
Function Add1(a,b)
add1=a+b
End Function

Function sub1(a,b)
sub1=a-b
End Function

End Class
'*********** ********* ********* ********* ********* **
To Use this class
'*********** ********* *
val=mat.add1( 2,3)
msgbox val
'*********** ********* *

Second Method:
Create a function which is returning class object by creating instance of the class.
'*********** ********* ********* ********* ********* **
Function mat()
Set mat=New Maths
End Function

Class Maths
Function Add1(a,b)
add1=a+b
End Function

Function sub1(a,b)
sub1=a-b
End Function

End Class
'*********** ********* ********* ********* ********* **
To Use this class
'*********** ********* *
Set mth=mat ' mat is a function which will return the class instance
val=mth.add1( 2,3)
msgbox val
'*********** ********* *

19 comments:

  1. Great.It is easy and powerful!
    Thanks, now I have basic view about classes in QTP.

    ReplyDelete
  2. Thanks.very useful information.
    Ajay,
    software testing engineer,
    bangalore.

    ReplyDelete
  3. Thanks Man, it was useful.

    ReplyDelete
  4. Thank for sharing, it's greate and useful.

    ReplyDelete
  5. Great and easy-to-understand tutorial.

    I would like to know about the specific/particular usage of these class objects.

    I don't know in which situations this would be very very useful.
    Any advice.

    Regards,
    Suresh

    ReplyDelete
  6. Can you tell us which way is better on performance ? or does classes actually increase performance?

    ReplyDelete
  7. why the list of functions are not visible when we create a class object and tries to access it via object name and put dot(.) list of all the functions should be available... but its not like that here.... :(

    we need to manually type the function name

    ReplyDelete
  8. The code written needs some chnages as the functions under class are not returning any value how r u priting this msgbox

    you should add one line here
    add1 =

    ReplyDelete
  9. Excellent!
    Very easy to understand.

    Though I have been seeing the classes in vbs, till now I could not understand them.

    ReplyDelete
  10. Hi i am very new into testing currently i am working in one organisation as Quality Assurance, i want to learn QTP through online, i got many ebooks but i am not satisfy with that if you have something related QTP Basics then just mail me to kakadesachin.kakade@gmail.com
    Please send links regarding QTP online Training and ebooks also .I am writing this comment to other user also if they have such info.Or if you have any soft copy in your own words will be very helpful for me. Because i am not getting much time to join QTP course in institutes
    Thanx
    Sachin

    ReplyDelete
  11. i am new learner,its easy understandable manner and its help me a lot. please send this tutorials to my mail...................
    my mail id : Sandyaddl@gmail.com

    ReplyDelete
  12. thanks...
    I am a new learner of QTP.this tutorials are help me a lot .
    please send this tutorials my mail
    my mail id Sandyaddl@gmail.com

    ReplyDelete
  13. thanks, nice post. helped to understand class

    ReplyDelete
  14. Thanks for posting...very helpful information

    ReplyDelete