Wednesday, 31 October 2012

Loops in VBScript



LOOPS IN VBScript:  To execute a block of code more than one time, we can use loops in VBScript

a)      WHILE LOOP:  To execute a block of code more than one time as long as given condition is true

----------------
---------------- (1)
----------------
                       While Condition
----------------       (2)
----------------        True (Loop)
  (3) False (Exit)               ----------------
                                                Wend
----------------
----------------
----------------
NOTE:  While loop ends with “Wend” (Not case sensitive)


b)      DOWHILE LOOP:  To execute a block of code more than one time as long as the condition is true, In this loop, the code will be executed at least once at the beginning without a condition

----------------
----------------
----------------
                                                    Do    (1)                  
----------------       (2)
----------------        True (Loop)
                              ----------------
                                                While condition
----------------  (3) False (Exit)
----------------
----------------

c)       DOUNTIL LOOP:  To execute a block of code more than one time as long as given condition is false, we can use this loop


----------------
----------------
----------------
                                                 Do (1)
----------------       (2)
----------------        False (Loop)
                              ----------------
                                                Until condition
----------------  (3) True (Exit)
----------------
----------------
NOTE:  DOUNTIL loop is otherwise around to DOWHILE loop

VBScript has an option of “EXIT DO” statement to terminate from “DOWHILE” and “DOUNTIL” loops if required


----------------
----------------
----------------
                                                   Do                            
----------------      
----------------       
                              ----------------
                                                If condition then
                                                Exit do

                            True (Exit)                               While condition

----------------   False (Exit)
----------------
----------------




d)      FORLOOP:  To execute a block of code for specified number of times, we can use this loop

EXP:
For i = 1 to 10 step 1

                ------------------
                ------------------
                ------------------
                      Next (Exit)

The above loop executes 10 times

EXP:
For i = 0 to 9 step 1

                ------------------
                ------------------
                ------------------
                      Next (Exit)

The above loop executes 10 times (here “0” counts as one loop)


EXP:
For i = 10 to 1 step -1

                ------------------
                ------------------
                ------------------
                      Next (Exit)

The above loop executes 10 times


EXP:
For i = 1 to 10 step 3

                ------------------
                ------------------
                ------------------
                      Next (Exit)

The above loop executes 4 times (first execution is 1, then 4, then 7, and 10)

e)      FOREACH LOOP:  To run a block of code more than one time depends on each element in an array or list
EXP:                            
                X = array (123, “mindq”, “abc”, 789)
                                 X(0)       x(1)        x(2)     x(3)

------------------
                ------------------
                ------------------
Next (Exit)
This loop executes 4 times based on he values in X array
ARRAYS IN VBScript:  Variables are used to store one value at a time of any type, but arrays are used to store multiple values of any type.  To create arrays in VBScript, we can follow below code

Option explicit
Dim x(4)

In above example, X is an ARRAY with 5 values in it (0-4)

EXP:           x(0)      x(1)           x(2)         x(3)        x(4)
“abc”
123
“mindq”
10.5
“date”
       X=
From the above diagram, X is an array with 5 values or elements and each element will be accessible by using an array name and index, index starts with x(0)

EXP:  x(3)  = 4th element in “X” array

The first index x(0) is called LBOUND (lower bound)
The last index x(4) is called UBOUND (upper bound)

EXPAMPLE#1:  Write a VBScript code in QTP tool to create an array with 5 elements to store 5 subject marks into that array, and display total marks
Option explicit
Dim x(4), i, y
For i = 0 to 4 step 1
    x(i)=inputbox ("Enter a subject marks")
Next
    y=0
For i = 0 to 4 step 1
    y = y+x(i)
Next
    msgbox("Total Marks   "&y)

EXPAMPLE#2:  Write a VBScript code in QTP tool to create an array with 5 elements to store 5 Sentences, with at least one or two sentences with a word “INDIA”

Option explicit
Dim x(4), i
For i = 0 to 4 step 1
   x(i)=inputbox ("Enter a Sentence")
Next
For i = 0 to 4 step 1
If instr (x(i), "india")<>0 Then
   msgbox (x(i))
End If
Next

INSTR ( ):- This function is used to check the availability of sub string in main string

SYTAX:  instr(“mindq”, “testing”)
                (m-string)  (s-string)

     If available returns “position”
     If not available returns “0”

Tuesday, 30 October 2012

Quick Test Professional Using VBScript Projects




Please Install QTP Trial Version To Practice These Excercises.  Its Really Fun Learning :-)
Please forgive me if there are any format changes due to compatiblity problems.

QTP TOOL LAUNCHING:                                                                                                
Ø  Start
Ø  QTP
Ø  Click “Continue” in license screen
Ø  Click “OK” in add in manager
Ø  File menu
Ø  New test
VARIABLES IN VBScript:
Variable means a storage, which consists of a value in any type, such as Intigen, Float, Character, String, Boolean…etc
Exp:       X = 10 (Here 10 is an Integer)
                X = 27.8 (Here 27.8 is a Float)
                X = “A” (Here “A” is a character)
                X = “MindQ” (Here “MindQ” is a String)
NOTE:  In VBScript, characters and string values must be enclosed by double quotes (“ “)
OPERATORS IN VBScript
Similar to other programming languages, VBScript also have a list of arithmetic operators as follows:
Exponentiation ^ (Power of)
Multiplication    *
Division                                /
Modular Arithmetic        Mod (Any Integer/2 = 0)
Addition                               +
Subtraction                         -
String Concatenation      & (Adding two Strings)

