Wednesday 26 March 2014

Basic VBScript for Software Automation Testing.

VBScript : -  Software automation tester can used VBScript while automating functional, regression and smoke test cases by using TestComplete/QTP. VBScript is most common scripting languages. Case insensitive  language,  It’s a light weight scripting language like other scripting language like JavaScript, Jscript, Ruby etc. VBScript is a inbuilt language in window platform.
VBScript has many powerful functions and provides excellent support for variables,
data types, and error handling.

Note – Window Scripting Host (WSH) tool runs VBScript programs in Window platform.

VBScript Variables Declaration: - A variable is used to stored values, In VBScript all variables are   variant type that can store any type of values such as int, float, char, string etc.
Example –
     Dim x
     x= 20
     x = 20.6
     x= “TheITQC”

Character and String values must be enclosed by double codes.
Variable declaration is optional in VBScript but to preventing the human error with variable name we can declare variable at the top of the script.
We can be declared explicitly and implicitly variables in VBScript.

Explicitly Variables:- Explicitly Variables are declared with Dim statement, Public Statement and Private Statement.
     Dim variableName
     Public employeeAddress

Implicitly Variables: - we can declare Implicitly Variables within the script by just using the variable name. But this is not a good practice it’s may be a cause of error.

While declaring variables, Test Automation engineer must follow the naming conversion like below-
    1-Variable name must be start with letter.
    2-Variable name should not be accede 255 character.
    3-Variable name should not contain. (dot)
    4-Variable name should not contain Keywords/Reserve word.
    5-Variable name should be unique.

VBScript Arrays: - We can used arrays to store more than one values into a variable, (all values should be same data type) every element of an array is associated with a unique index number. By default index number starts from 0, VBScrtipt supports multiple dimensions arrays.
Syntax-
     Dim ArrayName(ArraySize) 
     Dim TestArray(2)
     TestArray(0) = “abc”
     TestArray(1) = “xyz”

Operators in VBScript : - VBScript supports multiple operators for the evaluation of expression.


Addition
+
Subtraction
-
Multiplication
*
Division
/
Modulus arithmetic
mod
Equality/Assignment
=
Inequality
<> 
Less than
< 
Greater than
Less than or equal to
<=
Greater than or equal to
>=
Logical negation
Not
Logical conjunction
And
Logical disjunction
Or


VBScript Build in Function:-

VBScript has multiple inbuilt functions, We can use those function while preparing automation script in TestComplete. 

1-CreateObject :- We can use this function for create an object for specified class.
    Syntax -
         Set objectName = CreateObject(“Class”)
         Example: – Create an object for text/notepad file.
         Set fso = CreateObject(“Scripting.fileSystemObject”)
------------------------------------------------------------------------------------------------------------------------
2-InputBox :- We can use this function for reading input from the keyboard.
    Syntax-
         variableName = InputBox(“Your Message”)
------------------------------------------------------------------------------------------------------------------------
3-MsgBox : - We can use this function to display your message.
    Syntax –
         msgBox(“Your Message”)
------------------------------------------------------------------------------------------------------------------------- 
4-Arrays() :-  It’s used to create an array with specified list of values.
    Syntax-
         variableName = Array(list of value)
-------------------------------------------------------------------------------------------------------------------------
5-Ubond(): - We can use this function to get the index of last element of array.
    Example –
          Dim arrayName, size
         arrayName = Array(10,20,30,40)
         size = Ubond(arrayName)+1
    it’s shows the size of array and it will be return array size

6-LBound():-  We can use this function to get index of first element of an array.
------------------------------------------------------------------------------------------------------------------------- 7-Len(): - This function is used to find the length of given string.
    Example –
         Dim name = “TheITQC”
         Length = Len(Name)
    It will return the string length = 7
------------------------------------------------------------------------------------------------------------------------- 
8-Left():- left function is used to get the specified number of character from the left side of the given  string.
    Example –
         Dim name = “My Name is …..”
         leftChar = left(Name, 4)
    It will be return “My N” from the string “My Name is …..”
------------------------------------------------------------------------------------------------------------------------- 9-Right():- This function is used to get the specified number of character from the right side of the given string.
    Example-
         Dim name = “My Name is Test”
         rightChar = right(Name, 7)
    It will be return “Test” from the string “My Name is TheITQC”
-------------------------------------------------------------------------------------------------------------------------
10-Mid():- Mid function is used to get the substring from the main string.
    Syntax-
    mid (“Main String”, substring start point, length of sub string)
    Example-
         Dim name = “Testing with TheITQC”
         mid(Name,13 ,7)
    It will be return “TheITQC”
-------------------------------------------------------------------------------------------------------------------------
11-Split():- This function is used separate the line of text into the string. In this function we need to use separator for getting your desired value.
    Example -
         x = Split(“Testing with ITQC”, “ ”)
    Here we use blank/white space is used as separator.
