VBScript Tutorial for beginners

VBScript Tutorial 2 
(Comments, Data Types and Variables Part-1)


i) VBScript Comments
Comments are English words; we use comments for Code documentation.

Purpose:

    To make the code readable
    To make the code disable from execution

Syntax:
-------------
Use ' symbol before the statement

Use Rem command followed by space

'Dialog("Login").WinEdit("Agent Name:").Set "asdf"
Dialog("Login").WinEdit("Password:").SetSecure "555d2f3be286cd5dc51229cfb136d5bf41a13691"
Dialog("Login").WinButton("OK").Click ' t4tyy y56u56u867 76578789
Rem Window("Flight Reservation").Close
--------------------------------------
Comment a block of statements

    Select Statements
    Use Shortcut key (Ctrl + M)
    Or
Select Statements
Edit menu -> Format -> Comment
---------------------------------
Uncomment

    select comment block
    Use Shortcut key (Ctrl + Shift + M)

Or
Select statements
Edit menu -> Format -> Uncomment
---------------------------------------
Usage of Comments in UFT:
---------------------------
a) To write Test headers

'***************************************************
'Test Name: Verify the Total in Flight Reservation

'Author: abcd

'Date of Creation: May 21st 2015

'Date of Modification: NA

'Pre-requisites:
'login.tsr, abcd.vbs etc...

'Test Flow:
'i) Launch the Application and Login.
'ii) Open an Order and capture "Tickets", "Price", and "Total" values
'iii) Convert the data and compare if the "Total" = "Tickets" * "Price" or not
'iv) Close the Application
'***************************************************
b) To write Function headers
'**********************************************
'Function Name: Login

'Author: abcd

'Date of Creation: May 21st 2015

'Date of Modification: NA

'Input: Agent Name and Password

'Output: FR window

'Purpose: Agent Login to FR Application
'**********************************************
c) To explain the complex logic

d) To explain the resources usage.

----------------------------------------
ii) VBScript Data Types
-------------------------------------
> VBScript doesn't support Explicit declaration of Data Types.

> In VBScript only Data type is Variant, It can hold any type of data, based on usage of data VBScript internally considers Data sub types.

Ex:

Dim city

city = "Hyderabad" 'String
---------
----------
----------
city = 100 'Integer
---------
----------
----------
city = 1.24 'Double
---------
----------
----------
city = #10/10/2015# 'Date
-----------------------------
> Check Data Sub types using VarType Function

> Convert the Data from one sub type to another.
----------------------------------------------------
Types of Results in Computer Programming:
-----------------------------------------
i) Value based Result
Ex:

Msgbox 5 + 3 ' 8
Msgbox 4 * 7 '28
-------------------------
ii) Boolean / Logical Result

Ex:
Msgbox IsNumeric("ABCD") 'False
Msgbox IsNumeric(100) 'True
---------------------------------
iii) Constant based Result

Msgbox VarType(100) '2 for Integer
-----------------------------------------------
Check Data Sub types using VarType Function
----------------------------------------
Dim a
Msgbox VarType(a) '0 for Empty / Uninitialized

a = 100
Msgbox VarType(a) '2 for Integer

Msgbox VarType(100) '2 for Integer

Msgbox VarType("abcd") '8 for String

Msgbox VarType("100") ' 8 for String

Msgbox VarType(1.24) ' 5 for Double

Msgbox VarType(#10/10/2010#) '7 for Date

Set a = CreateObject("Scripting.FileSystemObject")
Msgbox VarType(a) '9 for Automation Object
--------------------------------------------
Convert the Data from one sub type to another.
----------------------------------------------
Why we need to convert the Data?

In Computer Programming value Assignment 2 types:

i) Initialization

Ex:

a = 100
----------------------------
ii) Reading
    Read data using Input devices
    Read data from a file
    Read data from a database
    Read data from Application objects
Ex:
'Before Conversion
Dim a, b
a = InputBox("Enter A Value")
b = InputBox("Enter B Value")
Msgbox a + b
---------------------------------------
Note: If we read data then VBScript considers the data as String type data.
In order to perform math-metical calculations, we need to convert the data.

> Using conversion Functions we can convert the data.

'After Conversion
Dim a, b
a = InputBox("Enter A Value")
b = InputBox("Enter B Value")
Msgbox Cint (a) + Cint (b)
----------------------------------
'We can't convert Alfa bytes into Integer or double type.

Ex:
Dim val, Tickets
val = InputBox("Enter a value")
Msgbox VarType(val) '8 for String
val = Cint(val)
Msgbox VarType(val)

Tickets = Window("Flight Reservation").WinEdit("Tickets:").GetROProperty("text")
Msgbox VarType(Tickets) '8 for String
Msgbox VarType(Cint(Tickets)) ' 2 for Integer
------------------------------------------------------
iii) Variables

1) What is Variable?

A named memory location to store the data.

In computer environment there are two types of memory

i) Primary memory: RAM

ii) Secondary memory: HDD, CD-Rom, DVD, USB Drive etc...

> Variables store in Primary Memory (RAM).
---------------------------------------------------
2) Declaration of variables

> using either Public or Private or Dim statements, we can declare Variables.

Ex:

Dim a
Dim x, y, z
Or
Dim x
Dim y
Dim z
---------------------------
3) Implicit and Explicit variables.

Ex:
Dim a
a = 100 'Explicit
b = 200 'Implicit
Msgbox a + b
---------------------
Dim Tickets, Price, Total
Tickets = 7
Price = 100
Total = Tickets * Priee
Msgbox Total '0 (Incorrect output)
-----------
Option Explicit
Dim Tickets, Price, Total
Tickets = 7
Price = 100
Total = Tickets * Priee
Msgbox Total 'Error
---------------------------
Option Explicit - It forces declaration of all variables in a Script
---------------------------------------------------------
4) Assigning Values to Variables

Assigning Values to Variables is two types

i) Initialization

ii) Reading

Example:
Dim num1, num2
num1 = 100 'Initialization
num2 = InputBox ("Enter Num2 value") ' Reading
Msgbox num1 + num2
-----------------------
5) Purpose of Variables

Dim a, ord
'Holding the data
a = 100
Msgbox a
'Storing the data that returned by a Function
a = Now
Msgbox a
'Storing the data that returned by a program
a = (10 ^ 3)*2
Msgbox a

'storing object Reference
Set a = CreateObject("Scripting.FileSystemObject")

'As parameter
For ord = 1 To 5 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 ord
Wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Next
------------------------------------------
6) Naming Restrictions

i) Variable names should start with Alfa bytes.
Ex:
Dim abc 'Correct
Dim a9 'Correct
Dim 7ab    'Incorrect

ii) Variable names should not contain embedded periods.
----------------------------------------
7) Scope of variables

8) Types of Variables

9) Array variables
    Constant, Dynamic and Diementional Arrays
    Assigning series of values to an Array variable
--------------------------------------------------------------
(*Incomplete)

2 comments:

This comment has been removed by a blog administrator.

Really very good tutorial. Thank you

Post a Comment