Sunday 27 October 2013

QTP Variables


What is variables?

Declaration of variables?

Option explicit statement

Assigning values into variables

usage of variables

Naming Restictions

Scope of variables

Type of variables

Array variables - Constant Array,dynamic Array, Dimensional array
        aAssiging series of value at a time


a) What is variable ?

-> Named memory location for storing or holding the information

-> The variable values change through out the program.

-> Primory memory -> RAM, Variable stored in primory memory. After execution

get completed, variable removed from primary memory location.

-> Secondary Memory -> Hard disk, CD-ROM, USB Drive


b) Declaration of variable?

using Pubilc,Private, Dim (99% used)

Syntax for DIM

Dim Variable name

Ex:  Dim a,b


c) Implicit and Explicit Declaration of variables

Explicit -> Declaring the variable then used in program is called the

Explicit declaration of the variable. ( This is best practice)

Implicit -> without declaration, directly we used variable in the program is

called implicit declaration. Default value for implicit variables is zero.


Example :

dim a=10
msgbox "Addition of" & a+b

o/p  Addition of 0    

Its not showing the error because VB script accept any variables and assign

default value to zero.if we want to throw error use option Explicit


d) Option Explicit Statement

Its forces to declare the all variable in the script. its show the error if

we are not declare in the script. Consider above example we get the error.

Its should be the first line in the statement

Example:

Option Explicit
dim a=10
msgbox "Addition of" & a+b

o/p  Error,   its show the error if we are not declare in the script.



e) Assigning values into variables

 we can assign the values in two types.

-> Initialization -> direct we assign the value

    Example:  Dim a=23

-> Reading -> its have 4 types

        a) Reading From input device (like keyboard or mouse)
       
        Ex: num2 = inputbox("Enter num2")

        b) Reading from files (Notepad or Excel)
        c) Reading from Databases
        d) Reading from application objects




Example ;

Dim num1,num2,res
num1 = 100       'Initalization
num2 = inputbox("Enter num2")    ' Reading
res num1+num2
msgbox res

O/P

Enter num2 200
300


Not : here num2 is string not a integer, because its value from inputbox.
here no need to convertion from string to integer.

because the concatenation symbol (Add + symbol), if both the values are

string, then we need conertion from string to integer. in above Example the

num2 value only string. So no need to convertion from string to Integer.





f) Usage of Variables

Stoing and holding the memory location

Different type of usage variable

-> Holding the data

a=100

-> Storing the value that returned by a program or code

a= (3+4) ^ 5    'exponantial function (7*7*7*7*7)
msabox a
   

-> Storing the object referance

set a = CreateObject ("Scripting.FileSystemObject")


-> As a parameter

For i=1 to 10 Step 1

    Browser("gmail").Page("gmail").WebEdit("userName").set i


   

   
g)Naming Restictions

-> Should start with aplhabets, not start with numbers

dim abc  'correct
dim 6gh   'incorrect

->Shold not contain Embeded period

dim as s     'incorrect
dim as.g     'incorrect
dim we-g      'incorrect
dim we*g    'incorrect
dim jo_e    'correct

-> must not required maximum 255 characters in length ( 1 tp 255 char)

-> must be unique in a scope of declaration

-> No duplicate variables

-> Vb Script is not a case sensitive language.so its treat as variable same

for both upper case and lower case.

Dim a,b,c   'correct
Dim h,B,r   ' incorrect no duplicate allowed.Shows Errr like "Variable

Redefined"


Standared naming restriction

The best practice if we follo below method, but QTP is not throw any error

message if you follow below methods

-> do not use any reserved words ( like Wait,Exit..)




h) Scope of variables

-> Program/Script level variables
-> Function/Procedure level variables

b=34
msgbox b

Function asb()
dim a=10
msgbox a
End Function

call asb()

msgbox a     ' o/p 0. if you are use option explict its shows error like

"variable is undefined"


i) Types of Variable :

-> Scalar variable or variable - its holding only one value at a time

-> Array Variables -> its storing series of values at a time based on type of array.

-> Associated array or hash variables - vbscriprt we dont have the hash

variable. but Vb script have the equalent feature. we are using dictionary

objects for hash variables.


Array variables

Constant Arrays

Its used when the size is defined

Ex:

Dim a,b(3)    ' b is array , its store 4 values its starts from 0 index.
b(0)=10
b(1)=34.32
b(2)="India"
b(3)= #10/10/2010#  
a= b(0)+b(1)
msgbox a


Note = if we are use b(4) in above examples, its shows error like "

subscript out of range"



Dynamic Arrays

its used when the size is undefined. if we want to use the dynamic array,

first define the size using redefined.

dim a,b(3),c()   'c is dynamic array variable
ReDim c(4)        '' its re define the array size, we should re define

the size before we using dynamic array

c(0) = 23
c(1) = 45 .....

Redim c(6)      ' now its store 7 values, we can redefine the size with in

same script and assign the values. its removed previous value.

c(0)=3
c(1)= 6 like that assign 7 value



Preserve keyword


used for keep the previous value



dim a,b(3),c()
ReDim c(4)

c(0) = 23
c(1) = 45 .....

Redim Preserve c(6)    ' its keep the previous value

c(5)=3
c(6)= 6






How to declare the dimensional array

vb script accepts 60 dimensional array. first number indicate rows and second number indicates column.

Dim a,b(2),c(),f(4,5)    ;f is two dimensional 4 is row and 5 is column. its stores the 30 values (5*6)  not (4*5). its starts from base 0.


isArray () function

its return the boolean value.its check whether the give variable is array variable or not. that means normal variable.


dim a(1)
msgbox IsArray(a)


o/p  True



Assigning series of value at a time

its achieved by two method

-> using array function
-> using split function


using array function


Example:

dim a
msgbox IsArray(a)  'False
a=Array("Array",100,34.23,#12/12/2003#)
msgbox IsArray(a)   'True
msgbox a(1)        'o/p = 100
msgbox a(2)        'o/p = 34.23


UBound() function

its used to find the how many values are hold by dynamic array variables.


dim a
msgbox IsArray(a)  'False
a=Array("Array",100,34.23,#12/12/2003#)
msgbox IsArray(a)   'True
msgbox a(1)        'o/p = 100
msgbox a(2)        'o/p = 34.23
msgbox UBound(a)    'o/p 4



using split function


dim a,b
b="VB SCRIPT LANGUAGE"
a= split(b)    ' its split thhe above sentence into 3 parts. its splitted

by space. space consider as delimeter
msgbox a(0)    ' O/p VB




dim a,b
b="VB@SCRIPT@LANGUAGE"
a= split(b,"@")    ' Here we are mention from which words we need to split
msgbox a(0)    ' O/p VB


dim a,b
b="VB@$SCRIPT@$LANGUAGE"
a= split(b,"@$")    ' Here we are mention from which words we need to

split
msgbox a(0)    ' O/p VB


here its stored as below
a(0) = VB
a(1) = SCRIPT
a(2) = LANGUAGE

No comments:

Post a Comment