Sunday, June 3, 2012

VBScript Special Characters


Before we actually look at the special characters, we’re going to cover a fundamental aspect of writing a script: the line. This one line is a script:
Wscript.Echo "My very first script."

If you want to echo two lines, you might try something like this:
Wscript.Echo "Line 1" Wscript.Echo "Line 2"

You might try it, but it won’t work. As a general rule, with VBScript you can have only one command per line. So you’d have to write the script like this:
Wscript.Echo "Line 1"
Wscript.Echo "Line 2"

Note: Okay, there is a way to put more than one command on a line. That’s another special character, the colon (:). This script will work:
Wscript.Echo "Line 1" : Wscript.Echo "Line 2"

As you can see, we just separated the two commands with a colon. However, if you want the advice of the Scripting Guys (and who doesn’t?), we would advise you never to use this type of notation. It makes your scripts hard to read, and is just plain ugly. If you do script like this, well, just don’t tell anyone you learned your scripting technique from us.

Another requirement of VBScript is that the entire command must be on one line. So this won’t work either:
Wscript.Echo
"Line 1"

Also, multiple spaces are pretty much ignored (unless of course they’re inside a string). So this:
Wscript.Echo "Line 1"

will work the same as this:
Wscript.Echo             "Line 1"

(but not the same as this:)
Wscript.Echo "         Line 1"

Given that, you can infer that it doesn’t matter where on the line you start. This allows for indenting that helps make your script more readable. For example:
For i = 1 to 5
    Wscript.Echo i
Next

This script simply counts from 1 to 5 and echoes the number. We’ll talk about loops in a different article, but for now just know that everything between the For line and the Next line will happen five times. One quick look at this loop tells you that Wscript.Echo is between a For line and a Next line, and will therefore be called five times. You would have to look much more closely to notice that fact in this next script, especially if it were part of a larger script or we had more than one Echo statement between the For and the Next:
For i = 1 to 5
Wscript.Echo i
Next

And with that foundation, let’s move on.


This & That
A character you’ll see quite often in VBScript is the ampersand (&). One of the primary purposes of the ampersand is to concatenate things, such as strings or variables. For instance, suppose we want the first and last name of a particular user. The problem is that you have to retrieve the first and last names from different places, such as two different fields in Active Directory or different records in a database, but you want to display them on a single line, like this:
Ken Myer

We already know we can’t do this:
First = "Ken"
Last = "Myer"
Wscript.Echo First Wscript.Echo Last

That’s too many commands on one line. We also can’t do this:
First = "Ken"
Last = "Myer"
Wscript.Echo First Last

Wscript.Echo lets you specify only one thing to echo at a time. So how do we get the first and last names together? That’s where the ampersand comes in:
First = "Ken"
Last = "Myer"
Wscript.Echo First & Last

Nice, huh? No? What’s wrong? Oh yes, the space. The above script gave us output like this:
KenMyer

But we want to show a distinct first and last name. That’s easy enough. You can use as many & signs as you want on a single line:
First = "Ken"
Last = "Myer"
Wscript.Echo First & " " & Last

That’s better:
Ken Myer

So you might be thinking, "Wait a minute. You guys said we could echo only one thing at a time, this is three things: First, Last, and a space!" That’s the beauty of the ampersand, it lets you bend the rules a little. Say you’re in the grocery store and the lines are really long. There’s one line that’s moving quickly, but it’s the express line where you must have 15 items or less. You have three six-packs of Pepsi. Do you have 18 items, banning you from the express line, or only three? Because they’re bundled up together, you would say you have three and go through the express lines, and chances are no one would complain. The ampersand basically bundles things together for you so you don’t have to deal with separate strings, variables, and commands, and VBScript won’t complain. Much quicker and easier, just like the express line.

But what happens if your script has a really long line? A single line can get pretty hard to read when it stretches out off the screen. For example:
intAnswer = 42
Wscript.Echo "The answer to life, the universe and everything is: " & intAnswer

