Menu Bar of the blog

Wednesday, 5 November 2014

Tutorial#3.3: Error Object in VbScript

An Object is an instance of a Class. It consists of methods and properties. We can create our own objects or we can use the existing objects in VB.

Consider the following statements,
Print "a"
Print CInt("a3")
Print "b"

The above code will stop the execution at the second step. But, what if we do not want the execution to stop?
For this, we use the statement - 'On Error Resume Next'. This statement tells UFT that even if an error appears, then execution won't stop. Hence, the code will become:-
On Error Resume Next
Print "a"
Print CInt("a3")
Print "b"

Err Object
Now, the code will execute completly without stopping in between. But, we shall report that the error was occured during execution. For this, we use Err object and its Description property.
On Error Resume Next
Print
"a" & Err.number
Print CInt("a3")
Print
"b" & Err.number
If Err.Number<>0 Then
    msgbox Err.Description
End If
Output:
a0
b13
Type Mismatch
Hence, the Error value changes from 0 to something other than 0, if an error occurs. And, the Error  Description - 'Type Mismatch' is shown.

Clearing an Error from memory -
To clear the Error from the memory, we use, Err.Clear. Using Err.Clear method, Err object is reset.
So, if we write Err.Clear after above lines and try printing the Err.number again then it will return 0.

Raising an Error Deliberately -
At times we need to raise an error deliberately. For this we use the following commands,
On Error Resume Next
Err.raise 10
print err.Description

Output: "The array is fixed or temporarily locked."
Hence, an error message corresponding to error number 10 is displayed."
More details on Err object can be studied from UFT Help as well.

=================**********************===================

Tuesday, 4 November 2014

Tutorial#3.2: Sub-routines and In-built Functions in VB Script

Sub-Routines are similar to functions but they cannot return any value. For Eg:-
sum 3,4
Sub sum(a, b)
    msgbox a+b
End Sub

There are a number of in-built functions in function libraries of VB. The complete list is given at - UFT Help -> VB Script -> VB Script Language Reference -> Functions. Over here, we are going to cover few of the important in-built functions:-
  1. Left
  2. Right
  3. Mid
  4. Split
  5. UCase/LCase
  6. ltrim, rtrim, trim
  7. cint, cstr
  8. isnumeric
  9. date
  10. time
  11. now
  12. datevalue
  13. datepart
  14. datediff
  15. hour
  16. min
  17. sec
Left - It is used to return left part of a string.
str = "Yes!! Today is Friday!!"
msgbox left(str, 5)

Output: Yes!!

Mid - It is used to return middle part of a string.
str = "Yes!! Today is Friday!!"
msgbox mid(str, 7,5)

Output: Today
Hence, syntax of mid is - mid(<string>, <start>, [optional] length)

Right - It is used to return Right part of a string.
str = "Yes!! Today is Friday!!"
msgbox Right(str, 8)

Output: Friday!!


Split - It is used to split an expression into various elements. And, it stores the elements of the expression in the form of an array. Eg:-
str = "Yes!! Today is Friday!!"arr = split(str, " ")     ' Here, space in the second parameter acts as a Delimiter.
For Iterator = 0 To ubound(arr)
    Print arr(iterator)  
Next

Output: Yes!!
Today
is
Friday!!


UCase and LCase - They are used to convert the case of a string to upper or lower.
Print ucase("abcdEFGH")
Print lcase("abcdEFGH")

Output: ABCDEFGH
abcdefgh

LTrim, RTrim and Trim - LTrim removes the blank spaces from left hand side, RTrim removes the blank spaces from right hand side and Trim removes the blank faces from both the sides.

IsNumeric, CStr and CInt - IsNumeric function tells us whether a string is numeric or not. And, CInt converts a string to an integer. Similarly, CStr is used to convert an Integer to a string.
= "44"
= "33"
If IsNumeric(a) and isnumeric(b) Then
    msgbox CInt(a) + CInt(b)  
End If

Output: 77.

a = 124
msgbox CStr(a)

Date, Time and Now - Date retruns Date, Time returns Time and Now returns both.
msgbox Date
msgbox Time
msgbox Now

msgbox datevalue("10 november, 2013") - This returns date in mm/dd/yyyy format.

How to find Date difference between 2 given dates?
For this, there is an in-built function called as dateDiff.
d1 = dateValue("november 10, 2010")
d2 = dateValue("november 10, 2011")
msgbox datediff("d", d1, d2) - Output: 365 - so, dateDiff returns difference in days.
msgbox datePart("d", d1) - Output: 10 - retruns date part of a date.
msgbox datePart("m", d1) - Output: 11 - returns month part of a date.


