VBScript Tutorial 10
(Error Handling, Debugging Scripts)
Error Handling using VBScript:
Handling expected as well as unexpected Errors
> Expected Errors:
Whenever nosotros utilization Invalid input as well as then nosotros tin await Errors.
Note: We utilization Invalid input for Negative Testing.
> Unexpected Errors
i) Resource missing
ii) Insufficient Resource
iii) Resource Response
-----------------------------------
How to Handle Errors:
i) Using Conditional Statements
ii) Using to a greater extent than or less built inward Functions
iii) Using Exit Statement
iv) Using Option Explicit Statement
v) On Error Statement
Etc...
-----------------------------------------
i) Using Conditional Statements
If Not Dialog("Login").Exist(3) Then
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
End If
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "asdf"
Dialog("Login").WinEdit("Password:").SetSecure "556fa5806c0b76ea4259c8e27e7c9af850b72f13"
Dialog("Login").WinButton("OK").Click
--------------------------------------------------
'Working alongside multiple event of Application
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
Dialog("text:=Login", "index:=0").Activate
Dialog("text:=Login", "index:=0").WinEdit("attached text:=Agent Name:").Set "asdf"
Dialog("text:=Login", "index:=0").WinEdit("attached text:=Password:").SetSecure "556fa5806c0b76ea4259c8e27e7c9af850b72f13"
Dialog("text:=Login", "index:=0").WinButton("text:=OK").Click
--------------------------------------------
'Working alongside multiple instantce of Application
Dialog("text:=Login", "index:=0").Activate
Dialog("text:=Login", "index:=0").WinEdit("attached text:=Agent Name:").Set "Hyderabad"
---------------------------------
Note: Latest Application event index is 0, oldest i index is n-1.
Browser("CreationTime:=1").close
We tin piece of employment alongside multiple browsers using "CreationTime" property, latest browser creation fourth dimension is n-1.
-----------------------------------------------------
ii) Using VBScript Built inward Functions
Examples:
Dim num1, num2
num1 = InputBox("Enter Num1 value")
num2 = InputBox("Enter Num2 value")
Msgbox "Addition of num1, num2 is: "& Cint (num1) + Cint (num2)
-------------------------------------
Dim num1, num2
num1 = Cint (InputBox("Enter Num1 value"))
num2 = Cint (InputBox("Enter Num2 value"))
Msgbox "Addition of num1, num2 is: "& num1 + num2
---------------------------------------------------
Dim a, b
a = "abc"
b = "xyz"
If a > b Then
Msgbox "A is a Big Number"
Else
Msgbox "B is a Big Number"
-----------------------------------
Dim a, b
a = InputBox("Enter a Value")
b = InputBox("Enter b Value")
If IsNumeric(a) = True And IsNumeric(b) Then
If Cint (a) > Cint (b) Then
Msgbox "A is a Big Number"
Else
Msgbox "B is a Big Number"
End if
Else
Msgbox "Invalid Input"
End If
------------------------------------
'Verify Data size as well as Data Type, display alone 10 digit numeric value.
Dim MobileNumber
MobileNumber = InputBox("Enter Mobile Number")
If Len(MobileNumber) = 10 And IsNumeric(MobileNumber) = True Then
Msgbox MobileNumber
Else
Msgbox "Invalid Input"
End If
--------------------------------------------
'Check Data Range as well as induce got the value is inward betwixt 21 to 35 only
Dim age
age = InputBox("Enter Age")
If IsNumeric (age) = True Then
If Cint(age) >= 21 And Cint(age) <= 35 Then
Msgbox "Accepted"
Else
Msgbox "Not Accepted"
End If
Else
Msgbox "Invalid Type of Data"
End If
---------------------------------------------
iii) Using Exit Statement
It is used to Terminate Loops.
Ex:
Exit For
Exit Do
------------------------------------------
' Terminate For loop
For OrderNumer = twenty To thirty Step 1
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set OrderNumer
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
If Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").Exist(3) Then
Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").WinButton("OK").Click
Window("Flight Reservation").Dialog("Open Order").WinButton("Cancel").Click
Window("Flight Reservation").WinButton("Button_2").Click
Reporter.ReportEvent micWarning, "Res", "Up to "& OrderNumer-1 &" Orders alone Exist"
Exit For
End If
Next
-----------------------------------
' Terminate Do loop
OrderNumer = 20
Do Until OrderNumer > thirty
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set OrderNumer
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
If Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").Exist(3) Then
Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").WinButton("OK").Click
Window("Flight Reservation").Dialog("Open Order").WinButton("Cancel").Click
Window("Flight Reservation").WinButton("Button_2").Click
Reporter.ReportEvent micWarning, "Res", "Up to "& OrderNumer-1 &" Orders alone Exist"
Exit Do
End If
OrderNumer= OrderNumer + 1
Loop
-----------------------------------
iv) Using Option Explicit Statement
Option Explicit - It forces annunciation of all variables inward a Script, therefore that nosotros avoid miss-spell problem.
Example:
Option Explicit
Dim Tickets, Price, Total
Tickets = 8
Price = 100
Total = Tickets * Priee
Msgbox Total
---------------------------------------------------
V) Using "ON Error Resume Next" statement
If whatever Error is there, don't halt the Script execution, skip the mistake as well as continue.
> When to Choose:
If at that spot is no touching on on terminal output as well as then nosotros tin utilization it.
Ex:
On Error Resume Next
VbWindow("Form1").Activate
VbWindow("Form1").VbComboBox("Combo1").Select "Chennai"
VbWindow("Form1").VbComboBox("Combo1").Select "Goa"
VbWindow("Form1").VbComboBox("Combo1").Select "Hyderabad"
VbWindow("Form1").VbComboBox("Combo1").Select "New Delhi"
VbWindow("Form1").VbComboBox("Combo1").Select "Mumbai"
> When Not to Choose:
If whatever touching on on terminal output as well as then don't utilization it.
'On Error Resume Next
Dim a, b
a = 100
b - 50
Msgbox a+b
----------------------------------
Debugging Scripts
i) What is Debugging?
Locating as well as isolating Errors through Step yesteryear Step execution is called Debugging.
Note: Debugging Scripts is Optional.
ii) When Debugging is Required?
Scenario 1:
Script is non showing errors as well as providing right Output - Debugging is non required.
Scenario 2:
Script is showing errors- Debugging is optional
Note: First right the errors as well as then almost of fourth dimension nosotros tin perish right output, otherwise
we tin debug the script.
Scenario 3:
Script is non showing errors as well as non providing right Output - Debugging is required.
-----------------------------
Whenever Script is non showing whatever errors as well as non providing right output as well as then
Debugging is required.
iii) How to Debug Scripts?
Using VBScript debug commands as well as breakpoints nosotros tin debug scripts.
Important Debug Commands:
a) Step Into (F11)
1) It starts the Script Execution.
2) It executes i contention at a time.
3) If is percentage call, opens the percentage as well as executes i percentage contention at a time.
b) Step Over (F10)
1) It executes i contention at a time.
2) If it is percentage call, it executes all percentage statements at a time.
Note: Once nosotros opened upwards the percentage using "Step Into" ascendence as well as then "Step Over" executes
one statement(function) at a fourth dimension only.
c) Step Out (Shift + F11)
1) It executes all remaining percentage statements at a Time.
------------------
Breakpoints (F9)
To intermission the Script execution.
It is for Hybrid execution.
Script Execution:
a) At a Time execution (UFT Run Command)
b) Step yesteryear Execution (VBScript debug commands)
c) Hybrid Execution (Using UFT Run command, VBScript Debug commands as well as Breakpoins)
Note: Debug commands as well as Breakpoint available inward "Run" card (UFT tool).
-------------------------------
Example:
Dim a, b, c
a = 10
b = 20
c = a+b
a = 5
b = 7
c = a*b
Call Login("gcreddy", "mercury")
a = 20
b = 3
c = a^b
a = 5
b = 7
c = a-b
a =100
-----------------------------------
(Error Handling, Debugging Scripts)
Error Handling using VBScript:
Handling expected as well as unexpected Errors
> Expected Errors:
Whenever nosotros utilization Invalid input as well as then nosotros tin await Errors.
Note: We utilization Invalid input for Negative Testing.
> Unexpected Errors
i) Resource missing
ii) Insufficient Resource
iii) Resource Response
-----------------------------------
How to Handle Errors:
i) Using Conditional Statements
ii) Using to a greater extent than or less built inward Functions
iii) Using Exit Statement
iv) Using Option Explicit Statement
v) On Error Statement
Etc...
-----------------------------------------
i) Using Conditional Statements
If Not Dialog("Login").Exist(3) Then
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
End If
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "asdf"
Dialog("Login").WinEdit("Password:").SetSecure "556fa5806c0b76ea4259c8e27e7c9af850b72f13"
Dialog("Login").WinButton("OK").Click
--------------------------------------------------
'Working alongside multiple event of Application
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
Dialog("text:=Login", "index:=0").Activate
Dialog("text:=Login", "index:=0").WinEdit("attached text:=Agent Name:").Set "asdf"
Dialog("text:=Login", "index:=0").WinEdit("attached text:=Password:").SetSecure "556fa5806c0b76ea4259c8e27e7c9af850b72f13"
Dialog("text:=Login", "index:=0").WinButton("text:=OK").Click
--------------------------------------------
'Working alongside multiple instantce of Application
Dialog("text:=Login", "index:=0").Activate
Dialog("text:=Login", "index:=0").WinEdit("attached text:=Agent Name:").Set "Hyderabad"
---------------------------------
Note: Latest Application event index is 0, oldest i index is n-1.
Browser("CreationTime:=1").close
We tin piece of employment alongside multiple browsers using "CreationTime" property, latest browser creation fourth dimension is n-1.
-----------------------------------------------------
ii) Using VBScript Built inward Functions
Examples:
Dim num1, num2
num1 = InputBox("Enter Num1 value")
num2 = InputBox("Enter Num2 value")
Msgbox "Addition of num1, num2 is: "& Cint (num1) + Cint (num2)
-------------------------------------
Dim num1, num2
num1 = Cint (InputBox("Enter Num1 value"))
num2 = Cint (InputBox("Enter Num2 value"))
Msgbox "Addition of num1, num2 is: "& num1 + num2
---------------------------------------------------
Dim a, b
a = "abc"
b = "xyz"
If a > b Then
Msgbox "A is a Big Number"
Else
Msgbox "B is a Big Number"
-----------------------------------
Dim a, b
a = InputBox("Enter a Value")
b = InputBox("Enter b Value")
If IsNumeric(a) = True And IsNumeric(b) Then
If Cint (a) > Cint (b) Then
Msgbox "A is a Big Number"
Else
Msgbox "B is a Big Number"
End if
Else
Msgbox "Invalid Input"
End If
------------------------------------
'Verify Data size as well as Data Type, display alone 10 digit numeric value.
Dim MobileNumber
MobileNumber = InputBox("Enter Mobile Number")
If Len(MobileNumber) = 10 And IsNumeric(MobileNumber) = True Then
Msgbox MobileNumber
Else
Msgbox "Invalid Input"
End If
--------------------------------------------
'Check Data Range as well as induce got the value is inward betwixt 21 to 35 only
Dim age
age = InputBox("Enter Age")
If IsNumeric (age) = True Then
If Cint(age) >= 21 And Cint(age) <= 35 Then
Msgbox "Accepted"
Else
Msgbox "Not Accepted"
End If
Else
Msgbox "Invalid Type of Data"
End If
---------------------------------------------
iii) Using Exit Statement
It is used to Terminate Loops.
Ex:
Exit For
Exit Do
------------------------------------------
' Terminate For loop
For OrderNumer = twenty To thirty Step 1
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set OrderNumer
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
If Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").Exist(3) Then
Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").WinButton("OK").Click
Window("Flight Reservation").Dialog("Open Order").WinButton("Cancel").Click
Window("Flight Reservation").WinButton("Button_2").Click
Reporter.ReportEvent micWarning, "Res", "Up to "& OrderNumer-1 &" Orders alone Exist"
Exit For
End If
Next
-----------------------------------
' Terminate Do loop
OrderNumer = 20
Do Until OrderNumer > thirty
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set OrderNumer
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
If Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").Exist(3) Then
Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").WinButton("OK").Click
Window("Flight Reservation").Dialog("Open Order").WinButton("Cancel").Click
Window("Flight Reservation").WinButton("Button_2").Click
Reporter.ReportEvent micWarning, "Res", "Up to "& OrderNumer-1 &" Orders alone Exist"
Exit Do
End If
OrderNumer= OrderNumer + 1
Loop
-----------------------------------
iv) Using Option Explicit Statement
Option Explicit - It forces annunciation of all variables inward a Script, therefore that nosotros avoid miss-spell problem.
Example:
Option Explicit
Dim Tickets, Price, Total
Tickets = 8
Price = 100
Total = Tickets * Priee
Msgbox Total
---------------------------------------------------
V) Using "ON Error Resume Next" statement
If whatever Error is there, don't halt the Script execution, skip the mistake as well as continue.
> When to Choose:
If at that spot is no touching on on terminal output as well as then nosotros tin utilization it.
Ex:
On Error Resume Next
VbWindow("Form1").Activate
VbWindow("Form1").VbComboBox("Combo1").Select "Chennai"
VbWindow("Form1").VbComboBox("Combo1").Select "Goa"
VbWindow("Form1").VbComboBox("Combo1").Select "Hyderabad"
VbWindow("Form1").VbComboBox("Combo1").Select "New Delhi"
VbWindow("Form1").VbComboBox("Combo1").Select "Mumbai"
> When Not to Choose:
If whatever touching on on terminal output as well as then don't utilization it.
'On Error Resume Next
Dim a, b
a = 100
b - 50
Msgbox a+b
----------------------------------
Debugging Scripts
i) What is Debugging?
Locating as well as isolating Errors through Step yesteryear Step execution is called Debugging.
Note: Debugging Scripts is Optional.
ii) When Debugging is Required?
Scenario 1:
Script is non showing errors as well as providing right Output - Debugging is non required.
Scenario 2:
Script is showing errors- Debugging is optional
Note: First right the errors as well as then almost of fourth dimension nosotros tin perish right output, otherwise
we tin debug the script.
Scenario 3:
Script is non showing errors as well as non providing right Output - Debugging is required.
-----------------------------
Whenever Script is non showing whatever errors as well as non providing right output as well as then
Debugging is required.
iii) How to Debug Scripts?
Using VBScript debug commands as well as breakpoints nosotros tin debug scripts.
Important Debug Commands:
a) Step Into (F11)
1) It starts the Script Execution.
2) It executes i contention at a time.
3) If is percentage call, opens the percentage as well as executes i percentage contention at a time.
b) Step Over (F10)
1) It executes i contention at a time.
2) If it is percentage call, it executes all percentage statements at a time.
Note: Once nosotros opened upwards the percentage using "Step Into" ascendence as well as then "Step Over" executes
one statement(function) at a fourth dimension only.
c) Step Out (Shift + F11)
1) It executes all remaining percentage statements at a Time.
------------------
Breakpoints (F9)
To intermission the Script execution.
It is for Hybrid execution.
Script Execution:
a) At a Time execution (UFT Run Command)
b) Step yesteryear Execution (VBScript debug commands)
c) Hybrid Execution (Using UFT Run command, VBScript Debug commands as well as Breakpoins)
Note: Debug commands as well as Breakpoint available inward "Run" card (UFT tool).
-------------------------------
Example:
Dim a, b, c
a = 10
b = 20
c = a+b
a = 5
b = 7
c = a*b
Call Login("gcreddy", "mercury")
a = 20
b = 3
c = a^b
a = 5
b = 7
c = a-b
a =100
-----------------------------------