Difference between Division and Modular Arithmetic


                2)100(50               100/2 is Division
                   100
                -----------
0                          0 is Mod

String Concatenation example as follows

X = “MindQ”
Y = “Systems”
Z = X&Y = “MindQSystems” (no space in between)


Example#1:  Write VBScript Code in QTP Tool to multiply two numbers as follows

X = inputbox(“Enter a number”)
Y = inputbox(“Enter a number”)
Z = X*Y
msgbox(“Z value”)

Example#2:  Write VBScript Code in QTP Tool to add two numbers as follows

X = inputbox(“Enter a number”)
Y = inputbox(“Enter a number”)
Z = cint(X)+cint(Y)
msgbox(“Z value”)

NOTE:  In VBScript “+” operator can override with Addition and Concatenation.  To get addition for “+” operator, we can use casting

TYPES OF CASTING FUCTIONS

Cint ( ) -> convert to INTEGER
Clng ( ) -> convert to LONG INTEGER
Cdbl ( ) -> convert to DOUBLE FLOAT
Cstr ( ) -> convert to STRING (by default in VBScript)
Cdate ( ) -> convert to DATE values
Ccur ( ) -> convert to CURRENCY values

COMPARISON OPERATORS IN VBScript

Similar to other programming languages, VBScript also have some comparison operators

Equality                                -> =
Inequality                             -> <>
Less than                              -> <
More than                             -> >
Less than or equal                -> <=
Greater than or equal           -> >=

LOGICAL OPERATORS IN VBScript            

Similar to other programming languages, VBScript also have some logical operators to combine conditions

AND       ->  Conjunction
OR          ->  Disjunction
NOT       ->  Negation

INPUT AND OUTPUT FUNCTIONS

To feed INPUT from keyboard for variables, we can use the below function
Inputbox (“message”) =

To get OUTPUT on the desktop screen, we can use the below function
Msgbox (variable)
Msgbox (“message”)
Msgobx (“message”&variable)
CONDITIONAL STATEMENTS      

In VBScript, one line of code is called one STATEMENT, but some statements are checking conditions

a)      “if” conditional statement:  we can use this statement to execute below code depends on condition

----------------
----------------
----------------
If condition then
----------------
----------------  True
----------------
Else
----------------
False            ----------------
----------------
End if
----------------
----------------
----------------

Example: Write VBScript code in QTP tool to check given number is even or odd

X = inputbox(“enter number”)
If X mod 2=0 then
   Msgbox(X& “even number”)
Else
   Msgbox(X& “odd number”)
End if

b)      IF ELSE IF conditional statement we can use this statement to check multiple conditions

If condition then
----------------------
----------------------
Else if condition then
----------------------
----------------------
Else if condition then
----------------------
----------------------
Else
----------------------
----------------------
End if
----------------------
----------------------

Example#1: Write VBScript code in QTP tool to check student’s TOTAL MARKS to display the GRADE
If total marks >=800 then grade is “A”
If total marks <800 and="and">=700 then grade is “B”
If total marks are <700 and="and">= 600 then grade is “C”
If total marks are <600 grade="grade" is="is" p="p" then="then">
x=inputbox("enter student total makrs")
  If x >=800 then
     msgbox("Grade A")
elseIf x < 800 and x >= 700 Then
    msgbox("Grade B")
elseIf x < 700 and x >= 600 Then
    msgbox("Grade C")
else
   msgbox("Grade D")
End If

Example#2: Write VBScript code in QTP tool to GROSS SALARY of an employee based on BASIC SALARY and COMMISSION

NOTE:  Gross salary is equal to Basic Salary + Commission

If basic salary >=50000 then commission is 10% of basic salary
If basic salary <50000 and="and">=35000 then commission is 5% of basic salary
If basic salary is <35000 commission="commission" is="is" p="p" rs200="rs200" then="then">

x=inputbox("Enter Basic Salary")
If x >=50000 then
   msgbox("Gross Sal   "& x+x*10/100)
elseIf x < 50000 and x >= 35000 Then
   msgbox("Gross Sal   " &x+x*5/100)
else
   msgbox("Gross Sal   " &x+200)
End If

SELECT CASE STATEMENT:

To execute a block of code depends on a value instead of condition, we can use this statement

Example#1:  Write VBScript code in QTP tool to Validate Credit Cards

Visa, American Express, and Mastercard are valid cards, other cards are invalid

x=inputbox("Enter Card Type")
Select Case x
Case "Visa"
   msgbox ("valid card")
Case "American Express"
   msgbox ("valid card")
Case "Mastercard"
   msgbox ("valid card")
Case else
   msgbox ("invalid card")
End Select

NOTE:  As per the above example, VBScript Language Coding is not case sensitive, but INPUT and OUTPUT data are case sensitive.


Example#2:  Write VBScript code in QTP tool to Validate Week Days by giving numbers as input

SUNDAY -1
MONDAY -2
TUESDAY- 3
WEDNESDAY-4
THURSDAY -5
FRIDAY -6
SATURDAY - 7

x=inputbox("Enter Day Number")
Select Case x
Case 1
   msgbox ("Sunday")
Case 2
   msgbox ("Monday")
Case 3
   msgbox ("Tuesday")
Case 4
   msgbox ("Wednesday")
Case 5
   msgbox ("Thursday")
Case 6
   msgbox ("Friday")
Case 7
   msgbox ("Saturday")
Case else
   msgbox ("Invalid Day Number")
End Select