Software Testing Material

A site for software testers. We provide free online tutorials on Manual Testing, Automation Testing - Selenium, QTP, LoadRunner, Testing Tools and many more.

  • Blog
  • Tutorials
    • Manual Testing
    • Java
    • Selenium
      • TestNG
      • Maven
      • Jenkins
    • Framework
    • Katalon
    • Agile
    • SQL
    • VBScript
    • API Testing
  • Tools
    • TestLodge
    • FrogLogic GUI Tool
    • CrossBrowserTesting
    • BrowserStack
    • TestCaseLab
    • Kobiton
    • Sikuli
    • Postman
  • Interview Q & A
    • Selenium
    • TestNG
    • Test Framework
    • Explain Framework
    • Manual Testing
    • Software QA
    • Agile
    • Testing As A Career
    • General Interview Questions
    • API Testing
    • SOAP
  • Free Resources
  • Guest Post
    • Guest Post Guidelines
  • Training

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

Last Updated on December 3, 2017 by Rajkumar

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

1
Dim SoftwareTestingMaterial

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

1
Dim SoftwareTestingMaterial, SoftwareTestingMaterial1, SoftwareTestingMaterial2

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.

1
2
Option Explicit
Dim SoftwareTestingMaterial

Assigning a numeric value to a variable:

1
2
3
Option Explicit
Dim SoftwareTestingMaterial
SoftwareTestingMaterial  = 123

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

Assigning a string value to a variable:

1
2
3
Option Explicit
Dim SoftwareTestingMaterial
SoftwareTestingMaterial  = "stm"

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

Assigning a number as a string to a variable:

1
2
3
Option Explicit
Dim SoftwareTestingMaterial
SoftwareTestingMaterial  = "123"

Assigning multiple values to a variable:

1
2
3
Option Explicit
'Dim arrayvalue(size)
Dim SoftwareTestingMaterial(2)

Note: Refer Arrays.

SUB DATA TYPES:

TypeName()

It gives the sub-data type of a variable

Example:

1
2
var="Software Testing Material"
msgbox typename(var)

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:

1
+, -, *, /, \

Example:

1
2
3
4
5
6
7
Dim a,b
a=20: b=10
msgbox a+b
msgbox a-b
msgbox a*b
msgbox a/b
msgbox a\b

RELATIONSHIP OPERATORS:

Always returns the Boolean Value

1
=, >, <, >=, <=, <>

Example:

1
2
3
4
5
6
7
8
9
Option explicit
Dim a,b
a=5: b=4
msgbox a=b
msgbox a<b
msgbox a>b
msgbox a>=b
msgbox a<=b
msgbox a<>b

LOGICAL OPERATORS:

Accepts the input as Boolean value

AND, OR, NOT

ABSeriesParallelReverse
A AND BA OR BNOT A
TTTTF
TFFTF
FTFTT
FFFFT

SPECIAL OPERATORS:

1
Single Quote (') - To comment

Example:

1
2
Str="Software Testing Material"
'msgbox str

1
Rem - To comment

Example:

1
2
3
4
str="Software Testing Material"
rem msgbox str
 
& -  concatenation

Example:

1
2
3
str="Software Testing Material"
str1="website: "
msgbox str1 & str

= – Assignment

Example:

1
2
3
4
5
option explicit
Dim a,b,c
a=5: b=4: c=3
a=b=c
msgbox a

Example:

1
2
3
4
5
Option explicit
Dim a,b,c,d
a=5: b=4: c=3: d=false
a=b=c=d
msgbox a

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

INPUT BOX FUNCTION:

1
2
3
str = Inputbox("Enter the value")
msgbox str
msgbox typename(str)

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:

1
2
3
4
str = "123"
msgbox cint(str)
msgbox typename(str)
msgbox typename(cint(str))

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:

1
2
3
4
5
6
option explicit
dim a, b, res
a = inputbox("Enter the value")
b = inputbox ("Enter the value")
res=a+b
msgbox res

Input: 4 and 5

Output: 45

1
2
3
4
5
6
If isnumeric(a) and isnumeric(b) then
res = cint(a) + cint(b)
msgbox res
else
msgbox "Enter integer value"
End if

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

SUBSCRIBE TO GET FREE EBOOK AND REGULAR UPDATES ON SOFTWARE TESTING

Filed Under: VBScript

data-matched-content-rows-num="2" data-matched-content-columns-num="3"
Previous Article:
VBScript for Automation (QTP/UFT) Testing
Next Article:
VBScript for Automation (QTP/UFT) Testing – Part 2

About the Author

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

Comments

  1. varshini says

    September 17, 2016 at 11:11 am

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

  2. manjit says

    December 3, 2017 at 2:39 am

    sir how is the output 45 in isnumeric program??

    • Rajkumar says

      December 3, 2017 at 3:07 am

      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

ADVERTISEMENT

Froglogic-Squish-GUI-Tester
CrossBrowserTesting

TUTORIALS

  • Manual Testing Tutorial
  • Selenium Tutorial
  • TestNG Tutorial
  • VBScript Tutorial
  • Agile Methodology
  • SQL Tutorial for Testers
  • INTERVIEW QUESTIONS

  • Framework Interview Questions
  • Real Time Manual Testing
  • 100 Top Selenium Interview Questions
  • 30 Top TestNG Interview Questions
  • Agile Interview Questions
  • 80 SQL Interview Questions
  • TESTING TOOLS

  • Test Lodge
  • FrogLogic Squish
  • Cross Browser Testing
  • BrowserStack
  • Test Case Lab
  • Sikuli
  • © 2019 www.softwaretestingmaterial.com | About us | Privacy Policy | Contact us | Guest Post | Disclaimer | Terms of use | Sitemap