UFT Tutorial 21

UFT Class 21(VBScript Fundamentals and Features)

1) Comments
    To make the code readable
    To make the code disable from execution

2) Data Types    VBScript supports implicit declaration of Data Types.
    In VBScript, only data type is Variant.
    Using VarType function we can check data sub-type
Dim a
a ="abcd"
a = 123
a = 1.23
a = #10/10/2010#

3) Declarations
a) Constants Declaration    Built in
    User defined

They are used to replace literal values and they never change


Const city = "London", num = 100, myDate = #10/10/2010#
b) Variables Declaration    Scalar (It can hold single value)
    Array (It can hold series values based on size of the Array)
    Dictionary object (Associated Array) (User can define Key (Index), value pairs

Variable, A named memory location to store the data.

Dim a, b(3), c(), d(4, 5)
4) VBScript Operators
Operators are used to perform Mathematical, Comparison and Logical operations.

i) Arithmetic

ii) Comparison

iii) Logical Operators
5) Flow Control Statements
a) Conditional Statements

    i) If Statement
    ii) Select Case Statement

Usage of Conditional Statements in UFT Test Automation:

i) To insert verification points

ii) For Error Handling
Types of Conditions
i) Single condition (Positive and Negative conditions)

If a = b Then -Positive Condition

If a <> b Then - Negative Condition
ii) Compound condition (Positive and Negative conditions)

If a > b And a > c Then

iii) Nested Conditions (Positive and Negative conditions)
If a > b Then
 If a > c Then
  If a > d Then
b) Loop statements
    i) For...Next
    ii) While...Wend
    iii) Do While / Until ...Loop
    iv) For Each...Next
6) VBScript FunctionsFunction, A reusable code.
> Whenever we want to perform an operation multiple times then we prefer functions.

Types of Functions

i) Built in Functions    Array functions
    String functions
    Date & Time functions
    I/O functions
    Conversion functions
    Math functions etc...

ii) User defined Functions
    Sub Procedures
        Public (Internal, External)
    Function Procedures
        Pubic (Internal, External)
7) Coding Conventions
Dim city, num, FSO - No coding standards

Dim strCity, intNum, objFSO -Coding Standards
8) Regular Expressions
Regular Expression, It is a formula for matching patterns

Constant matching

india.doc - india.doc

Pattern matching



Usage of Regular Expression in UFT

    To handle Dynamic objects
    For Search operations
9) File System Operations
> What is Computer File System?

It is a feature of Operating System used to work with Drives, Folders and files.
> Examples for File System Operations

Create a Folder
Copy a Folder
Delete a Folder

Create a Text file
Read data
write data
compare data
search operations etc...
> How end user performs File System Operations

End user performs File system operations manually with the help of Input devices.
> How to perform automatic File System Operations

Using FileSystemObject we can perform automatic File System Operations.

Ex: VBScript for Creating a Folder:

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CreateFolder "C:\Users\G C Reddy\Desktop\UFT"
> File System object is only for performing operations on Drives, Folders and Flat files

> Using File System Object we can create and delete all types of files(Text, Excel, Word, PDF etc...)

> Using File System Object we can Perform all operations on Flat files.
Syntax for Creating Automation Object

Set Variable = CreateObject ("Class Value")

Set - VBScript Statement

CreateObject - VBScript Built in Function
Class Value for creating FileSystemObject ("Scripting.FileSystemObject")
10) Excel Application Operations
Using Excel Application Object we can perform Excel operations

Create an excel file
Read data
Write data
Compare data
Rename sheets
Add sheets
Move sheets
Class Value for creating Excel Application Object ("Excel.Application")
11) Word Application Operations
Word Application Object
It is used to perform operations on Word Application/word documents

Class Value for creating Word Application Object ("Word.Application")
12) Database Operations
i) Database Connection Object
It is used to connect to Databases (Any database, connection string only varies form one database to another)

Class value for creating Database connection object- ("Adodb.Connection")
ii) Database Recordset Object
It is used to perform operations on database tables(Records)

Class value for creating Database Recordset object - ("Adodb.Recordset")
13) Dictionary Object
It is used to define Key, value pairs.

Class value for creating Dictionary Object - ("Scripting.Dictionary")
14) Error Handling in VBScript
Handling expected and unexpected Errors

Expected Errors: Whenever we use invalid input then we can expect Errors.

Why we need to use Invalid input?

We use Invalid Input for Negative Testing.

Unexpected Errors:

Resource Response

Insufficient Resource

Availability of Resource
    Using Conditional Statements
    Using VBScript Built in Functions
    Using Option explicit statement
    Using Exit Statement
    Using On Error Resume Next statement


Post a Comment