Menu Bar of the blog

Sunday, 2 November 2014

Tutorial#2.1: VB Scripting Fundas Part-I

Why does UFT use VB Script as a language?
   VB Script is a very powerful language made by Microsoft. Since, Microsoft has also made Windows, so it has provided full compatibility between VB language and Windows. Anything in Windows can be handled like Browsers or Windows Applications, using VB language. For Eg.: A simple notepad file can be made with .vbs extension with the code - Msgbox "Hello". On double clicking this file, a message is displayed saying "Hello", as expected. Similarly, if we want to open a browser or perform any other operation on Windows it can be simply done by a .VBS file. This is the reason why VB Script is used for automating anything in Windows through UFT.
Apart from VB, there are languages like JAVA Script which is very useful with Browsers. And languages like Ruby which is very useful with SOAP UI.
The complete automation scripts can be written in these .vbs files, but we use UFT as it contains in-built libraries which eases our tasks. We can give the same command msgbox "Hello" in UFT screen and on running the script it gives the same output.

Running a Script and Storing Results at Temporary Location:-
On running UFT, a window is shown in which we can select the option to store the results at a Temporary location, so that our hard disk space is not used unnecessarily.


Disabling popping up of Run Results Viewer after execution -
As the execution is complete, Run Results Viewer is displayed. We will explore this Window, but as of now since we are not aiming to analyze this Window so we can disable it by:-
Step 1: Go to Tools -> Options
Step 2: Go to Run Sessions
Step 3: Uncheck 'View Results when run session ends.'

Variables and Constants -
Now, coming back to our script in UFT, the statement written in UFT window - 'Msgbox "Hello"', contains Msgbox in blue color. It shows that 'Msgbox' is a reserved word in VBScript. There are some other reserved words like InputBox, Set etc. Coming to the second part of the statement, it says "Hello". The double quotes are important here, which shows that this is a string and should be displayed as it is, since it is being used as a Constant. If we remove double quotes then it becomes a variable which may have some value.
In VB, we do not need to declare variables separately. For Eg: If we write,
a = 100
msgbox a
a = "We are learning."
msgbox a
The output will be as expected, "100" and then "We are learning". So, in VB, we don't have to declare a variable as an integer or a string. VB takes care of this by itself.

The Command- Run From Step:-
Another point to note while execution of scripts in UFT is - We can start the execution from any line by moving the cursor to that line and pressing Ctrl+F5 or by right clicking on that line in UFT Window and selecting the option to Run From Step. The same cannot be done in a .vbs file.

Commenting a code -
Simply put a single quote (') in front of a line to comment it. To comment multiple lines, select all the lines and press Ctrl+M and to uncomment them press Ctrl+Shift+M. For Eg:-
'a = 100
'msgbox a

Print Command -
Msgbox stops UFT execution at one place, and the execution is stopped until we press Ok. Hence, a Print command is used. On using this command, the complete output during execution of the script is shown at the end in the Output panel. If Output panel is not displayed by itself, it can be enabled from View tab on UFT window.


InputBox Command -
Write InputBox on UFT screen and give a space, following tag is shown. So, it is a feature of UFT to show these tags when a known command is used. For InputBox, as we can see prompt field is mandatory and rest are optional.

If we write,
inputbox "Enter your name", "My Window"; then following window is shown to the user for getting his inputs.

To capture input by the user, following command can be used:-
a = inputbox("Enter your name", "My Window")
msgbox a
Why brackets were used in this case with inputbox, will covered in next module.

Concatenation Operator (&) -
a = inputbox("Enter your name", "My Window")
msgbox "Welcome" & a
In some languages, '+' is used, but in VB concatenation operator is '&'. For Eg:
x=100
y=400
msgbox x+y, returns 500.
and msgbox x & y, returns 100400.

How to handle long strings in UFT - 
Suppose my string is - "We have installed UFT 12.00 and have started to learn UFT. Uptill now, we have covered msgbox, inputbox commands and concatenation operator". If we want UFT to write this statement in multiple lines but treat it as one string then the operator to be used is - "&_". So, the string will be defined as -
str = "We have installed UFT 12.00 and have started to learn UFT. "&_
"Uptill now, we have covered msgbox, inputbox commands and concatenation operator".
UFT will treat str as one string.

vbcrlf Command - 
vbcrlf - It is used to separate 2 lines.
print "We have installed UFT 12.00" & vbcrlf & "We have started to learn UFT."
The output will be in separate lines:-
We have installed UFT 12.00
We have started to learn UFT.

Other topics on VB Scripting Fundas are covered in Next Tutorial -
Tutorial#2.2: VB Scripting Fundas Part-II

No comments:

Post a Comment