Vbs
Vbs
Sec.
1 2 3 4 5 6 7 8 9 10 11 12 VBScript in QTP VBScript Data Type VBScript Variable VBScript Arrays VBScript Constants VBScript Functions & Subroutines Arguments in Procedures VBScript Conditional Statements VBScript Looping Statements VBScript Classes VBScript - Property Let, Property Get, Property Set Example of VBScript - Property Let, Property Get, Property Set
Contents
Page
2 2 3 4 4 7 10 11 13 17 20 23
http://www.softwaretestinggenius.com
Page 1 of 23
1) VBScript in QTP
Scripting language for QuickTest Professional (QTP) is VBScript. VBScript (short form of Visual Basic Scripting Edition) is a lively scripting language interpreted via Microsoft's Windows Script Host. VBScript has many powerful functions and provides excellent support for variables, data types, and error handling. Two script engines can interpret VBScript- VBScript.dll, which is invoked by asp.dll is used in web environment and Wscript.exe & Cscript.exe in Windows GUI environment using Windows Script Host (WSH). We typically, use VBScript within WSH to automate systems administration tasks. WSH is the system module that transforms a VBScript file into a Windows executable file. Wscript.exe is used to display output and receive input in Windows GUI format such as dialog and input boxes. Cscript.exe is used in a command-line environment. When VBScript source code is contained in standalone files, they have the file extension .Vbs.
http://www.softwaretestinggenius.com
Page 2 of 23
http://www.softwaretestinggenius.com
Page 3 of 23
4) VBScript Arrays
Array Variable: A variable containing a single value is a scalar variable. we can create a variable that can contain a series of values using an index number. This is called an array variable. Arrays are useful when we are storing sets of similar data. We can store any kind of data in an array. The array can hold a combination of data types. Arrays are of two types: 1) Fixed Length Arrays 2) Dynamic Arrays Fixed arrays have a specific number of elements in them, whereas dynamic arrays can vary in the number of elements depending on how many are stored in the array. Here we have explained all the ways to initialize and use arrays in VBScript. Every element of an array is associated with a unique index number. By default, index number starts from 0. The number of elements in an array is a fixed number. It can also be re-adjusted dynamically. http://www.softwaretestinggenius.com Page 4 of 23
Function
IsArray LBound
Preserve
ReDim UBound
http://www.softwaretestinggenius.com
Page 6 of 23
<html> <body> <script type=text/vbscript> Function do_square(number) Do_square = number + number end function 'first way of calling a function document.write(do_square(5)) document.write(" ") 'second way of calling a function if 90 < do_square(8) then document.Write(90 is less than 8*8") else document.Write("8*8 is less than 90) end if </script> </body> </html>
http://www.softwaretestinggenius.com
Page 8 of 23
This shows that only the address is being passed that is why it is showing the updated value third time in Back in mysub. Passing variable by value example: <html> <head> <script type=text/vbscript> sub mysub ( ) dim x x=2 document.write("in mysub" & x ) call newsub(x) http://www.softwaretestinggenius.com Page 9 of 23
7) Arguments in Procedures
There are two types of arguments in procedures 1) By Val: Indicates that the argument is passed by value. 2) By Ref: Indicates that the argument is passed by reference. By default all arguments are 'ByRef'. For example: Function demo_add(a,b) demo_add=a+b End Function Here a,b are the arguments. By default these are 'ByRef'. In simple terms ByRef Means the value which is assigned to the variable with in the function is permanent and we can use that value out side that function as well. ByVal means the value, which is assigned to the variable with in the function, is temporary and we can use that value only with in that function. For example: Function demo_parameters(byref x,byval y) x=20 y=50 demo_parameters=x+y End Function a=10 b=20 msgbox demo_parameters(a,b) msgbox a msgbox b In the above function x and y are the arguments, declared as byref and byval. With in that function we have assigned values to x and y. Outside of the function we have assigned values to two variables and passing those variables in to the
http://www.softwaretestinggenius.com
Page 10 of 23
http://www.softwaretestinggenius.com
Page 11 of 23
http://www.softwaretestinggenius.com
Page 13 of 23
http://www.softwaretestinggenius.com
Page 15 of 23
Class Machine Private obj_oOS Public Property Set op_sys(oObj) Set obj_o0S = oObj End Property Public Property Get op_sys( ) Set op_sys = obj_o0S End Property End Class We can make a property Read-Only by following two methods: Method1: By writing only a Property Get procedure for the property: In the absence of a Property Let procedure, code outside of the class cannot write to the employeeName property. Class employee Private ename Public Property Get employeeName() employeeName = ename End Property End Class Method2: By declaring the Property Get procedure as Public and the Property Let procedure as Private: Class employee Private ename Private Property Let employeeName(strName) ename = strName End Property Public Property Get employeeName() employeeName = ename End Property End Class Class Methods: Where functions or procedures are written inside the class they are called methods. If a class method is declared as Public there it will be available to code outside or inside the class, and a method that is declared as Private will be available only to code inside the class.
http://www.softwaretestinggenius.com
Page 18 of 23
Dim my_object Set my_object = New welcome With my_object .Name = Genius" .Showwelcome "Informal" .Showwelcome Formal" .Showwelcome Casual" End With Set my_object = Nothing
Class Events: Class_Initialize and Class_Terminate are associated with every class that we create. Class_Initialize is used whenever an object based on a class is. instantiated. e.g. Set objectname = New classname Class_Initialize event's general format is: Private Sub Class_Initialize( ) 'Initialization code goes here End Sub The Class Terminate event is used when the object goes out of scope, or when the object is set to othing. Class Terminate event's general format is: Private Sub Class_Terminate( ) 'Termination code goes here End Sub
http://www.softwaretestinggenius.com
Page 19 of 23
Explanation of VBScript for use in HP QuickTest Professional (QTP) 11) VBScript- Property Let, Property Get, Property Set
Class properties in VBScript are used to assign Values to private variable and handle the process of data validation. Property Let: Which is used by the outside code to store a value in the private property variable. It is similar to a procedure in the sense that it does not return a value. A Property Let procedure Must accept at least one argument. If the private variable you are using is an object then the process of assignment and data validation is handled by Property bet. Property Set: Similar to Property Let but used for object based properties. By default, the Property Set Procedure is Public. To retrieve the value of a private variable we will retrieve the value of a Property. Property Get: This is used by code outside of our class to read the value of a private property variable. It is similar to a function in the sense that it returns a value to the calling code -- this value is the private variable value. The Property comet procedure does not accept any arguments. We can add an argument to it, but then we have to add an additional argument to the property's corresponding Property Let or Property Set procedure, because Property Let/Set procedure must always have exactly one more argument than its corresponding Property Get procedure. If the property get procedure returns an object then we can use the set statement (but it works well without set also) to return the value. Class ABC 'Private object Private var_obj Public Property Get username() Set username = var_obi End Property End Class Read only Properties have only Property Get procedure Write-only properties have only a Property Let or a Property Set procedure Read-Write properties have a Property Get procedure and either a Property Let or a Property Set procedure Example - 1 of Property Let, Property Get, Property Set Following Example, shows a simple class that defines a private variable, m_var, and a two read-write properties, one_type and two_type, the latter of which is an object property. Class Computer Private m_var Private o_var Public Property Let one_type(stringtype) m_var = stringtype End Property
http://www.softwaretestinggenius.com
Page 21 of 23
http://www.softwaretestinggenius.com
Page 23 of 23