Granted, this line isn’t outrageously long, but just go with us here. We already saw that we can’t just put part of the command on the next line, like this:
intAnswer = 42
Wscript.Echo "The answer to life, the universe and everything is: " 
    & intAnswer

So what do we do? Technically we don’t have to do anything; lines can be as long as we want. However, to ensure our scripts are easily readable we use the line continuation character, otherwise known as the underscore (_).

To Be Continued

The primary use of the underscore is to make scripts easier to read. Suppose you had a single command with 100 or more characters in it. The line might go out past the end of your Notepad window and you’d have to keep scrolling over to see the whole script. We can’t even give you a script to try here because that would stretch this Web page out and make this whole article hard to read. So we’ll show you a picture:
Long Lines 
You could always turn on Word Wrap in Notepad, but that gets a little ugly too:
Long Lines with Word Wrap 
So what do we do about this? We use the underscore to break everything up. Now we can show you a script:
intComputers = 40
intServer = 5
intDesktop = 35
Wscript.Echo "There are " & intComputers & _
    " computers in use by the Accounting department: " & _
    intServer & " servers and " & intDesktop & " desktop machines."

And here’s what it looks like in Notepad:
Long Lines Formatted 
Notice that we also indented a few spaces so you can tell at a glance that the two continued lines are still part of the Wscript.Echo statement. The script is much easier to read now. And you know how much the Scripting Guys like to do things the easy way.

More Information




Statement Breaks in VBScript - Windows 2000 Scripting Guide

' Comments

One of the Scripting Guys is a big fan of the television game show Jeopardy! That’s not to say this Scripting Guy is particularly good at this game; most of the show is spent saying "I know that, I know that…ummm…." This same sort of memory lapse can occur when you write scripts. "Why did I write it this way?" "What was this script doing?" "I know I had a good reason for this…"

The easiest way to ensure you’ll always know what a script is doing is to comment it. Now, you might hear people saying things like "comment every line," or "put as many comments in as you possibly can." We’re going to be a little more sensible about it. After all, commenting means more typing, and we like to avoid excess typing whenever possible.


