Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

How to create and work with Class using VBScript Code in QTP

Using Class in VBScript, we are able to create an instance of object and use properties and methods of the object. 


Points to remember with VBScript Class concept:

  • We can define variables, methods, properties, and event members in the class.
  • All the members of a class can be declared as Private or Public.
  • Members declared as Private are visible within the Class block. 
  • Members declared as Public are accessible from within and outside the class.
  • Members are by default Public. If we don't provide member as Private or Public


Syntax of Class Statement in VBScript


Class ClassName

       Statement of Code

End Class


Understanding Implementation of class in VBScript with an example


'' Here we have defined a class with name as VBSriptClass

Class VBScriptClass

''Once we have created a class, next we need to define the members of the VBScript.

''We have taken all the types of members in this example

'' A variable can be defined as Private or Public 


Private var_ClassName 
Public var_Name

''Class Events - Class_Initialize and Class_Terminate are events which we can define in a class

''We can define the action which we need to perform when we instantiate a class in the class_initialize sub as shown below.

Private sub Class_Initialize(  )
msgbox "We have initialised the test. This pop up will appear the moment new instance of class object is created"
End Sub

''When Object is Set to Nothing/destroyed, Class_Terminate event is executed and actions are performed as defined in the sub. 

Private Sub Class_Terminate(  )
msgbox "We have terminated the test. This pop up will appear the moment instance of class object is destroyed/set as nothing"
End Sub

'' Property Let and Get allows to set and extract values from properties in the class

''get method allows to extract value set for property in the class

Public Property Get ClassName
       ClassName = var_ClassName
End Property 

''Let method allows to set value for the Property

Public Property Let ClassName (strClassName) 
         var_ClassName = strClassName 
End Property 

''We can define function in the class. the below example will sum two numbers

''Only Public functions can be used in code outside of the class 

public function sum(a,b)
        sum = a+b
        addition a,b
End Function

''Private method cannot be called outside the class but can be called by another function within the class

Private function addition(a,b)
       msgbox a*b
End function

''A sub/function which is not defined as either Private/Public can be assesed outside the class and works as public method

Sub DisplayUserName 
      msgbox UserName 
End Sub 
End Class 


Using Class members outside the class code


''We can define an instance or object of class as set in below code

Set objClass = New VBScriptClass

''Once the instance of the clas is created, we can used the public members of the class as shown below.

'' Set value of the property of the method.

objClass.ClassName  = "Are we talking about Dance Classes?"
msgbox objClass.sum(12,15)
msgbox objClass.ClassName

'' Close the instance of the class object

Set objClass = nothing


How to use err object to continue test execution in VBSript and QTP


''Err object holds the information for last runtime error that occurred in the test execution


'' In the piece of code, we can add on error resume next to continue with execution . It will not abort the execution but will store the information of the error.


'' Once a new runtime error occur, in that case , the err object stores the information of new error. So handle must be taken to add reporting in test and clearing the error


''Methods for err object are err.raise and err.clear


'' On error Resume next will continue with execution. try to comment the On error resume next , and then see what happens in case on error resume next statement is not provided.


On Error Resume Next 

'' Err.raise <number> will generate err object for err number provided. At runtime as an error is encountered, err object populates the information of error. 

Err.Raise 9err_number= Err.number 

'' Using err.description, we can extract information about the error

err_description= Err.description 

'' In case no error is encountered in the execution , the err. number has number as 0. Therefore we can use If else loop based on value of err.num and report to the QTP Report.

If numerr <> 0 Then 

 ''Reporter.Reportevent ........

msgbox (numerr & " Some error in application")

Else

''Reporter.reportevent ........

End If



'' Once we have validated a piece of code, we should clear the err using err.clear

err.clear

msgbox err.number



''you will see msgbox will display value as 0 now. 



How to create array from string separated by delimiter using vbscript split function


Suppose we have a string with value as : stringA = "nitin>joshi>qa>qtp>automation"

'' We can create an array using split function


Strarray = Split(StringA, ">")
'' we can loop through the array element using:

 intPropSet = Ubound(Strarray)  ''this gives the length or upper bound index of array.

  for i = 0 to intPropSet

      msgbox strarray(i)

  Next


''This can be useful to loop in validating multiple values ''e.g : Validating existence of multiple links with name as nitin or joshi or qtp or automation


''We can combine the values in an array using join statement


If (IsArray(Strarray)) then

   stringB = Join(Strarray, ">")

   msgbox "array with text: " + stringB

Else

   msgbox "not an array"

End If


So while understanding How to create array from string separated by delimiter using vbscript split function, we have now understanding of Join, split, IsArray,Ubound and lbound also