• VBScript for Automation (QTP/UFT) Testing – Part 1

    In VBScript – Part 1, let’s see the following topics:

    Introduction
    Prerequisites
    General Information
    Data Types
    Variables
    Operators – Arithmetic, Relationship, Logical & Special
    Conversion functions
    Validation functions

    VBScript - Part 1

    INTRODUCTION:

    VBScript (Microsoft Visual Basic Script) is a scripting language developed by Microsoft. VBScript is the scripting language for QTP (Quick Test Professional).

    A scripting language is a lightweight programming language

    Why should a tester learn VBScript?

    If a tester would like to become an Automation (QTP) Tester, learning VBScript is necessary.

    PREREQUISITES:

    Basic knowledge on computer programming

    Tools:

    Notepad or Notepad++ or EditPlus. Lots of tools available online for VBScript. Google it and use the one which is handy to you.

    GENERAL INFORMATION:

    VBScript is case insensitive language (not case sensitive)

    VBScript is an interpreter (line by line execution). It is not a compiler (like C++, .net, java)

    See the difference between Interpreter and Compiler here.

    VBScript is also called as line by line compiler

    In windows, 2 Injens to support VB Script file:

    wscript.exe (windows script) – default

    cscript.exe (command script) – if file is missing/corrupt use this. (cscript “file name”)

    Lots of editors are available to write scripts. I suggest Notepad++

    DATA TYPES:

    VBScript supports only one data type called Variant. The variant data type is a special kind of data type that can contain different kinds of information. It acts as a string when used in a string context and acts as a numeric when used in a numeric context.

    VARIABLES:

    VBScript variables are used to hold values or expressions

    Variable Name Rules:

    Must begin with an alphabet

    Cannot contain a period (.)

    Cannot exceed 255 Characters

    Must not use predefined types (e.g. rem, ‘)

    Must not use special characters

    Must not include space

    Variable Declaration:

    Variables in VBScript can be declared in three ways:

    1. Dim Statement
    2. Public Statement
    3. Private Statement

    Single variable: We declare single variable as shown below

    Multiple variables: We can declare multiple variables by separating variables with comma

    For a best practice, use Option Explicit statement to declare variables. Sometimes we may misspell the variables names in the script. It leads to failing the script. Option explicit allows us to avoid this issue.

    Assigning a numeric value to a variable:

    In the above expression, SoftwareTestingMaterial is a variable and 123 is a value

    Assigning a string value to a variable:

    In the above expression, SoftwareTestingMaterial is a variable and “stm” is a string value

    Assigning a number as a string to a variable:

    Assigning multiple values to a variable:

    Note: Refer Arrays.

    SUB DATA TYPES:

    TypeName()

    It gives the sub-data type of a variable

    Example:
    Different Sub Data Types available in VBScript:

    Integer, String, Double, Date, Currency, Boolean, Empty, Null, Error, Nothing

    OPERATORS:

    Different Operators supported in VBScript:

    1. Arithmetic Operators
    2. Relationship Operators
    3. Logical Operators
    4. Special Operators

    ARITHMETIC OPERATORS:

    Example:

    RELATIONSHIP OPERATORS:

    Always returns the Boolean Value

    Example:

    LOGICAL OPERATORS:

    Accepts the input as Boolean value

    AND, OR, NOT

    A B Series Parallel Reverse
    A AND B A OR B NOT A
    T T T T F
    T F F T F
    F T F T T
    F F F F T

    SPECIAL OPERATORS:

    Example:
    Example:
    Example:
    = – Assignment

    Example:
    Example:
    Note: The first left most “=” operator is an assignment operator and remaining all are relationship operators.

    INPUT BOX FUNCTION:

    CONVERSION FUNCTIONS:

    It converts from one valid sub-data type to another valid sub-data type.

    cint() – Converts from String (it should be always a numeric string) to a variant of subtype Integer.

    cdbl() – Converts from String to a variant of subtype double value

    cdate() – Converts a valid date and time expression to the variant of subtype Date

    cbool() – Converts from String to a variant of subtype Boolean value string either True/False

    cstr() – Converts from Integer to a variant of subtype String

    ccur() – Converts an expression to a variant of subtype Currency

    cbyte() – Converts an expression to a variant of subtype Byte

    Example:

    VALIDATION FUNCTIONS:

    To validate the type of the data

    All the validation functions returns Boolean value

    Some of the important validation functions are

    Isnumeric(), Isempty(), Isarray(), Isobject()

    Example:
    Input: 4 and 5

    Output: 45

    I would like to conclude VBScript – Part 1 here and will start VBScript – Part 2 in the next post.

    VBScript Series:

    VBScript for Automation (QTP/UFT) Testing – Part 1

    VBScript for Automation (QTP/UFT) Testing – Part 2

    VBScript for Automation (QTP/UFT) Testing – Part 3

    VBScript for Automation (QTP/UFT) Testing – Part 4

    VBScript for Automation (QTP/UFT) Testing – Part 5

    Sharing is caring.

    Share on facebook
    Facebook
    Share on twitter
    Twitter
    Share on linkedin
    LinkedIn

    Like This Post?

    We have a lot more where that came from?

    We only send really good stuff occasionally, promise.

    Rajkumar SM

    3 thoughts on “VBScript for Automation (QTP/UFT) Testing – Part 1”

    1. Excellent post!!! QTP has been a leading automation tool. Thanks admin for sharing this useful information with us. Keep on updating.

      1. Hi Manjit, we are passing two input string values 4 and 5 so the output is 4+5 (4 concatenate 5) = 45. If you convert the input value to integer (cint) then it will be 9 (cint(4)+cint(5).
        Here is the sample,
        option explicit
        dim a, b, res
        a = inputbox(“Enter the value”)
        b = inputbox (“Enter the value”)
        res=cint(a)+cint(b)
        msgbox res

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    Share on facebook
    Share on google
    Share on twitter
    Share on linkedin
    Share on whatsapp

    Recent Posts:

    Scroll to Top
    API Testing eBook

    DOWNLOAD FOR FREE