So what is a comment? Well, this brings us to the final character we’re going to discuss today. In VBScript, a comment is any line that starts with a single quote character (').
' This script echoes a name
Wscript.Echo "Ken Myer"

So you know how we just said we’d be sensible? That comment isn’t sensible. One look at the Wscript.Echo statement and you know exactly what the script is doing, you don’t need a comment to tell you that. Just to show you a better example, we’ve taken a script from a recent Tales from the Script column on scripting popup blockers and added some comments:
' Returns whether or not the registry key associated with 
'  the MSN Toolbar Suite popup blocker exists. If it does, the
'  popup blocker is already installed.
Const HKEY_CURRENT_USER = &H80000001

strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
 
strKeyPath = "Software\Microsoft\MSN Apps\MSN Popup Blocker"
strValueName = "Enabled"
objRegistry.GetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue

If IsNull(dwValue) Then
    Wscript.Echo "The registry key does not exist."
Else
    Wscript.Echo "The registry key exists."
End If

One important thing to note is that you can’t use the continuation character (underscore) with comments. Every line that has a comment must start with a single quote.

You can also put a comment at the end of a line. Try this:
Wscript.Echo "Line 1" ' This echoes one line

You know how we said you can put only one thing after the Echo statement unless you’ve used an &? Well, comments don’t count as being a "thing." Everything on a line following the single quote is ignored; as far as VBScript is concerned, it doesn’t even exist.

Advanced: Troubleshooting with Comments

So there’s a really important use of the comment character other than putting in comments to explain your code. As we said, if VBScript sees a single quote mark, it ignores that quote and everything on the line after it. That means you can use the quote mark as a strategic means of troubleshooting. Try this script, which you already know won’t run:
intAnswer = 42
Wscript.Echo "The answer to life, the universe and everything is: " 
    & intAnswer

This script produces an error message that states the problem is on line 3 (that’s what the 3 in (3, 5)means):
C:\Scripts\test.vbs(3, 5) Microsoft VBScript compilation error: Expected statement

So let’s turn line 3 into a comment (know as "commenting out" line 3) by placing a single quote mark at the beginning of the line. Try the script now:
intAnswer = 42
Wscript.Echo "The answer to life, the universe and everything is: " 
'    & intAnswer

As you can see we no longer have an error, which means that yes, the problem is with the third line, the rest of the script is fine.

Armed with this information, you can probably come up with other uses for commenting, or commenting out, lines of code.

Saturday, June 2, 2012

Difference between Action, Procedure, Function and Sub?

I believe that many QTP newbie’s are confused about Action, Procedures, Function and Subs. Am I right?

No worries! Here we are going to discuss all of them in detail to eliminate all the confusions!

In QTP, there are two ways (on broad level); we can break up the code into logical units. Logical unit is nothing but a 'Piece of code' or ‘a series of VBScript statements’, to perform specific activity in QTP. These two ways are -

1. Actions - specific to QTP
2. Procedures - vbscript provided


Okie.. but what about Functions and Subs? Wait guys! we'll come there.

We know that we use vbscript as scripting language in QTP. 


VBScript has two kinds of procedures: Sub procedure and Function procedure

So, in brief, its goes like this..



1. Actions
2. Procedures
   2.1. Subs/Subroutine
   2.2. Function

Now we know that Action and Procedures are the two things. Procedure is further devided in two types - Function & Sub.

Feeling batter? Great!!

So, Lets start with actions..

1. QTP Actions:-

Action is specific to QTP and not the part of vbscript. Every QTP test has at least one Action(default name is Action1). 
Action can have an object repository associated with it. Action can return multiple values in form of 'output parameters'.

2. Procedures:
2.1. VBScript Sub Procedures:-

A Sub procedure:

  • is a series of statements, enclosed by the Sub and End Sub statements
  • can perform actions, but does not return a value
  • can take arguments
  • without arguments, it must include an empty set of parentheses ()

Sub mysub()
  Print "my Sub Procedude"
End Sub

or

Sub mysub(argument1,argument2)
  Print "my Sub Procedure"
End Sub

How to call Sub Procedures:

To call a Sub, you will use Call statement by enclosing arguments (if any) in parentheses.


The Call statement is not necessary to call a Sub, but if you want to use Call statement (Recommended), you must enclose arguments (if any) in parentheses.

Call mysub(argument1,argument2)

You can call a Sub without using Call statement as well, but not recommended.

mysub argument1,argument2


2.2. VBScript Function Procedures:-

A Function procedure:

  • is a series of statements, enclosed by the Function and End Function statements
  • can perform operations and can return a value
  • can take arguments that are passed to it by a calling procedure
  • without arguments, must include an empty set of parentheses ()
  • returns a value by assigning a value to function name itself

Function myfunction1()
  Print "my fuction1"
End Function

or

Function myfunction2(a,b)
myfunction2=a+b  'assign value to function name
End Function


How to call Function Procedures:

Call myfunction1() 'calling 1st function, without any return value

abc=myfunction2(argument1,argument2)  'calling 2nd function, with a return value

Here you call a Function called "myfunction2", the Function returns a value that will be stored in the variable "abc".

We can either use actions or function to return multiple value.. but in case of function, you need to store all the output values to an array and return that array, bcoz function can return only one value. Refer the below code..

Function myfunction2(a,b)
val_sum = a+b
val_sub = a-b
val_div = a/b
arr=Array(val_sum,val_sub,val_div)
myfunction2=arr 'assign value to function name
End Function

x= myfunction2(15,3)

msgbox x(0)
msgbox x(1)
msgbox x(2)