VBScript Tutorial 1

VBScript stands for Visual Basic Script and is developed by Microsoft.

Quick Test Professional (QTP) uses VBScript as its scripting language. This article talks about:

 - How to Run VBScript code
 - Variables & Variable Declaration
 - Types of Variable
 - Comments in VBScript
How to Run VBScript code

VBScript doesn’t require any special editor to write and execute script. VBScript code can be written and executed with Notepad. Simply write your code in notepad and save your script with .vbs extension. Make sure that while saving you choose value All Files in the drop down Save as type.

Save As Dialog Box

Once your script is saved, just double click on the script file to run it.

Variable & Variable Declaration

A variable is a place holder for some value. VBScript is not strict about declaring a variable before using it. A variable is automatically declared when it is used anywhere in the script. This is called implicit declaration. But it is not considered a good practice and may introduce hard to debug issues in the code. E.g.:

a=5
b=7
msgbox a+b

In the above code snippet, we didn’t declare variables a and b before using them.

To make declaration of variables mandatory, we use command Option Explicit. This is called explicit declaration.

If you are using Option Explicit, it must be the first statement in your VBScript program.

Try using Option Explicit command with the above code snippet.

Option Explicit
a=5
b=7
msgbox a+b

Now run this script. You would get variable is undefined error message and your script won’t run because Option Explicit command expects that you would declare the variables before using them.

Variables in VBScript are declared using command Dim. E.g.:

Dim 
Dim <varname1>, ….so on

Now let’s fix the above code snippet by using variable declaration.

Option Explicit
Dim a, b
a=5
b=7
msgbox a+b

Run the script now and you script would run successfully.

Variable Naming Restrictions-

 – Variable name must begin with and alphabet.

 – Variable name cannot contain an embedded period symbol.

 – Variable name must not exceed 255 characters.

Types of Variables

VBScript has two types of variables:

1. Scalar

2. Arrays

1. Scalar Variables – A variable that can hold only one value at a time is called scalar variable. All the variables that we used in above sample scripts were scalar variables. They are declared as:

Dim varname1, varname2,….

2. Arrays: A variable that can hold multiple values at a time is called an array. To declare array variable, variable name is followed by parenthesis & parenthesis contains dimensions of the array and size for each dimension. E.g.:

Dim firstArray(5)

Here firstArray is an array variable of size 6 that it can hold six values at a time.

You might be wondering that we gave the value 5 in parenthesis then why is it an array of size 6. It’s because arrays in VBScript are zero index arrays that means array index starts at 0. So the size of the arrays would be value mentioned in the parenthesis + 1 hence it can contain 6 elements.

Here is how you assign value to each element in the array:

firstArray(0) = 25
firstArray(1) = 23
firstArray(2) = 45
firstArray(3) = 782
firstArray(4) = 226
firstArray(5) = 96

The array we declared above is a single dimension array. VBScript allows arrays with up to 60 dimensions.

Here is how you declare a multi dimension array:

Dim multiDimenArray(5, 9)

Here the first value in the parenthesis is first dimension of the array and represent number of rows. In this case, number of rows are 6.

The second value in the parenthesis is the second dimension of the array and represents no of columns. In this case, no of columns are 10.

Few more examples are:

Dim array1(2, 4, 5) – Three dimension array
Dim array2(12, 4, 6, 8) – Four dimension array
Dim array3(2, 14, 5, 8, 45) – Five dimension array

Types of array: VBScript has two types of array:

1. Static array: An array whose size can’t be changed at run time.

2. Dynamic array: An array whose size can be changed at run time.

Static arrays are declared with Dim followed by variable name with parenthesis having a value in the parenthesis. The array firstArray we declared above was a static array.

Dynamic arrays are also declared in the same way except that parenthesis doesn’t contain any value. Parenthesis must be blank. To give a size to the dynamic array and make it a usable, Dim statement must be followed by a ReDim statement. Lets see an example:

Dim dynArray()
Redim dynArray(5)

So now dynArray is a dynamic array of size 6. You can use Redim statement any number of times after this to resize this array.

Redim dynArray(1)
Redim dynArray(4)

What happens to the existing data when you resize a dynamic array:  If you resize the array using Redim, the data in the array is not retained. If you wish you to retain the existing data in the array, using keyword Preserve while resizing the array:

Our last statement resized the array to size 4. Let’s resize it to size 10 using Preserve along with Redim.

Redim Preserve dynArray(9)

But if you resize an array to a smaller size than it was initially, you are bound to lose the data.

NOTE: If you are using Preserve keyword:

1. You can only resize last dimension of the of a multi dimension array. In the above example, dynArray had only one dimension so you could resize it. Lets take an example of multi dimension array:

Dim multiDimArray()
ReDim multiDimArray(2, 4, 5)
Redim Preserve multiDimArray (2, 4, 6)

You can only resize the last dimension here, if you try to resize any other dimension you will get a run time error.

2. You cannot increase or decrease the number of dimension. So this would not work:

Dim multiDimArray()
ReDim multiDimArray(2, 4, 5)
Redim Preserve multiDimArray (2, 4)

How to place comments in the VBScript:

Comments in VBScript

Comments are non executable statements in the code that provide info about the code so that it is easy to maintain

1. Using apostrophe symbol: Any text on the same line following apostrophe symbol is treated as a comment. E.g.:

‘This is a comment.

2. Using Rem keyword: Any text on the same line following keyword Rem is treated as a comment. E.g.:

Rem This is a comment too.

That’s all for this article. Next article will talk about data types & operators in VBScript.

Advertisements

1. Introdction to QTP

As our target audience are those people who would be trying to learn QTP from scratch so I thought why not to start from the very basic. So let me first tell you how to just open the quick test window and make you comfortable with some of the components of qtp and common terminology we would be using in qtp.

Opening a Quick Test Window

1. Choose Start > Programs > HP QuickTest Professional > HP QuickTest Professional

 2. In the Add-in Manager, confirm that the Web Add-in is selected, and clear all other add-ins. Click OK to close the Add-in Manager and open QuickTest. While QuickTest loads your selected add-ins, the QuickTest splash screen is displayed.

3. If QuickTest is already open, check which add-ins are loaded by selecting Help > About QuickTest Professional. If the Web Add-in is not loaded, you must exit and restart QuickTest. When the Add-in Manager opens, select the Web Add-in, and clear all other add-ins.

Components of QTP

The image below shows a QuickTest window as it would appear after you create a test, with all toolbars and the Data Table and Resources panes displayed:

Click on the image to enlarge it.

  • Test. Enables you to create, view, and modify your test in the Keyword View or Expert View:
    • Keyword View. Contains each step, and displays the object hierarchy, in a modular, icon-based table.
    • Expert View. Contains each step as a VBScript line. In object-based steps, the VBScript line defines the object hierarchy.
  • Function Library. Enables you to create, view, and modify functions (operations) for use with your test. Function libraries open as separate tabs or windows in the documentation area, similar to tests

Recording a test

Now Lets try and record a test. For recoding a test would advise you to please open the Mercury Tour home page ie http://newtours.demoaut.com. I am using internet explorer ie8 and the browser would look like somewhat like below:

Click on the image to enlarge it.

How to record a test?

Just click on the Record button displayed below on your QTP window

Click on the image to enlarge it.

As soon as you would click on the record button the Record and Run Setting window would open.

Click on the image to enlarge it.

Select the web tab

Now we could see two check boxes

1. Record and run test on any open browser

2. Open the following application when a record or run session begins

We would select the Record and run test on any open browser for the moment and click the ok button.

Now Activate Mercury Home Page and Login by entering tutorial in both the User Name and Password boxes and click Sign-In.

Post Sign-In activates the QTP window again and click on the stop button shown below

Click on the image to enlarge it.