TIME - 
print time - Returns current time.
print hour(time) - Returns hour part of time.
print minute(time) - Returns minute part of time.
print second(time) - Returns second part of time.
Output:
11:53:27 PM
23
53
27
==============***********************===============

Monday, 3 November 2014

Tutorial#3.1: Arrays and Functions in VB Script

Arrays constitute an important part of VB Script. They divide the memory space into equal parts. The below diagram depicts an array with 6 elements. First element of the array is at 0th position.
In VB, each element of an array can be of any data type, for eg, the below array x contains integers, string, boolean and decimal.
Dim x(5)    'Declaring an array with 6 elements.
x(0) = 10
x(1) = 22
x(2) = "Hi All"
x(3) = true
x(4) = 123.64
x(5) = 13


To find size of an array -
print "Size of the Array:" & ubound(x) + 1.
Output: 6.
Hence, ubound() method returns Largest subscript of a given array.
To print an element of the array:
print x(2)
Output: Hi All

To iterate an array -
For i = 0 To ubound(x)     
   print x(i)
Next


To iterate an array in reverse order -
For i = ubound(x) To 0 Step -1     
   print x(i)
Next


One of the practical uses of arrays in automation is:-
If we need to fetch a large amount of data from browsers and store them in the form of variables, then arrays can be used. Instead of making numerous variables, we can store the values in different elements of an array.

Re-dimensional Arrays:-
Arrays are redimensional, that is, their dimension can be changed at run-time.
Dim y()
ReDim y(2)     'Redimensional array with size 2.
y(0) = 10
y(1) = 21
y(2) = 32
ReDim Preserve y(4)
y(3) = 43
y(4) = 58
For i = 0 To ubound(y)
    print y(i)
Next

In the above code, an array 'y' is declared as re-dimensional with size 2. Redim keyword represents that the size of the array can be changed at a later stage.
In the highlighted statement, dimension of the array is changed to 4 with a Preserve keyword. Here, preserve keyword represents that already existing elements of the array will remain preserved while other elements are being added. Hence, dimension of the array is extended from 3 to 5, while keeping the existing elements preserved.
So, the Output will be:-
10
21
32
43
58

Functions in VB Script -
Hence, a function is something which can accept any number of values from 0 to n and can return either a single value or no value at all. The below example explains how a function is invoked, declared and defined; and how we can return a value through a function.
Writing a function to find sum of first n numbers-
sum = 0
Function sumFirstN(n)       'Declaring and defining a function.
    For i = 0 To n
        sum = sum + i     
    Next
    sumFirstN = sum    'A function can return a value by assigning function name to the value to be returned.
End Function
msgbox "Sum of first 10 numbers:" & sumfirstN(10)   ' SumFirstN() function is called with 10 as a parameter.


