Top Banner
VB SCRIPT
67
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: VB Script

VB SCRIPT

Page 2: 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

Page 3: VB Script

Scripting Language V/S Programming Language

Scripting Language Programming Language

Client Side Scripting Server Side Scripting

Interpreter Compiler

Client side validation Server side validation

Page 4: VB Script

Execution Environments & Editors• Windows Script Host (WSH)• Notepad• Third Party Editor

Page 5: VB Script

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

Page 6: VB Script

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

Page 7: VB Script

Exercise• Option explicit• Dim a• a=10• Msgbox a

• Option explicit• Dim a• a=10• b=5• Msgbox a

Page 8: VB Script

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

Page 9: VB Script

Sub types of variant• Integer• Long• Single• Double• String• Boolean• Empty• Null• Error• Nothing

Page 10: VB Script

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#

Page 11: VB Script

Exercise

Dim aa=10  'IntegerMsgbox  typename(a)

Dim aMsgbox typename(a)Msgbox a    'Returns Empty

Page 12: VB Script

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

Page 13: VB Script

Operators• Arithmetic operators

• + , - , * ,/, \,mode ,^

• Relational operators ( Always returns Boolean values)• >, <, <=, >=, =, <>

• Logical operators ( Always returns Boolean values)• AND , OR, NOT

Page 14: VB Script

Exercise• Dim a,b• a=5• b= 2• Msgbox a/b Returns 2.5• Msgbox a\b Returns 2• Msgbox a>b Returns True

Page 15: VB Script

Miscellanies operators• Miscellanies operators

• & is used for concatenation• Vbnewline is for newline• Inputbox retrieves the input from user, returns string value.

Page 16: VB Script

Exercise• Dim a,b• a=“online”• b=“training”• Msgbox a&b• Msgbox a&” “&b• Msgbox a&vbnewline&b

Page 17: VB Script

• Dim a• a= inputbox (“Enter value”)• Msgbox a• Msgbox typename(a) Returns string

Page 18: VB Script

Arrays• Types of arrays

• One dimensional• Two dimensional

Page 19: VB Script

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

Page 20: VB Script

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

Page 21: VB Script

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

Page 22: VB Script

Dynamic Arrays• In ONE and TWO dimensional arrays we cant increase or

decrease elements at run time. It is possible through Dynamic Arrays.

Page 23: VB Script

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)

Page 24: VB Script

Conditional Statements• Simple If• If ..Else• Nested If• Select Case

Page 25: VB Script

If condition• Dim age• Age=Cint(Inputbox(“Enter age:”))• If age>18 then• Msgbox “Eligible to vote”

• End if

Page 26: VB Script

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

Page 27: VB Script

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

Page 28: VB Script

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

Page 29: VB Script

Looping Statements• While..Wend• For..Next• Do..While• Do..Loop While• Do..Loop Until

Page 30: VB Script

While..Wend• Dim I• i=1• While i<=10• Msgbox I• i=i+1• Wend

Page 31: VB Script

For..Next• Dim i• For i=1 to 10• Msgbox I• Next

• Dim i• For i=1 to 10 step 2• Msgbox i• Next

Page 32: VB Script

Do..While• Dim I• i=1• Do while i<=5• Msgbox I• i=i+1• Loop

Page 33: VB Script

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

Page 34: VB Script

Built-in Functions• Conversion functions• Verification functions• Date functions• String functions

Page 35: VB Script

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

Page 36: VB Script

CStr• Dim a• a=55• Msgbox typename(a) ‘ Integer• b=CStr(a)• Msgbox typename(b) ‘ String

Page 37: VB Script

• Dim a• a= “true”• Msgbox typename(a) ‘String• B=Cbool(a)• Msgbox typename(b) ‘ Boolean

• ‘a’ value can be string or integer

CBool

Page 38: VB Script

• 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)

Page 39: VB Script

• Dim a• a=“10/07/14”• Msgbox typename(a) ‘ String• B= Cdate(a)• Msgbox typename(b) ‘ Date• Msgbox b ‘ Returns 10/07/2014

CDate

Page 40: VB Script

• Dim a• a=10.567 ‘ Double• Msgbox Typename(a) ‘ Double• Msgbox Cint(a) ‘ Returns Integer

CInt

Page 41: VB Script

CDbl• Dim a, b

• a="123.88"• msgbox typename(a)

• b=Cdbl(a) • Msgbox typename(b)

Page 42: VB Script

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

Page 43: VB Script

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.

Page 44: VB Script

• Dim a• A=“55xyz” ‘ String• Msgbox IsNumeric(a) ‘ Returns False

Page 45: VB Script

IsEmpty & IsNull• Dim a• Msgbox IsEmpty(a) ‘ Returns True• Msgbox IsNull(a) ‘ False

• Dim a=Null• Msgbox IsNull(a) ‘ True

Page 46: VB Script

IsArray• Dim a• Msgbox IsAarray(a) ‘ False

• Dim a(2)• Msgbox IsArray(a) ‘ True

Page 47: VB Script

Date Functions• 1) Date Returns current system date• 2) Time Returns current system time• 3) Now Returns both system date and time

Page 48: VB Script

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.

Page 49: VB Script

Exercise• Msgbox Date • Msgbox Day(Date)• Msgbox Month(Date)• Msgbox Year(Date)

Page 50: VB Script

Time• Hour(Time) Extracts hour of time• Minute(Time) Extracts minutes of time• Second(Time) Extracts seconds from time

Page 51: VB Script

Exercise• Msgbox Time• Msgbox Hour(Time)• Msgbox Minutes(Time)• Msgbox Seconds(Time)

Page 52: VB Script

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

Page 53: VB Script

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

Page 54: VB Script

• 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.

Page 55: VB Script

• 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)

Page 56: VB Script

• 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.

Page 57: VB Script

• Dim str1, str2• Str1=“A”• Str2=“a”• Msgbox Strcomp(str1,str2) Compared ASCII values

• Str1=“Apple”• Str2=“Orange”• Msgbox Strcomp(Str1, Str2)

Page 58: VB Script

• 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

Page 59: VB Script

• Dim str, i• Str =“This is sample script”• a=Split(str, “ “)• Msgbox IsArray(a)• For each i in a• Msgbox I• Next

Page 60: VB Script
Page 61: VB Script

Procedures• Sub procedure

• Does not return value

• Function procedure(UDF- User Defined Function)• Returns some value

Page 62: VB Script

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

Page 63: VB Script

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

Page 64: VB Script

Argument Types• By Value• By Reference ( Default)

Page 65: VB Script

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

Page 66: VB Script

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

Page 67: VB Script

More Exercises

Microsoft Word Document

Microsoft Word Document