-------------------------------------------------------------------------------------------------------------------------
12-Trim(): - This Function is used to remove the white space from the given string.
    Example-
         Dim name1= “  TheITQC”
         Dim name2 = “TheITQC  ”
         Dim name3 = “  TheITQC  ”
Here name1 has a left white space, name2 has right white space and name3 has both side white space, if you want to remove those white spaces from the string then we use Trim function like below –
-------------------------------------------------------------------------------------------------------------------------
    1-strName = LTrim(name1)

    LTrim function removes the left hand side white space from the given string. Hence it will be returns “TheITQC”

    2-strName = RTrim(name2)

    RTrim function removes the right hand side white space from the given string. It will be return “TheITQC”.

    3-strName = Trim(name3)

    Trim function removes the white spaces from the given strings and it will be returns “TheITQC”
-------------------------------------------------------------------------------------------------------------------------   
13-Instr() :- Instr() function is used to find the existing sub string from the main string. Instr function will return 0 when given or substring not found in main string.
    Example –
         msgBox(Instr(“We are Software Tester”, “Tester”))
-------------------------------------------------------------------------------------------------------------------------
14-Join(): - This function is used to combine two or more sub string as a main string.
    Syntax-
         Join(list of sub Strings)
    Example-
         strName = Join(“We”, “are”, “Tester”)
    it will be return strName = “We are Tester”
-------------------------------------------------------------------------------------------------------------------------
15-UCase()& LCase() :- Ucase is used to converting a string into a Upper case and LCase is used to converting string into a lower case.
    Syntax-
    1-LCase(“String in UpperCase”)
    2-UCase(“String in lowerCase”)
-------------------------------------------------------------------------------------------------------------------------
16-strComp() : - We are used this function for comparing two strings.
    Syntax-
         strComp(“string1”, “string2”)
This function return 0 when both strings are same, it will be return -1 when string1 less than string2 or it will be return 1 when string1 greater than string2.
-------------------------------------------------------------------------------------------------------------------------
17-strReverse():- This function is used to reverse the characters from the given strings.
    Example-
         name = strReverse(“TEST”)
    It will be return “TSET”
-------------------------------------------------------------------------------------------------------------------------
18-Replace(): - We can used this function for replacing the specified String/Characters by using another string/character in the given string.
    Example-
         Replace(“xxpXPxP”, “p”, “y”)
    It will be return “xxyXPxP” here we are replace all p with y.
-------------------------------------------------------------------------------------------------------------------------
19-Eval():- This function is used to evaluate the given mathematical expression.
    Example-
         a= 2*3
         b= eval(a)
    It will be return 6.
-------------------------------------------------------------------------------------------------------------------------
20-Round():- If you want to get rounded no. from the given value at that time we are use this function.
    Example-
         Dim varName, Pi
         Pi= 3.14159
         varName = round(Pi, 2)
    it will be return 3.14
-------------------------------------------------------------------------------------------------------------------------
21-Int():- We are using this function for capturing the integer part from the given number, without rounding.
    Example-
         Dim a, b
         a= 3.14159
         b= int(a)
    it will be returns 3.
-------------------------------------------------------------------------------------------------------------------------
22-CInt(): -This function is used to converting the given number into the integer number. It will converting number with rounding.
    Example-
         Dim a, b
         a= 3.14159
         b=Cint(a)
    it will be returns 3.
         Dim a, b
         a= 3.890
         b=Cint(a)
    it will be return 4.
-------------------------------------------------------------------------------------------------------------------------
23-Cdbl():- We can use this function to convert given value into a double or float.
         Example- 
              Dim a, b
              a= 3.1415912457812
              b=Cdbl(a)
       it will be returns 3.1415912457812
-------------------------------------------------------------------------------------------------------------------------
24-Cbool():- Converting the given value into the Boolean value.
    Example-
         Dim x, y
         x= 1
         y= 2
         If y>x = Cbool(true) then
           ………….
           ………….
         End if
-------------------------------------------------------------------------------------------------------------------------
25-Cstr():-  This function is used to converting the given value into a string. Basically TestComplete treated any values as a string.

26-varType():- This function is used to get the variable type.

27-Now():- Now function is used for get the system’s current Date and Time.
         Dim d
         d= Now()
    it will be return current date and time of the system
-------------------------------------------------------------------------------------------------------------------------
28-Date():- This function is used to get the current Date only.
         Dim d
         d= Date()
    it will be return current date of the system
-------------------------------------------------------------------------------------------------------------------------
29-Time():- This function is used to get the current Time only.
         Dim d
         d= Time()
    it will be return current date and time of the system
-------------------------------------------------------------------------------------------------------------------------