Monday, 26 August 2013

How to use Increment/Decrement Operator feature in QTP using VBScript

Hi friends, I have been involved with Software Testing and QTP automation for almost 3 years , and I have noticed that in Vb scripting we don't use increment and decrement operator.

We don't have any language support form VB Script itself for increment/decrement operator.It means we cant use below

msgbox i++

we need to do i=i+1 and sometime when writing long chunk of modules this lack of feature is quite trouble some.
For this we can implement feature by using OOPS architecture.

1. Create a new .qfl file (A New function Library) .you can use the existing qfl too, but i recommend to use a new qfl for all CLASS related code.(We say the file name as modifiedOperator.qfl)

2. Now We will create a new Class which will support the this feature, add the below code in your .QFL file.

Class GenericNumber

 Private numberValue

 //Below function will handle the code for PreIncrement function
 Public function [++]()
 End Function

 //Below function will handle the code for Predecrement function
 Public function [--]()
 End Function

 Public default property get result()
 End Property

 Public property let result(n)
 End Property

 Sub Class_Initialize()
 End Sub

End Class

Now the Above mentioned code will handle the operation for PreIncrement/Predecrement Operations. now we need to implement a constructor like operations so that we can actually use this class

Function [customOperation](x)
   Set [customOperation]=New GenericNumber
   If isnumeric(n) Then
   End If
End Function
The Function [customOperation] actually enable the user to use the Class we used now store both class and Function customOperation in modifiedOperator.qfl file Step 2. Now we are goin to use the code we written into our tests/programes.Please remember that we need to include the qfl file into your test ,Application area to use this feature now instead of using a normal integer variable we use a GenericNumber type variable.(We make an object of class GenericNumber for which we use the function customOperation . ) this functions returns the object of the class GenericNumber
// the variable num will hold the a object of type GenericNumber  with value as 5
Set  num=[As Num](5)

msgbox num.[++]
//We use .[++] instead of ++ as was in c/c++ languages.

//now run this code and the answer will be 6 
msgbox num.[--]
//now run this code and the answer will be 5

I hope this helped.

No comments:

Post a Comment