Introduction to VBScript
Display user defined message:
MsgBox Function: -It displays given message and waits for the user to click OK button or close dialog
Ex: - MsgBox “Welcome to QTP World”
Comments in QTP: - By using single cots (‘) user can give the comments for scripts
Display Message with Required Dialog Title and Buttons: -
Syntax: -MsgBox “<message>”, button no, “<dialog title>”
Ex: -MsgBox “Welcome to QTP World”, 0 “sample”
Here: - 0 is the number for the different types of buttons we have 0, 1, 2, 3, 4, 5, 16, 32…etc. go to
MsgBox help by pressing function1 (F1).
“Sample” is dialog title.
Print Statement: -
- It displays user given message in Quick Test Print Log window
- It doesn’t require any user interaction
- It will works in QTP only
Note: - MsgBox is defined in VBScript
Print statement defined in QTP
Display a message and Close: -**
Set obj= CreateObject (“Wscript.Shell”)
obj.Popup “Welcome to QTP World”, 5, “Sample”
Here: - number 5 is seconds to close the popup
Reading Values during runtime: -
InputBox: -it displays a prompt and waits for the user, to enter text and click OK/ Cancel button
Syntax: - <variable>= InputBox (“<message>”)
Ex: - Val = InputBox (“Enter any Value”)
MsgBox Val
Operators: -
Types of VBScript Operators: -
1. Arithmetic Operators
2. Assignment Operator
3. Comparison Operator
4. Concatenation Operators
5. Logical Operators
Arithmetic Operators: -
These are used to perform Mathematical operations on given values.
1. ^ operators: -It raises a number to the power of an exponent
Ex: - MsgBox 2^3 ‘returns 8 (2*2*2)
2. * operators: - Multiplies
Ex: - MsgBox 2*3 ‘returns 6
3. / operators: - divides
Ex: - MsgBox 2/3 ‘returns 0.5(Float value)
4. \ operators: - divides
Ex: - MsgBox 2/3 ‘returns 0(Integer value)
5. MOD operators: - divides
Ex: - MsgBox 7 mod 5 ‘returns 2 (7 divided by 5)
6. + operators: - Sum of given number
7. - operators: - Finds difference between given number
Assignment Operator: -
= operator: - assigns a given value to a variable
Ex: - Val = 10
MsgBox Val
Comparison Operator: -
These are used to compare two given values
Ex: - Val1=10
Val2 = 20
MsgBox Val1 < Val2 ‘Returns True
MsgBox Val1 > Val2 ‘Returns False
MsgBox Val1 <> Val2 ‘Returns True
MsgBox Val1 = Val2 ‘Returns False
Concatenation Operator: -
& operator: -It concatenates/ appends to given values
Ex: - Val1= “Hi”
Val2 = “Bye”
MsgBox Val1 & Val2 (Without space)
MsgBox Val1 &” ” & Val2 (With space)
Logical Operator: -
AND operator: - Perform logical conjunction on 2 given conditions
OR operator: - Perform logical disjunction on 2 given conditions
NOT operator: - Perform logical disjunction on 2 given conditions
Ex: -
Val1 = 10
Val2 = 20
Val3 = 30.
MsgBox Val1 < Val2 AND Val2 < Val3
MsgBox Val1 >Val2 AND Val2 > Val3
MsgBox Val1 < Val2 OR Val2 < Val3
MsgBox Val1 > Val2 OR Val2 > Val3
MsgBox NOT Val1 < Val2 OR Val2 < Val3
MsgBox NOT TRUE ‘Returns False
MsgBox NOT FALSE ‘Returns True
Conditional statement: -
These are used to execute a group of statements depends on given condition
VBScript Conditional statements: -
1. If…. Then
2. If…. Then…. Else
3. If…. Then…. Else…. If
4. Select…. case
If….Then: -
It executes a group of statements when the condition is pass/true
Syntax: - If<condition> Then
<Statement>
End If
Ex: - Apply If Then condition for bellow scenario.
1> Do you know Testing Tools course, If yes then display a message Try for a Job
Val= InputBox ("Do you know Test Tools?")
If Val="yes" Then
MsgBox "Try for Job"
End If
If….Then….Else: -
It executes a group of statement condition are Pass or Fail
Syntax: - If<condition> Then
<Statement>
Else
<Statement>
End If
Ex: - Apply If Then Else condition for bellow scenario.
1> Do you know Testing Tools course, If yes then display a message Try for a Job. If No then display
message “go to LoginF1to learn”
Val1 = InputBox ("Do you know Testing Tools?")
If Val1= "yes" Then
MsgBox "Try for Job"
Else
MsgBox "Go to Login F1 to Learn TT"
End If
**** End of Chapter III ****
Ex: -
Val1 = 10
Val2 = 20
Val3 = 30.
MsgBox Val1 < Val2 AND Val2 < Val3
MsgBox Val1 >Val2 AND Val2 > Val3
MsgBox Val1 < Val2 OR Val2 < Val3
MsgBox Val1 > Val2 OR Val2 > Val3
MsgBox NOT Val1 < Val2 OR Val2 < Val3
MsgBox NOT TRUE ‘Returns False
MsgBox NOT FALSE ‘Returns True
Conditional statement: -
These are used to execute a group of statements depends on given condition
VBScript Conditional statements: -
1. If…. Then
2. If…. Then…. Else
3. If…. Then…. Else…. If
4. Select…. case
If….Then: -
It executes a group of statements when the condition is pass/true
Syntax: - If<condition> Then
<Statement>
End If
Ex: - Apply If Then condition for bellow scenario.
1> Do you know Testing Tools course, If yes then display a message Try for a Job
Val= InputBox ("Do you know Test Tools?")
If Val="yes" Then
MsgBox "Try for Job"
End If
If….Then….Else: -
It executes a group of statement condition are Pass or Fail
Syntax: - If<condition> Then
<Statement>
Else
<Statement>
End If
Ex: - Apply If Then Else condition for bellow scenario.
1> Do you know Testing Tools course, If yes then display a message Try for a Job. If No then display
message “go to LoginF1to learn”
Val1 = InputBox ("Do you know Testing Tools?")
If Val1= "yes" Then
MsgBox "Try for Job"
Else
MsgBox "Go to Login F1 to Learn TT"
End If
If….Then….Else….If: -
Maintaining conditional statement, in another conditional statement
Syntax: - If<condition> Then
<Statement>
Else
If<condition>Then
<Statement>
End If
End If
Ex: - Apply If….Then….Else….If to bellow scenario,
Do you know testing tool course. If yes then find do you know QTP. If QTP know then display a message
Try for JOB. If QTP doesn’t know then display a message “Go to login f1 to learn QTP”. If testing tool
doesn’t know then display a message “Got login f1 to learn TT”.
Val2 = InputBox ("Do you know Testing Tools?")
If Val2="yes" Then
Val3= InputBox ("Do you know QTP?")
If val3="yes" Then
MsgBox "Try for job"
Else
MsgBox "Go to login F1 to learn QTP"
End If
Else
MsgBox "Go to Login F1 to learn TT"
End If
Select….Case: -
It is used to execute a group of statements depends on multiple conditions
Syntax: -
Select Case <variable>
Case 1 <condition>
<Statement>
Case 2 < condition >
<Statement>
Case 3 < condition >
<Statement>
Case Else ‘Optional
<Statement>
Ex: -Display one number and display a day name
Val = InputBox ("Enter any number")
Select Case Val
Case 1
MsgBox “Sun Day"
Case 2
MsgBox "Mon Day"
Case 3
MsgBox “Tue Day"
Case 4 MsgBox "Wed Day"
Case 5 MsgBox "Thu Day"
Case 6 MsgBox "Fri Day"
Case 7 MsgBox "Sat day"
Case Else MsgBox "Invalid Number"
End Select
Variable Declaration: -
Variable: -It is a storage place where we store variable.
Declaration: -We declare the variable by using DIM statement.
Dim Var1, Var2…
Note: - ****
1. Variable names should be meaning full
2. Variable name should explain what type of data we are storing in it
Ex: -
- If store INTIGER then we starts with ‘i’
- If store STRING then we starts with‘s’
- If store DOUBLE then we starts with‘d’
- If store BOOLEAN then we starts with ‘b’
- If store DATE then we starts with ‘dt’
- If store TIME then we starts with‘t’
3. Variable names are not case sensitive but we follow standards
4. Variable names starts with small latter, if it contains multiple words then second word on wards the first latter
should be a capital latter
Ex: -Dim sEmpName, dEmpSal, dtDob, iEmpId….
5. Default variable is EMPTY
6. When we give the values (numbers) from InputBox for ‘+’ operator it treats as String Integer and concatenates
the values. For ‘-‘, ‘*’, ‘/’ operators converts the numbers from String Integers into integer values and do the
mathematical operations
7. VBScript variables are variant data type (any kind of data)
Ex: -Variable: -
MsgBox Krishna + sreekar ‘Returns 0 (zero)
MsgBox Krishna - sreekar ‘Returns 0 (zero)
MsgBox Krishna * sreekar ‘Returns 0 (zero)
MsgBox Krishna / sreekar ‘Returns Overflow error Massage
Strings: - MsgBox “Krishna” + “sreekar” ‘Returns Appends / concatenates the strings
MsgBox “Krishna” - “sreekar” ‘Returns Error msg as not able to perform String subtraction
MsgBox “Krishna” * “sreekar” ‘Returns Error msg not able to perform String Multiplication
MsgBox “Krishna” / “sreekar” ‘Returns Error msg not able to perform String Division
Strings and Variable: -
MsgBox “Krishna” + sreekar ‘Returns krishna
MsgBox “Krishna” - sreekar ‘Returns Error
MsgBox “Krishna” * sreekar ‘Returns Error
MsgBox “Krishna” / sreekar ‘Returns Error
String Integers: -
MsgBox “10” +”20” ‘Returns 1020- appends/ concatenates
MsgBox “10” -”20” ‘Returns -10
MsgBox “10” *”20” ‘Returns 200
MsgBox “10” /”20” ‘Returns 0.5
String Integers and Numbers: -
MsgBox “10” + 20 ‘Returns 30
MsgBox “10” - 20 ‘Returns -10
MsgBox “10” * 20 ‘Returns 200
MsgBox “10” / 20 ‘Returns 0.5
String and Numbers: -
MsgBox “krishna” + 20 ‘Returns Error Massage
MsgBox “krishna” - 20 ‘Returns Error Massage
MsgBox “krishna” * 20 ‘Returns Error Massage
MsgBox “krishna” / 20 ‘Returns Error Massage
In VBScript declaring variables are not mandatory, But we are declaring
[Q: - Is variable declaration is mandatory in VBScript? answer is no then next...Q: - Why?]
Option Explicit: -***
It displays error massage for undefined variable names. Option Explicit is must declared in the first line of the
program.
Ex: -
OptionExplicit
Dim sEmpname
sEmpName “sreekar”
MsgBox sEName ‘Returns Error Massage for not defined sEName variable
Note: - Variable names should not be a KEY word
Loop Statement: -
These are used to execute a group of statements for multiple times
VBScript Loop Statements: -
1. For… Next
2. While… Wend
3. Do… Loop
For…Next: - It executes a script for required number of given times
Syntax: - For <variable> = <Start Position > To < End Position > [Step]
<Statement>
[ExitFor]
Next
{Here written in [] are optional}
Ex: - Display a massage “QTP” for 5 times
For index = 1 To 5
MsgBox index
Next
> In bellow code a and z treats as variable returns empty values so answer is 0
For index = a To z
MsgBox index
Next
> In bellow code a and z treats as variable returns empty values so answer is 0
For index = “a” To “z”
MsgBox index ‘Returns error massage
Next
Ex: - Display numbers from 5 to 10
> In bellow given string integers are converts in to integers
For index = “5” To “10”
MsgBox index
Next
STEP Command:
Ex: -
For index = 1 To 5 Step 3
MsgBox index ‘Returns 1, 3
Next
Ex: - Display even numbers 2 to 10
For index = 2 To 10 Step 2
MsgBox index ‘Returns 2, 4, 6…
Next
Ex: - Display odd numbers from 1 to 10
For index = 1 To 10 Step 2
MsgBox index ‘Returns 1, 3, 5…
Next
Ex: - Display numbers from 5 to 1
For index = 5 To 1 Step -1
MsgBox index ‘Returns 4, 3…
Next
ExitFor: -
Ex: - For index = 1 To 10 Step 2
MsgBox index ‘Returns 1 and exits from For loop
ExitFor
Next
Ex: - For index = 1 To 10 Step 2
MsgBox index
If index = 3 Then
ExitFor
EndIf
Next
While… Wend: - It execute group if statements until condition is True
Syntax: -
While <condition>
<Statement>
Wend
Ex: - Display a massage “QTP” for 5 times
index = 1
While index <= 5
Print "QTP"
index = index+1
Wend
Ex: -Display numbers from 1 to 10
index = 1
While index <= 10
Print index
index = index+1
Wend
Ex: - Display odd numbers from 1 to 10
index = 2
While index <= 10
Print index
index = index+2
Wend
Do… Loop: - It executes group statement until condition becomes True
Syntax: - Do <condition>
<Statement>
Loop While <condition>
Ex: -Display a massage “QTP” for 5 times
index = 1
Do
Print "DTP"
index = index+1
Loop While index = 5
Ex: -Display numbers from 1 to 10
index = 1
Do Print index
index = index+1
Loop While index <= 10
Functions
Functions are used for scripts reusability
1. Built-in Functions
2. User Defined Functions
Built-in Functions: -
These are pre defined by VBScript
Note 1: - Function names are not Case Sensitive
Note 2: - We follow standards like function name starts with capital later if it contains multiple words then 2nd word
on words 1st later should be a capital latter
Ex: -
For index = 1 To 5 Step 3
MsgBox index ‘Returns 1, 3
Next
Ex: - Display even numbers 2 to 10
For index = 2 To 10 Step 2
MsgBox index ‘Returns 2, 4, 6…
Next
Ex: - Display odd numbers from 1 to 10
For index = 1 To 10 Step 2
MsgBox index ‘Returns 1, 3, 5…
Next
Ex: - Display numbers from 5 to 1
For index = 5 To 1 Step -1
MsgBox index ‘Returns 4, 3…
Next
ExitFor: -
Ex: - For index = 1 To 10 Step 2
MsgBox index ‘Returns 1 and exits from For loop
ExitFor
Next
Ex: - For index = 1 To 10 Step 2
MsgBox index
If index = 3 Then
ExitFor
EndIf
Next
While… Wend: - It execute group if statements until condition is True
Syntax: -
While <condition>
<Statement>
Wend
Ex: - Display a massage “QTP” for 5 times
index = 1
While index <= 5
Print "QTP"
index = index+1
Wend
Ex: -Display numbers from 1 to 10
index = 1
While index <= 10
Print index
index = index+1
Wend
Ex: - Display odd numbers from 1 to 10
index = 2
While index <= 10
Print index
index = index+2
Wend
Do… Loop: - It executes group statement until condition becomes True
Syntax: - Do <condition>
<Statement>
Loop While <condition>
Ex: -Display a massage “QTP” for 5 times
index = 1
Do
Print "DTP"
index = index+1
Loop While index = 5
Ex: -Display numbers from 1 to 10
index = 1
Do Print index
index = index+1
Loop While index <= 10
Functions
Functions are used for scripts reusability
1. Built-in Functions
2. User Defined Functions
Built-in Functions: -
These are pre defined by VBScript
- Access functions in QTP
- Insert Menu StepGenerator Category as Functions Library as Built-in Function find the functions in Operation drop down
Note 1: - Function names are not Case Sensitive
Note 2: - We follow standards like function name starts with capital later if it contains multiple words then 2nd word
on words 1st later should be a capital latter
1. Abs() [Absolute]: -It returns absolute value to a given number
Ex: - MsgBox Abs (10) ‘Returns 10
MsgBox Abs (-10) ‘Converts the –ve in to +ve ‘Returns 10
MsgBox Abs (10.56) ‘Returns 10.56
MsgBox Abs (-10.56) ‘Returns 10.56
MsgBox Abs ("10") + Abs ("20") ‘Returns 30 – Converts the string into Integer Value
2. Asc(): - It returns ASCII to given Character
Ex: -MsgBox Asc ("a") ‘Returns 97
MsgBox Asc ("z") ‘Returns 122
MsgBox Asc ("A") ‘Returns 65
MsgBox Asc ("Z") ‘Returns 90
3. Chr(): - It returns ASCII to given Character
Ex: - MsgBox Chr (97) ‘Returns a
MsgBox Chr (122) ‘Returns z
MsgBox Chr (65) ‘Returns A
MsgBox Chr (90) ‘Returns Z
4. CInt(): - It Converts String Integer in to Integer
Ex: - iVal1 = "10"
iVal2 = "20"
MsgBox iVal1+iVal2 ' Returns 1020
MsgBox CInt (iVal1) +CInt (iVal2) ' Returns 30
Ex: - 10.56 the CInt Converts into 11
iVal1= "10.56"
iVal2= "-20.78"
iVal1= iVal1*100
iVal2= iVal2*100
MsgBox (iVal1+iVal2) / 100
5. CStr(): - It converts given value into Strings
iVal1 = 10
iVal2 = 20
MsgBox iVal1+iVal2 Returns 30
MsgBox CStr (iVal1) + CStr (iVal2) ‘Returns 1020
MsgBox CStr (iVal1) – CStr (iVal2) ‘Returns -10
Date and Time Factions
6. Date: - It returns current system Date in MM/DD/YYYY format
Ex: - MsgBox Date
7. Time: - It returns current system Time in HH/MM/Sec format
8. Ex: - MsgBox Time
9. Now(): - It returns current system Date & Time
Ex: - MsgBox Now
10. DateAdd(): - This function add/ subtracts given date or time, given date and time
Ex: - MsgBox DateAdd ("yyyy", 1, Date) ‘Returns Year with adding 1 to the current year
MsgBox DateAdd ("q", 1, Date) ‘Returns Quarter with adding 1 to the current Quarter
MsgBox DateAdd ("m", 1, Date) ‘Returns Month with adding 1 to the current Month
MsgBox DateAdd ("y", 1, Date) ‘Returns Day of year with adding 1 to the current Day of year
MsgBox DateAdd ("d", 1, Date) ‘Returns Day with adding 1 to the current Day
MsgBox DateAdd ("w", 1, Date) ‘Returns Weekday with adding 1 to the current Weekday
MsgBox DateAdd ("ww", 1, Date) ‘Returns Week of year with adding 1 to the current Week of year
MsgBox DateAdd ("h", 1, Date) ‘Returns Hour with adding 1 to the current Hour
MsgBox DateAdd ("n", 1, Date) ‘Returns Minute with adding 1 to the current Minute
MsgBox DateAdd ("s", 1, Date) ‘Returns Second with adding 1 to the current Second
MsgBox DateAdd ("yyyy", 8000, Date) ‘Returns Error because year above 10000
11. DateDiff (): - This function is used to find the difference in between given 2 dates or 2 times. In day, Month,
Year, hour, Minutes, Seconds intervals
Syntax: -MsgBox DateDiff (Interval, date1, date2)
MsgBox DateDiff ("d", Date,"01-01-2013") 'Returns -2 (My System date 03-01-2013)
MsgBox DateDiff ("d", "01-01-2013", Date) 'Returns 2 (My System date 03-01-2013)
12. DatePart(): - It is used to retrieve required interval date or time from given date or time
Syntax: - MsgBox DatePart (Interval, Date/ Time)
MsgBox DatePart (“d”, Date)
MsgBox DatePart (“m”, Date)
MsgBox DatePart (“yyyy”, Date)
13. Day(): - It retrieves day from given date
Ex: - MsgBox Day (Date)
14. Month(): - It retrieves Month from given date
Ex: - MsgBox Month (Date)
15. MonthName(): - It retrieves the name of the month from given number from 1 to 12
Ex: - MsgBox MonthName (Date)
16. Year(): - It retrieves Year from given date
Ex: - MsgBox Year (Date)
17. WeekDay(): - It retrieves Weekday from given date
Ex: - MsgBox WeekDay(Date)
18. WeekDayName(): - It retrieves WeekDayName from given number(1-7)
Ex: - MsgBox WeekDayName (1) ‘Returns Sunday
MsgBox WeekDayName (WeekDay (Date)) ‘Returns current WeekDayName
Boolean Functions: -
19. IsDate (): -It retrieves Boolean value for given expression is date or not. It returns True if given value is date
else false
Ex: - MsgBox IsDate (“Suresh”) ‘Returns False
MsgBox IsDate (“3-01-2013”) ‘Returns True
20. IsEmpty (): -It retrieves Boolean value for given value is Empty or not. It returns True if given value is Empty
else false
Ex: - Dim val1, val2, val3
Val1 = 10
Val2 = Null
MsgBox IsEmpty(Val1) ‘Returns False
MsgBox IsEmpty(Val2) ‘Returns False
MsgBox IsEmpty(Val3) ‘Returns True
21. IsNull (): -It retrieves Boolean value for given value is Null or not. It returns True if given value is Null else
false
22. IsObject (): - It retrieves Boolean value for given expression is Object or not. It returns True if given value is
Object else false
Ex: - Dim Val1, Obj
Val1 = 10
Set obj = CreateObject (WScript. Shell)
MsgBox IsObject (Val1) ‘Returns False
MsgBox IsObject (Obj) ‘Returns True
23. IsNumeric (): - It retrieves Boolean value for given expression is Numeric or not. It returns True if given value
is Numeric else false
Ex: - Dim val1, val2, val3
Val1 = 10
Val2 = 13.45
Val3= “suresh”
MsgBox IsNumeric (Val1) ‘Returns True
MsgBox IsNumeric (Val2) ‘Returns True
MsgBox IsNumeric (Val3) ‘Returns False
4. CInt(): - It Converts String Integer in to Integer
Ex: - iVal1 = "10"
iVal2 = "20"
MsgBox iVal1+iVal2 ' Returns 1020
MsgBox CInt (iVal1) +CInt (iVal2) ' Returns 30
Ex: - 10.56 the CInt Converts into 11
iVal1= "10.56"
iVal2= "-20.78"
iVal1= iVal1*100
iVal2= iVal2*100
MsgBox (iVal1+iVal2) / 100
5. CStr(): - It converts given value into Strings
iVal1 = 10
iVal2 = 20
MsgBox iVal1+iVal2 Returns 30
MsgBox CStr (iVal1) + CStr (iVal2) ‘Returns 1020
MsgBox CStr (iVal1) – CStr (iVal2) ‘Returns -10
Date and Time Factions
6. Date: - It returns current system Date in MM/DD/YYYY format
Ex: - MsgBox Date
7. Time: - It returns current system Time in HH/MM/Sec format
8. Ex: - MsgBox Time
9. Now(): - It returns current system Date & Time
Ex: - MsgBox Now
10. DateAdd(): - This function add/ subtracts given date or time, given date and time
Ex: - MsgBox DateAdd ("yyyy", 1, Date) ‘Returns Year with adding 1 to the current year
MsgBox DateAdd ("q", 1, Date) ‘Returns Quarter with adding 1 to the current Quarter
MsgBox DateAdd ("m", 1, Date) ‘Returns Month with adding 1 to the current Month
MsgBox DateAdd ("y", 1, Date) ‘Returns Day of year with adding 1 to the current Day of year
MsgBox DateAdd ("d", 1, Date) ‘Returns Day with adding 1 to the current Day
MsgBox DateAdd ("w", 1, Date) ‘Returns Weekday with adding 1 to the current Weekday
MsgBox DateAdd ("ww", 1, Date) ‘Returns Week of year with adding 1 to the current Week of year
MsgBox DateAdd ("h", 1, Date) ‘Returns Hour with adding 1 to the current Hour
MsgBox DateAdd ("n", 1, Date) ‘Returns Minute with adding 1 to the current Minute
MsgBox DateAdd ("s", 1, Date) ‘Returns Second with adding 1 to the current Second
MsgBox DateAdd ("yyyy", 8000, Date) ‘Returns Error because year above 10000
11. DateDiff (): - This function is used to find the difference in between given 2 dates or 2 times. In day, Month,
Year, hour, Minutes, Seconds intervals
Syntax: -MsgBox DateDiff (Interval, date1, date2)
MsgBox DateDiff ("d", Date,"01-01-2013") 'Returns -2 (My System date 03-01-2013)
MsgBox DateDiff ("d", "01-01-2013", Date) 'Returns 2 (My System date 03-01-2013)
12. DatePart(): - It is used to retrieve required interval date or time from given date or time
Syntax: - MsgBox DatePart (Interval, Date/ Time)
MsgBox DatePart (“d”, Date)
MsgBox DatePart (“m”, Date)
MsgBox DatePart (“yyyy”, Date)
13. Day(): - It retrieves day from given date
Ex: - MsgBox Day (Date)
14. Month(): - It retrieves Month from given date
Ex: - MsgBox Month (Date)
15. MonthName(): - It retrieves the name of the month from given number from 1 to 12
Ex: - MsgBox MonthName (Date)
16. Year(): - It retrieves Year from given date
Ex: - MsgBox Year (Date)
17. WeekDay(): - It retrieves Weekday from given date
Ex: - MsgBox WeekDay(Date)
18. WeekDayName(): - It retrieves WeekDayName from given number(1-7)
Ex: - MsgBox WeekDayName (1) ‘Returns Sunday
MsgBox WeekDayName (WeekDay (Date)) ‘Returns current WeekDayName
Boolean Functions: -
19. IsDate (): -It retrieves Boolean value for given expression is date or not. It returns True if given value is date
else false
Ex: - MsgBox IsDate (“Suresh”) ‘Returns False
MsgBox IsDate (“3-01-2013”) ‘Returns True
20. IsEmpty (): -It retrieves Boolean value for given value is Empty or not. It returns True if given value is Empty
else false
Ex: - Dim val1, val2, val3
Val1 = 10
Val2 = Null
MsgBox IsEmpty(Val1) ‘Returns False
MsgBox IsEmpty(Val2) ‘Returns False
MsgBox IsEmpty(Val3) ‘Returns True
21. IsNull (): -It retrieves Boolean value for given value is Null or not. It returns True if given value is Null else
false
22. IsObject (): - It retrieves Boolean value for given expression is Object or not. It returns True if given value is
Object else false
Ex: - Dim Val1, Obj
Val1 = 10
Set obj = CreateObject (WScript. Shell)
MsgBox IsObject (Val1) ‘Returns False
MsgBox IsObject (Obj) ‘Returns True
23. IsNumeric (): - It retrieves Boolean value for given expression is Numeric or not. It returns True if given value
is Numeric else false
Ex: - Dim val1, val2, val3
Val1 = 10
Val2 = 13.45
Val3= “suresh”
MsgBox IsNumeric (Val1) ‘Returns True
MsgBox IsNumeric (Val2) ‘Returns True
MsgBox IsNumeric (Val3) ‘Returns False
String Functions
24. Left (): - It returns number of characters from left side of a given string
Ex: - sString = “My Name is Suresh”
MsgBox Left (sString, 4) ‘Returns My N
25. Right (): - It returns number of characters from right side of a given string
Ex: - sString = “My Name is Suresh”
MsgBox Right (sString, 4) ‘Returns resh
26. Mid (): -*** It returns a substring from a given String
System: - MsgBox Mid (String, start position [, Number of characters])
Ex: - Ex: - sString = “My Name is Suresh”
MsgBox Mid (sString, 4, 7)
MsgBox Mid (sString, 4)
MsgBox Mid (sString, 100)
MsgBox Mid (sString, 100, 1)
sString = "Rs:1000/-"
MsgBox Mid (sString, 4, 7) ' Returns 1000/-
MsgBox Mid (sString, 4, 4) ' Returns 1000
'To get any number
MsgBox Mid (sString, 4, Len (sString)-5) ' Returns 1000
27. Len(): - ***It returns number of characters from given string
Ex: - sString = “My Name is Suresh”
MsgBox Len (SString)
28. Replace(): - This function is used to replace a text with another text in given text
Syntax: - Replace (String, replacing text, new text)
Ex: - sString = "My name is suresh"
MsgBox Replace (sString, "suresh", "babu")
MsgBox Replace (sString, "is", "")
29. LTrim(): - It removes all spaces from Left side of a given string [Ex: - MsgBox LTrim(sString)]
30. RTrim(): - It removes all spaces from Right side of a given string [Ex: - MsgBox RTrim(sString)]
31. Trim(): - It removes all spaces from Left and Right side of a given string
32. LCase(): - It converts given text in lower case letters
33. UCase(): - It converts given text in upper case letters
Ex: - MsgBox LCase (“My Name Is SURESH”)
MsgBox UCase (“My Name Is SURESH”)
34. StrReverse(): - ***It displays / retrieves a reverse text for given string/ text
sString= “suresh”
MsgBox StrReverse (sString)
Without using StrReverse:-
Dim sString
sString = “suresh”
For index = Len (sString) To 1 Step -1
str = str & Mid(sString, index,1)
Next
Print str
35. InStr() and InStrRev(): - It retrieves the first occurrence position of the given text in actual text
sString = “My name is suresh your name is babu”
MsgBox InStr (sString,”suresh”) ‘Returns the position of‘s’ from suresh
MsgBox InStr (sString,”name”) ‘Returns 4 first occurrence of name
MsgBox InStr (sString,”babu”) ‘Returns 0
MsgBox InStrRev (sString,”name”) ‘Returns 24 Last occurrence of name
MsgBox InStr (14, sString,”babu”) ‘Returns 0: suresh is not found from 14th position
36. StrCompp(): - It is used to compare two given strings
37. String(): - It is used to display the string in number of times
Ex: -MsgBox String (5, *) ‘Returns *****
38. TypeName(): -It returns the data type for the given variable
Ex: - Dim Val
Val = 10
MsgBox Val 'Returns 10
MsgBox TypeName (Val) 'Returns Integer
Val = 10.05
MsgBox Val 'Returns 10
MsgBox TypeName (Val) 'Returns Double
Val = "Suresh"
MsgBox Val 'Returns 10
MsgBox TypeName (Val) 'Returns String
39. Split (): - It is used to split the text with given delimiters
Scripts
15. Write a script to display * as bellow with respect to given value
Option Explicit
Dim iVal, sString,index,index1
sString=""
iVal = 5 ' InputBox ("Enter a Number")
For index = 1 To iVal
For index1 = 1 To index
sString = sString & " * "
Next
Print sString
sString=""
Next
16. Write a script to display * as bellow with respect to given value
17. Write a script to display * as bellow with respect to given value
9 programs to write for this
1) Pro1
Option Explicit
Dim iVal, sString,index,index1
sString=""
iVal = 5 ' InputBox ("Enter a Number")
For index = iVal To 1 Step-1
For index1 = 1 To index
sString = sString & " * "
Next
Print sString
sString=""
Next
2) Pro 2
Option Explicit
Dim iVal, sStar,index,index1,iSpace
iVal = 5
For index = iVal To 1 Step-1
For index1 = 1 To iSpace
sStar= sStar & " "
Next
For index1 = 1 To index
sStar = sStar & "*"
Next
Print sStar
sStar ="" ' Removing the value after print
iSpace= iSpace + 1' Increasing the spaces for next iteration
Next
'#############################################################################################
'######### Program to write a character in a particular pattern using VBS #########################
'#############################################################################################
'Declaring variables
Dim MainCounter,TempCounter,sStr,sTempStr,sMainStr,PatternChar
'Initializing variables
PatternChar = "*"
sStr = ""
'Reading from Input and initializing
MainCounter = InputBox("Enter any number")
If MainCounter = "" Then
MainCounter = 9
End If
MainCounter = Abs(MainCounter)
TempCounter = MainCounter
'Looping twice the supplied value to draw the pattern and reverse it
For MainIndex = 1 to MainCounter * 2
'Increase the tempCounter for drawing the pattern and
'Decrease the tempcounter to draw the pattern in reverse order
If Abs(MainIndex) <> 1 and Abs(MainIndex) <> MainCounter + 1 Then
If MainIndex <= MainCounter Then
TempCounter = Abs(TempCounter) - 1
Else
TempCounter = Abs(TempCounter) + 1
End If
End If
sTempStr = ""
iSpace = ""
'Looping to Create the pattern for a single row
For indx = 1 to TempCounter
sTempStr = sTempStr & PatternChar
Next
'manipulating space for a row
iSpace = Space((Abs(MainCounter)-Abs(TempCounter)) * 2)
'Adding the space for a single row
sStr = sStr & sTempStr & iSpace & sTempStr & vbNewline
Next
'sStr= sStr & vbNewline & StrReverse(sStr)
'MsgBox sStr
'Writing the pattern to a txt file in D drive
Dim filesys, testfile
Set filesys = CreateObject("Scripting.FileSystemObject")
Set testfile= filesys.CreateTextFile("d:\PatternDrawnFromVbs.txt", True)
testfile.WriteLine sStr
testfile.Close
Example: - write a script to retrieve India in given string “I love India”
sVal = "i love InDiA"
sVal = Split(s," ")
MsgBox sVal(UBound(s))
40. Arrays
Arrays are used to store multiple values in a variable
Declaration: -
1> Static Array declaration
Dim sName (3)
2> Dynamic Array declaration
Dim sName ()
Scripts
9. Write a script to find the SUM of odd number up to given number
10. Write a script to find the SUM of even number up to given number
11. Write a script to find given Text/ Numbers is palindrome or not [madam reverse madam]
sString = UCase(InputBox("Enter the String/Number:"))
rString = StrReverse(sString)
If StrComp (sString,rString) = 0 Then
MsgBox “It is a Palindrome"
Else
MsgBox “It is Not a Palindrome"
End If
12. Write a script to find Factorial of given number [5*4*3*2*1=120]
Option Explicit
Dim iVal1 ,iVal2, iFact
iVal1= Int (InputBox ("Enter a Number :"))
iFact = 1
iVal2=1
Do while iVal2 <=iVal1
iFact = iFact * iVal2
iVal2=iVal2+1
Loop
Print iFact
15. Write a script to display * as bellow with respect to given value
Option Explicit
Dim iVal, sString,index,index1
sString=""
iVal = 5 ' InputBox ("Enter a Number")
For index = 1 To iVal
For index1 = 1 To index
sString = sString & " * "
Next
Print sString
sString=""
Next
16. Write a script to display * as bellow with respect to given value
17. Write a script to display * as bellow with respect to given value
9 programs to write for this
1) Pro1
Option Explicit
Dim iVal, sString,index,index1
sString=""
iVal = 5 ' InputBox ("Enter a Number")
For index = iVal To 1 Step-1
For index1 = 1 To index
sString = sString & " * "
Next
Print sString
sString=""
Next
2) Pro 2
Option Explicit
Dim iVal, sStar,index,index1,iSpace
iVal = 5
For index = iVal To 1 Step-1
For index1 = 1 To iSpace
sStar= sStar & " "
Next
For index1 = 1 To index
sStar = sStar & "*"
Next
Print sStar
sStar ="" ' Removing the value after print
iSpace= iSpace + 1' Increasing the spaces for next iteration
Next
'#############################################################################################
'######### Program to write a character in a particular pattern using VBS #########################
'#############################################################################################
'Declaring variables
Dim MainCounter,TempCounter,sStr,sTempStr,sMainStr,PatternChar
'Initializing variables
PatternChar = "*"
sStr = ""
'Reading from Input and initializing
MainCounter = InputBox("Enter any number")
If MainCounter = "" Then
MainCounter = 9
End If
MainCounter = Abs(MainCounter)
TempCounter = MainCounter
'Looping twice the supplied value to draw the pattern and reverse it
For MainIndex = 1 to MainCounter * 2
'Increase the tempCounter for drawing the pattern and
'Decrease the tempcounter to draw the pattern in reverse order
If Abs(MainIndex) <> 1 and Abs(MainIndex) <> MainCounter + 1 Then
If MainIndex <= MainCounter Then
TempCounter = Abs(TempCounter) - 1
Else
TempCounter = Abs(TempCounter) + 1
End If
End If
sTempStr = ""
iSpace = ""
'Looping to Create the pattern for a single row
For indx = 1 to TempCounter
sTempStr = sTempStr & PatternChar
Next
'manipulating space for a row
iSpace = Space((Abs(MainCounter)-Abs(TempCounter)) * 2)
'Adding the space for a single row
sStr = sStr & sTempStr & iSpace & sTempStr & vbNewline
Next
'sStr= sStr & vbNewline & StrReverse(sStr)
'MsgBox sStr
'Writing the pattern to a txt file in D drive
Dim filesys, testfile
Set filesys = CreateObject("Scripting.FileSystemObject")
Set testfile= filesys.CreateTextFile("d:\PatternDrawnFromVbs.txt", True)
testfile.WriteLine sStr
testfile.Close
Example: - write a script to retrieve India in given string “I love India”
sVal = "i love InDiA"
sVal = Split(s," ")
MsgBox sVal(UBound(s))
Application (Window, VB, Java, Dot Net, Web) Script
Application Script: -
Script 1: -Write a script to enter student details
Note: - Before writing a VBScript with respect to application we need to know bellow 5 information’s
1. Object Name
2. Object Class Name
3. Object Parent Object Name
4. Object Parent Object Class name
5. Method Name. To perform operation
Note: - To know about the object use Object SPY in TOOL MENU Object SPY
For Web Application
iHwnd = Browser("Gmail")GetROProperty("hwnd") ‘ first 2 lines to activate the browser
Window("hwnd:="&iHwnd).Activate
Browser("Student").Page("Student").WebEdit("").Set "Sreekar"
Browser("Student").Page("Student").WebEdit("Course").Set "QTP"
Browser("Student").Page("Student").WebButton("Submit").Click
**** End of Chapter III ****




No comments :
Post a Comment