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”800>
If total marks are <700 and="and">= 600 then grade is “C”700>
If total marks are <600 grade="grade" is="is" p="p" then="then">
600>
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
salary50000>
If basic salary is <35000 commission="commission" is="is" p="p" rs200="rs200" then="then">
35000>
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
No comments:
Post a Comment