Transcript
VB SCRIPT
Introduction• VB Script is derived from Visual Basic( VB).• QTP/UFT Supports VB Script.• VB comes with Windows OS ( By default)• File extension is .vbs
Scripting Language V/S Programming Language
Scripting Language Programming Language
Client Side Scripting Server Side Scripting
Interpreter Compiler
Client side validation Server side validation
Execution Environments & Editors• Windows Script Host (WSH)• Notepad• Third Party Editor
Rules for variable names• First character should be an alphabet character.• Special characters are not allowed except _ (Under score)• Keywords are not allowed (Ex: break, if etc.)• Should not exceeds 255 characters
Types of declaration• 1) Implicit declaration
• When the variables are used without declaration
• 2) Explicit declaration• When the variables are used if declared first.• It is followed only when Option Explicit is written in the first line of
script.• If variable is not declared error will thrown
Exercise• Option explicit• Dim a• a=10• Msgbox a
• Option explicit• Dim a• a=10• b=5• Msgbox a
Data Types• There is only ONE data type of variable in VB Script
called ‘variant’.• DIM is a keyword used to declare a variable.
• Dim a here a is a variable of data type variant
Sub types of variant• Integer• Long• Single• Double• String• Boolean• Empty• Null• Error• Nothing
Date & Time representation• To represent a date, # symbol is used along with the valid
delimiters.• Ex:-
• #10/07/14#• #10-07-14#• #10,07,14#
• To represent a time, . ( dot) or : (colon) is used.• Ex:-
• #10:10:10#• #10.10.10#
Exercise
Dim aa=10 'IntegerMsgbox typename(a)
Dim aMsgbox typename(a)Msgbox a 'Returns Empty
Declarations
a= "online training" ‘stringa= #10/07/14# 'datea=#10%07%14# ' Wrong Declarationa=#10,07,14# 'datea=#10,10,10# ' time
a= Null ' Undefined valuemsgbox a ' cannot print
Operators• Arithmetic operators
• + , - , * ,/, \,mode ,^
• Relational operators ( Always returns Boolean values)• >, <, <=, >=, =, <>
• Logical operators ( Always returns Boolean values)• AND , OR, NOT
Exercise• Dim a,b• a=5• b= 2• Msgbox a/b Returns 2.5• Msgbox a\b Returns 2• Msgbox a>b Returns True
Miscellanies operators• Miscellanies operators
• & is used for concatenation• Vbnewline is for newline• Inputbox retrieves the input from user, returns string value.
Exercise• Dim a,b• a=“online”• b=“training”• Msgbox a&b• Msgbox a&” “&b• Msgbox a&vbnewline&b
• Dim a• a= inputbox (“Enter value”)• Msgbox a• Msgbox typename(a) Returns string
Arrays• Types of arrays
• One dimensional• Two dimensional
One dimensional Array• Ex:-
• Dim a(5) has 6 elements• Msgbox lbound(a) returns 0• Msgbox ubound(a) returns 5
• Array can have any type of data type
Exercise• Dim A(5)• A(0)=10• A(1)=“abc”• A(2)=TRUE• Msgbox a(0) 10• Msgbox a(1) abc• Msgbox a(2) TRUE
Two dimensional Array• Dim A(3,4)• Total number of elements=(3+1)*(4+1) =20
• Dim A(3,4)• A(2,2)=10• msgbox A(2,2) 10• Msgbox A(1,3) Empty
Dynamic Arrays• In ONE and TWO dimensional arrays we cant increase or
decrease elements at run time. It is possible through Dynamic Arrays.
Exercise• Redim A(2)• A(0)=1• A(1)="HI"• A(2) =4
• Redim preserve A(3)
• A(3)=77
• Msgbox A(3)• Msgbox A(2)
Conditional Statements• Simple If• If ..Else• Nested If• Select Case
If condition• Dim age• Age=Cint(Inputbox(“Enter age:”))• If age>18 then• Msgbox “Eligible to vote”
• End if
If…Else condition• Dim age• Age=Cint(Inputbox(“Enter age:”))• If age>18 then• Msgbox “Eligible to vote”
• Else• Msgbox “Not Eligible to vote”
• End if
Nested If Else• Dim marks• Marks=Cint(inputbox(“Enter marks”:))• If marks <33 then
• Msgbox “ failed”
• Else if marks>=33 and marks<50 then• Msgbox “passed”
• Else if marks>=50 and marks<60 then• Msgbox “second division”
• Else if marks>60 then• Msgbox “First division”
• Else• Msgbox” Invali input”
• End if
Select Case• Dim color• Color=inputbox(“Enter color:”)• Select case color• Case “red”, “RED”
• Msgbox “STOP”
• Case “yellow”• Msgbox “READY”
• Case “green”• Msgbox “GO”
• Case else• Msgbox “ Wrong input”
• End Select
Looping Statements• While..Wend• For..Next• Do..While• Do..Loop While• Do..Loop Until
While..Wend• Dim I• i=1• While i<=10• Msgbox I• i=i+1• Wend
For..Next• Dim i• For i=1 to 10• Msgbox I• Next
• Dim i• For i=1 to 10 step 2• Msgbox i• Next
Do..While• Dim I• i=1• Do while i<=5• Msgbox I• i=i+1• Loop
Do..Loop While• Dim I• i=1• Do• Msgbox I• i=i+1• Loop while i<=5
• Dim I• i=6• Do• Msgbox I• i=i+1• Loop while i<=5
Built-in Functions• Conversion functions• Verification functions• Date functions• String functions
Conversion functions• 1) CStr Converts any data to String.• 2) Cbool Converts any data to Boolean.• 3) Cdate Converts any data to Date.• 4) Cint Converts any data to Integer• 5) Cdbl Converts any data to Double
CStr• Dim a• a=55• Msgbox typename(a) ‘ Integer• b=CStr(a)• Msgbox typename(b) ‘ String
• Dim a• a= “true”• Msgbox typename(a) ‘String• B=Cbool(a)• Msgbox typename(b) ‘ Boolean
• ‘a’ value can be string or integer
CBool
• Dim a• a=0 ‘ Integer• Msgbox typename(a) ‘ Integer• B=Cbool(a)• Msgbox typename(b) ‘ Boolean• Msgbox b ‘ Returns false
• ** only 0 gives false , any other number gives true(even it is negative)
• Dim a• a=“10/07/14”• Msgbox typename(a) ‘ String• B= Cdate(a)• Msgbox typename(b) ‘ Date• Msgbox b ‘ Returns 10/07/2014
CDate
• Dim a• a=10.567 ‘ Double• Msgbox Typename(a) ‘ Double• Msgbox Cint(a) ‘ Returns Integer
CInt
CDbl• Dim a, b
• a="123.88"• msgbox typename(a)
• b=Cdbl(a) • Msgbox typename(b)
Verification Functions• 1) IsNumeric Checks if data is numeric• 2) IsEmpty Checks if variable is Empty or not• 3) IsNull Checks if variable contains Null• 4) IsAarray Checks if variable is an array or not
IsNumeric• Dim a• a=“55” ‘ String• Msgbox typename(a) ‘String• Msgbox IsNumeric(a) ‘ Returns True
• ** Returns TRUE even the data is string , because it consider internal value.
• Dim a• A=“55xyz” ‘ String• Msgbox IsNumeric(a) ‘ Returns False
IsEmpty & IsNull• Dim a• Msgbox IsEmpty(a) ‘ Returns True• Msgbox IsNull(a) ‘ False
• Dim a=Null• Msgbox IsNull(a) ‘ True
IsArray• Dim a• Msgbox IsAarray(a) ‘ False
• Dim a(2)• Msgbox IsArray(a) ‘ True
Date Functions• 1) Date Returns current system date• 2) Time Returns current system time• 3) Now Returns both system date and time
Date• Day(Date) Extracts the day from the date passed.• Month(Date) Extracts the month name from the date
passed.• Year(Date) Extracts the year from the date passed.
Exercise• Msgbox Date • Msgbox Day(Date)• Msgbox Month(Date)• Msgbox Year(Date)
Time• Hour(Time) Extracts hour of time• Minute(Time) Extracts minutes of time• Second(Time) Extracts seconds from time
Exercise• Msgbox Time• Msgbox Hour(Time)• Msgbox Minutes(Time)• Msgbox Seconds(Time)
String Functions• 1) Len Returns length of the string• 2) Lcase Converts the string into lower case• 3) Ucase Converts string into upper case• 4) ASC Returns ASCI code of the character• 5) Chr Returns equivalent character of ASCII
code(Reverse of ASC)• 6) StrReverse Reverse the string• 7) Space Returns the number of spaces
Exercise• Dim str• Str=“example”• Msgbox Len(str)• Msgbox Lcase(“EXAMPLE”)• Msgbox Ucase(“example)• Msgbox ASC(“a”)• Msgbox Chr(97)• Msgbox Strreverse(str)• Msgbox “online”&space(5)&”training” ‘ 5 spaces added• Msgbox “online” & vbtab&”training” ‘ one tab space
• 8) Right Extracts no.of characters from right side• 9) Left Extract no.of characters from left side• 10) Ltrim Delete all space• 11) Rtrim Delete all spaces from right side• 12) Trim Delete all spaces for the both left and right
sides.
• Dim str• Str=“ good day”• Msgbox Left(str,4)• Msgbox Right(str,3)
• Dim str• str=“ example “• Msgbox str• Msgbox Ltrim(str)• Msgbox Rtrim(str)• Msgbox Trim(str)
• 13) StrComp Compares two strings• Returns 1 if str1>str2• Returns -1 if str1<str2• Returns 0 if str1=str2
• 14) Replace Replaces a substring with some other string
• 15) Split -> Split a string based on a delimiter.
• Dim str1, str2• Str1=“A”• Str2=“a”• Msgbox Strcomp(str1,str2) Compared ASCII values
• Str1=“Apple”• Str2=“Orange”• Msgbox Strcomp(Str1, Str2)
• Dim str• Str=“example”• Msgbox Str• Msgbox Replace(Str,”m”,”M”) ‘ exaMple
• Msgbox Replace(Str,”q”,”M”) ‘ Retuns original string bcoz q is not exists in string
• Msgbox Replace(Str,”m”,” ”) ‘ Deletes m in string• Msgbox Replace(Str,”e”,” ”,1,1) ‘ Multiple places
• Dim str, i• Str =“This is sample script”• a=Split(str, “ “)• Msgbox IsArray(a)• For each i in a• Msgbox I• Next
Procedures• Sub procedure
• Does not return value
• Function procedure(UDF- User Defined Function)• Returns some value
Sub procedure• Dim val1,val2• Val1=cint(inputbox(“ enter value1”))• Val2=cin(inputbox(“ entervalue2”))• Avrg val1,val2 (or) call avrg(val1,val2)
• Sub avrg(a,b)• Msgbox (a+b)/2• End sub
Function Procedure• Dim val1,val2• Val1=cint(inputbox(“ enter value1”))• Val2=cin(inputbox(“ entervalue2”))• Av= avrg(val1,val2)• Msgbox av
• Function avrg(a,b)• Avrg= (a+b)/2• End Function
Argument Types• By Value• By Reference ( Default)
By Value• Dim val1, val2• Val1=10• Val2=20• Msgbox val1 Returns 10• Msgbox val2 Returns 20• Example val1,val2• Msgbox val1 Returns 10• Msgbox val2 Returns 20
• Sub Example(byval a, byval b)• A=a+5• B=b+5
• Msgbox a Returns 15• Msgbox b Returns 25
• End Sub
By Reference• Dim val1, val2• Val1=10• Val2=20• Msgbox val1 Returns 10• Msgbox val2 Returns 20• Example val1,val2• Msgbox val1 Returns 15• Msgbox val2 Returns 25
• Function Example(byref a, byref b)• A=a+5• B=b+5
• Msgbox a Returns 15• Msgbox b Returns 25
• End Function
More Exercises
Microsoft Word Document
Microsoft Word Document
top related