Note:-
  1. Scope of a variable inside a function is always the function in which it is defined. Out of the function, the variable doesn't exist.
  2. Whenever a return value is expected from a function, then brackets are used while calling the function. Eg: Function call - sumfirstN(10
==================*********************=====================

UFT Code#2: To execute UFT from a .vbs file

As explained in earlier posts, a .vbs file can be used to perfrom any operation on Windows like, handling a Windows application or working on a browser or running a UFT script.
To invoke and run a particular test on UFT, from an External vbs file:-
  1. Copy the below function in a notepad and save the file with .vbs extension.
  2. To run the program, double click the saved .vbs file.
runTestOnUFT()
 Function runTestOnUFT()
 set qtApp = CreateObject("QuickTest.Application")
 qtApp.Launch
 qtApp.Visible =True
 qtApp.Open <projectPath>
 Set qtTest = qtApp.Test
 qtTest.Run
 qtTest.Save
 qtTest.Close
 qtApp.quit
End Function


====================*************************************====================

Sunday, 2 November 2014

Tutorial#2.2: VB Scripting Fundas Part-II

Option Explicit
It specifies VB Script to make sure that a variable is declared before it is defined.
So, the following statement will throw an error:-
Option Explicit
a = 100
It should be written as:-
Option Explicit
Dim a 'Declaring the variable.
a = 100

Syntax of 'If' statement -
'To find greatest of 3 numbers.
a = 100
b = 200
c = 300
If a>b and b>c Then
print "a is greatest."
elseif b>c Then
print "b is greatest."
else
print "c is greatest."
End If

While Loop Syntax - 
While i<10
print i
i = i + 1 'If this line is missed then program will go in an infinite loop.
Wend

For Loop Syntax -
For x = 1 To 10 Step 1 'Step 1'- means the value of x will increment by 1 in each iteration. 
print x
Next
To get numbers in reverse order,
For x = 10 To 1 Step -1
print x
Next

Debugging
Shortcut key - F8.
Breakpoints are used to debug a given section of code to analyze the values/state of variables at that point. At the Breakpoint, when the execution is stopped, we can see the value of a variable in Local Variables tab. Or, we can select a variable and Add to Watch to see its value in Watch section.
At the Breakpoint, when the execution is stopped, if we click F10, then the execution goes line by line. And, if we click F5, then the execution will go on normally.

Nested For Loop -
'To print tables from 1 to 20.
For tableNum = 1 To 20 Step 1
print "Table for " & tableNum
For x = 1 to 10 Step 1
print tableNum & "*"& x & "=" & tableNum*x
Next
Next

This completes the VBScript Fundas required to proceed with the course.
Link to the next module: Module#3: Arrays, Functions, Sub-routines and Objects in VB Script 

==================***********************=====================

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

Saturday, 1 November 2014

Module#1.1: Download and Install UFT 12.00

The basic concepts of both the tools - QuickTest Professional (QTP) and Unified Functional Testing (UFT) are the same. Till version 11.0, the term QTP was referred but for latest versions the name was changed to UFT. So, we will be using the term UFT from hereon until it is specifically required to differentiate between the two.

What is Test Automation?
In today's IT world, when software keeps changing on day to day basis, and with Agile methodology being adopted in more and more projects, it is highly required that regression testing is performed after every turn or deployment. (Regression Testing refers to testing a software to assure that change in one module of a code has not effected other modules of the software.)
So, if there are 1000 Test Cases which remain the same and needs to be executed after every turn then Test Automation is used. It is carried out with the help of software tools like UFT or Selenium.

Unified Functional Testing
This tool is being owned and marketed by HP. It is used to carry out Test Automation. Before explaining further about the tool, lets see how to download and install the software.

Downloading UFT -
Step 1: Go to Google. Enter Download UFT.
Step 2: Go to first link provided by google, that is, on hp.com site.
Step 3: Click on Free Trial.
Step 4: Fill up the form and you will get the download link on mail.
The tool is aorund 3 to 4 GB.

Installing UFT -
Step 1: Go to UFT 12 folder created after download.
Step 2: Double click setup.exe. HP UFT 12.00 screen is displayed.
Step 3: Select the first link - Unified Functional Testing Setup.
Step 4: On clicking first link, before the installation of UFT, following 3 other software tools are installed on your machine:-
  1. Ms Office Access database engine 2010.
  2. Ms Visual C++ 2012 Redistributable
  3. Ms Visual C++ 2012 Redistributable x64
Step 5: After installation, list of Add-ins that can be installed is shown. By default, there are 4 Add-ins which will be installed:-
  1. Samples
  2. ActiveX Add-in
  3. Visual Basic Add-in
  4. Web Add-in
Other Add-ins can be installed at a later stage too.
Step 6: Click install to install the selected Add-ins.
Step 7: Once it is complete, UFT icon is shown on Desktop.
Step 8: Under All Programs, in HP Software, various tools can be seen which got installed with UFT.
Step 9: One of the tool is - Additional Installation Requirements. Select this tool to open. Select first 3 options to configure IE and DCOM setting for ALM Integration and Automation Scripts, and click Run. This step is necessary for UFT to work properly.
Step 10: Double click UFT. License Warning is shown. Click continue and install the default Add-ins.
HP Unified Functional Testing Window is shown, as below.

UFT will start recording the scripts once User Account Contral Settings are set on the system.
Now, how can we set these settings? - By turning OFF user Account control settings.
How to do so?
Step1: Open User Account Control Settings in Windows.
Step2: Turn it OFF, by selecting Never Notify.

Advantages of UFT, in brief -
1. VB Script used by UFT is an easy programming language.
2. Time to develop a script in UFT is much less.

Disadvantages of UFT, in brief-
It is a Paid Tool, whereas, tools like Selenium are open-source.

We will go to further details on UFT in coming posts..

Next Article: Module#2: VB Script Fundas