Now, close all open browsers and open http://newtours.demoaut.com. Post this just click on the Run button shown below.

Click on the image to enlarge it.

As soon as you would click on the record button you would see your script logging into the website on it own.

I suppose that’s more than enough for the first post, so guys try record and run some more on this site.

The next post would be about understanding the recorded scripts on both the Keyword and expert view and understanding how the objects get added to the Object Repository(OR).

Happy learning!

Robot Framework with Selenium2Library on Windows

Robot Framework is a test automation framework developed in Python. It uses keyword driven approach and allows user to write test cases in easily understood language. This post explains how to set up RF with Selenium on a Windows machine.

Before starting the installation process, check if your windows machine is of 32-bits or 64-bits. Make sure your installers are of same bit size. It’s important because if you install 64-bit python on 32-bit machine or vice versa, one or the other thing won’t work.

windows

Step 1 – Install Python: We need python because Robot Framework is implemented using python and so we need python interpreter to run it. Run following command on command line:

python --version

If you get the response as shown in the snapshot below, it means Python isn’t installed in your system.

No Python

Go to http://python.org and install python. Python 2.5 is the minimum version that is required. Robot framework & RIDE doesn’t support python 3.3 (latest version) as of writing of this article and Python 3.3 is not backward compatible. You can install python 2.7.3 version. Choose the same bit installer as is your Windows.

Step 2 – Install wxPython: Robot Framework IDE’s (RIDE) GUI is implemented using wxPython tookit. You need version 2.8.6 or higher of wxPython with Unicode Support. The ANSI version of wxPython is not supported. In Windows 7, recommended wxPython version is 2.8.11.0 or newer.

Go to http://wxpython.org/download.php and download & install wxPython.

wxPython

Do add your python installation directories to environment variable. By default Python is installed at C:\Python27 and C:\Python27\Scripts.

Type the command python –version on command line, it will return the Python version you just installed if installation was successful.

Python Installed

Step 3 – Install Robot Framework (RF) and Robot Framwork IDE (RIDE): There are multiple ways to install Robot Framework. You can install RF from source, via Windows installers or through Python package managers. Following installation steps in this post are via Python package manager pip. pip is the successor of python package manager easy_install and RF official documentation recommends using pip over easy_install.

To install pip, go to this page, download pip-win_1.4.exe & install it. pip-Win automatically installs pip in your system and provides with a GUI interface to install/uninstall/upgrade other packages. Once pip-win is installed, type and run following commands one by one, either in pip-Win GUI that launches after pip-Win installation or on command line.

pip install robotframework

RF

pip install robotframework-ride

Ride

Above two command will download and install latest version of Robot Framework and Robot Framework IDE (RIDE) respectively. If you want to install some specific version, type the command as:

pip install robotframework==<version>
pip install robotframework-ride==<version>

Once the installation is done, run following command on command line:

pybot --version

You will get the version of the Robot Framework installed, if the installation was successful.

Run ride.py on command line & RIDE will launch.

Step 4 – Install Selenium2Library: Selenium2Library can be installed using pip and advantage of installing it via pip is that it will automatically install its dependencies. If you install Selenium2Library using easy_install package manager, you will have to install Selenium2Library’s dependencies separately. To install using pip type following command in pip-Win GUI or on command line:

pip install robotframework-selenium2library

Side note: Click Check package freshness button in pip-Win, it will list down all the packages, along with any dependencies that might have installed, with the version and status(up to date or not).

pip Packages

Notice the first four packages listed in the snapshot, distribute, decorator, docutils, Pygments, those are Selenium2Library’s dependencies that were installed automatically(advantage of pip). If you use easy_install, you’d need to install them separately.

With this installation of Robot Framework with Selenium & Robot Framework IDE(RIDE) is complete. Next post will talk about how to write RF test cases.

References:

Official Robot Framework Installation Documentation
Official Robot Framework IDE (RIDE) Installation Documentation