http://quicktesthp.blogspot.com

QTP VBScript new series + Interview Question Bank on QTP for enrichment of Knowledge in QTP

This Site has been brought to you by HP Certified Expert of QTP.

Exciting new articles for October:

1) QTP Tip:Deselect all Radio Buttons

2) HP QTP Crypt Object

3)Adding Hyperlinks in Excel Spreadsheet

Best of Luck Friends ! ! !

Expert QTP
expert.qtp@gmail.com

All Articles are Copyright Protected. These are Free for Reading & Personal Use. Reproduction in Any Form without the Permission is Illegal & Strictly Prohibited.

Copyright © 2009 ExpertQTP

Google Search

Saturday, November 8, 2008

VBScript a Stepping-stone to learn QTP Automation Tool

It is quite obvious that to be a successful practitioner of QTP, one needs to thoroughly understand Vbscript.

I am presenting below my first article cum tutorial in this series on VBscript, which is a step-by-step guide for all enthusiasts eager to learn QTP.

***********************
VBScript:

Vbscript is a scripting language developed by Microsoft. It is the simplified version of Visual Basic. It is very closely related to BASIC programming language.

What is a Variable?

A variable is a container in the computer’s memory which is used to store information that you want to store. It is possible to change the value of the variables inside your script. In VBScript, all variables are of one fundamental datatype: Variant which means it can store different types of data.

Naming conventions for Variables

While creating variables, you need to follow some rules. These are:
  • They should always begin with a letter and they should not contain a period(.) sign.
  • Variable names should not exceed 255 characters.
  • They should be unique in the scope in which they are declared.
  • They should not have names same as keywords like “Dim”, “For” etc.
How can I create variables with the above knowledge?

Variables can be created using Public, Private or Dim statements. The most common statement is Dim.

For an example, you can use the following syntax to declare variables.

###########
Dim var
###########

Initially this variable has no value which means its empty. After initializing a variable and assigning a value like in above case, the following expressions will evaluate to True:

If var = 0 Then
If var = “” Then

You can also create variables even without using Dim statement however it is not recommended to do so. Following example will clarify the concept:

Dim A
A=B+C

Is there any way to use keywords as variable names?

Yes, there is a way. Through this way we can even use keywords or some special symbols as variable names. Lets see, how it done. For this, you have to place variable names inside brackets.

Dim [For]
[For] = 20
Msgbox [For]

Here, we created a variable named [For] inside brackets which is not possible otherwise. Now the “Msgbox” is a predefined function inside VBScript which is used to display a dialog box to see the value of the variable. The output of the code is:


What is Option Explicit statement?

Option Explicit is used to force explicit declaration of all variables inside your script. This statement is always used at the top of all VBScript statements. This means, you are forced to declare all your variables using either of the statements “Dim”,“Public” or “Private”.

Now lets see what happens if we try to use a variable without using Dim statement in the case when Option Explicit statement is also used.

Option Explicit
A=10
MsgBox A

This is the output you will get:


Hence to avoid this error, use the following code:

Option Explicit
Dim A
A=10
MsgBox A


Please wait for my next VBScript Tutorial in this series going to be posted very shortly.

Keywords:
QTP, QTP & VBScript, VBScript Tutorial, VBScript & QTP

No comments:

 
Copyright © 2009 ExpertQTP