Friday, January 25, 2013

Procedures


The grouping of the lines of code to execute it repeatedly can be broadly classified as Procedures.
In QTP, we have two different types of procedures – Sub Procedures and Functions.

Executing a Procedure in QTP

Any Procedure (Function/Sub) in VBScript has two main aspects which are mandatory for executing it in VBScript or for that matter any other programming language. These are -
  • Function (Sub) Definition or Declaration: Function Definition is the actual code that you want to run as part of the function
  • Function Call: This is a statement which executes your Function or Sub
Let’s see an example of this where we would create a function to find the sum of two numbers and display the result.

Sample Code 1: Structure of a Procedure (Function & Sub)
'Structure for Sub
'====================

'1) Sub Definition - Actual Code
Sub fnSum()
  var1 = 5 
 var2 = 10
  sum = var1 + var2
  msgbox "The Sum of Numbers is: " & sum
End Sub

'2) Executing Sub
Call fnSum() 'Call keyword is not mandatory to execute a Sub
fnSum()

'Structure for Function
'======================

'1) Function Definition - Actual Code
Function fnSum()
  var1 = 5 : var2 = 10
  sum = var1 + var2
  msgbox "The Sum of Numbers is: " & sum
End Function

'2) Executing Function
Call fnSum() 'Call keyword is not mandatory to execute a Function
fnSum()
In the above example you can see that Call statement can also be used while calling a function/sub. To know more about Call statement you can check the difference between Call statement and normal function call.

Subs in QTP

A Sub Procedure in QTP -
  • - is a collection of statements that are enclosed between Sub and End Sub statements.
  • - can be used both with and without parameters.
  • - does not return any value.

Passing Parameters in a Sub Procedure

Consider a situation where you want to run the same set of code multiple times but with different set of data. In such a case, you can pass the data to your Sub in the form of parameters. This way you can make the code generic which can work with multiple set of data. Let’s see an example for the same.
Sample Code 2: Using Parameters in a Sub
Dim v1, v2
v1=5 : v2=10

fnSum v1, v2 'Passing parameters using variables
fnSum 10, 20 'Passing parameters as literals
fnSum v1+10, v2*2 'Passing parameters as expression

'Sub Definition
Sub fnSum(var1, var2)
  sum = var1 + var2
  msgbox "The sum of numbers is: " & sum
End Sub

Functions in QTP

A Function in QTP -
  • - is a set of statements that are enclosed between Function and End Function statements.
  • - can be used with and without parameters
  • - can return a value.
The difference between a function and a sub is that a function can return a value, but a sub can’t return any value.

Passing Parameters in a Function

You can pass parameters in a function the same way it’s done for a sub. Let’s see an example for this.
Sample Code 3: Using Parameters in a Function
Dim v1, v2
v1=5 : v2=10
fnSum v1, v2 'Passing parameters using variables
fnSum 10, 20 'Passing parameters as literals
fnSum v1+10, v2*2 'Passing parameters as expressions
'Function Definition
Function fnSum(var1, var2)
  sum = var1 + var2
  msgbox "The sum of numbers is: " & sum
End Function

Returning a value from a Function in QTP

One additional advantage that Functions have over Sub is that a Function can return a value. To return a value from a function, we need to take care of the following two things -
  • 1) To return a value from a function, you need to use the statement functionName = ReturnValue, where functionName is the actual name of the function and ReturnValue is the value you want to return.
  • 2) To capture the returned value, you need to use the statement someVariable = functionName() while calling the function.
Let’s understand this with the help of an example.

Sample Code 4: Returning value from a Function
Dim result 'variable that will capture the result

result = fnSum(10, 20) 'parameters should be passed using parenthesis when the function returns a value
msgbox "The sum of the numbers is:  " & result

'Function Definition
Function fnSum(var1, var2)
  sum = var1 + var2
  'return the result
  fnSum = sum
End Function

Passing Parameters to a Function/Sub – Pass By Value & Pass By Reference

You can pass parameters to a function or sub procedure by value or by reference. Let’s see what both these terms mean.

Passing Parameters by Value (byVal). With this way of passing parameters, only a copy of the original parameters is passed. This means that whatever modifications we make to the parameters inside the function, it doesn’t affect the original parameters.

Sample Code 5: Passing parameters by value to a function
Dim val
val=5

'Function Call
fnFunc val
msgbox "Original Value: " & val 'msgbox displays value 5

'Function Definition
Function fnFunc(byVal val)
  val = val + 2
  msgbox "New Value: " & val 'msgbox displays value 7
End Function
In the above example you would see that the new value get changed to 7 but it doesn’t get reflected to the original value which still shows the value as 5 only.


Passing Parameters by Reference (byRef). In this case, the reference of the original value is passed as the parameter to the function. Therefore, whatever change is done to the parameter inside the function, the same is reflected in the original parameter also. By default, values are passed by reference in a function. i.e., even if you don’t use byRef keyword, the parameters are passed by reference.

Sample Code 6: Passing parameters by reference to a function
Dim val
val=5
'Function Call
fnFunc val
msgbox "Original Value: " & val 'msgbox displays value 7
'Function Definition
Function fnFunc(ByRef val)
  val = val + 2
  msgbox "New Value: " & val 'msgbox displays value 7
End Function
Since the original parameter is passed as reference to the function, both the original and new value has the updated value 7.

Thursday, January 24, 2013

Regular Expressions in QTP

A regular expression is a string that describes or matches a set of strings. It is often called a pattern as it describes set of strings.

Given underneath is one of the most widely used and ever confused BackLash character. The remaining expressions are serialized below that.

Using the Backslash Character
A backslash (\) instructs QuickTest to treat the next character as a literal character, if it is otherwise a special character. The backslash (\) can also instruct QuickTest to recognize certain ordinary characters as special characters. For example, QuickTest recognizes \n as the special newline character. 
For example:
w matches the character w
\w is a special character that matches any word character including underscore
For example, in QTP, while entering the URL of a website,
http://mercurytours.mercuryinteractive.com
The period would be mistaken as an indication of a regular expression. To indicate that the period is not part of a regular expression, you would enter it as follows:
mercurytours\.mercuryinteractive\.com Note: If a backslash character is used before a character that has no special meaning, the backslash is ignored. For example, \z matches z.

Expressions & Explanation
Special characters and sequences are used in writing patterns for regular expressions. The following describes the characters and sequences that can be used.


\
Marks the next character as either a special character or a literal. For example, "n" matches the character "n". "\n" matches a newline character. The sequence "\\" matches "\" and "\(" matches "(".

^
Matches the beginning of input. 

$
Matches the end of input.

*
Matches the preceding character zero or more times. For example, "zo*" matches either "z" or "zoo".

+
Matches the preceding character one or more times. For example, "zo+" matches "zoo" but not "z".

?
Matches the preceding character zero or one time. For example, "a?ve?" matches the "ve" in "never".

.
Matches any single character except a newline character.

(pattern)
Matches pattern and remembers the match. The matched substring can be retrieved from the resulting Matches collection, using Item [0]...[n]. To match parentheses characters ( ), use "\(" or "\)".

xy
Matches either x or y. For example, "zwood" matches "z" or "wood". "(zw)oo" matches "zoo" or "wood".

{n}
n is a nonnegative integer. Matches exactly n times. For example, "o{2}" does not match the "o" in "Bob," but matches the first two o's in "foooood".

{n,}
n is a nonnegative integer. Matches at least n times. For example, "o{2,}" does not match the "o" in "Bob" and matches all the o's in "foooood." "o{1,}" is equivalent to "o+". "o{0,}" is equivalent to "o*".

{n,m}
m and n are nonnegative integers. Matches at least n and at most m times. For example, "o{1,3}" matches the first three o's in "fooooood." "o{0,1}" is equivalent to "o?".

[xyz]
A character set. Matches any one of the enclosed characters. For example, "[abc]" matches the "a" in "plain".

[^xyz]
A negative character set. Matches any character not enclosed. For example, "[^abc]" matches the "p" in "plain".

[a-z]
A range of characters. Matches any character in the specified range. For example, "[a-z]" matches any lowercase alphabetic character in the range "a" through "z".

[^m-z]
A negative range characters. Matches any character not in the specified range. For example, "[m-z]" matches any character not in the range "m" through "z".

\b
Matches a word boundary, that is, the position between a word and a space. For example, "er\b" matches the "er" in "never" but not the "er" in "verb".

\B
Matches a non-word boundary. "ea*r\B" matches the "ear" in "never early".

\d
Matches a digit character. Equivalent to [0-9].

\D
Matches a non-digit character. Equivalent to [^0-9].

\f
Matches a form-feed character.

\n
Matches a newline character.

\r
Matches a carriage return character.

\s
Matches any white space including space, tab, form-feed, etc. Equivalent to "[ \f\n\r\t\v]".

\S
Matches any nonwhite space character. Equivalent to "[^ \f\n\r\t\v]".

\t
Matches a tab character.

\v
Matches a vertical tab character.

\w
Matches any word character including underscore. Equivalent to "[A-Za-z0-9_]".

\W
Matches any non-word character. Equivalent to "[^A-Za-z0-9_]".

\num
Matches num, where num is a positive integer. A reference back to remembered matches. For example, "(.)\1" matches two consecutive identical characters.

\n
Matches n, where n is an octal escape value. Octal escape values must be 1, 2, or 3 digits long. For example, "\11" and "\011" both match a tab character. "\0011" is the equivalent of "\001" & "1". Octal escape values must not exceed 256. If they do, only the first two digits comprise the expression. Allows ASCII codes to be used in regular expressions.

\xn
Matches n, where n is a hexadecimal escape value. Hexadecimal escape values must be exactly two digits long. For example, "\x41" matches "A". "\x041" is equivalent to "\x04" & "1". Allows ASCII codes to be used in regular expressions.

Tuesday, January 15, 2013

Identifying Right Test Cases for Automation


It is impossible to automate all test cases, the first step to successful automation is to determine what test cases should be automated first.The benefit of automated testing is correlated with how many times a given test can be repeated.Tests that are only performed a few times are better left for manual testing. Good test cases for automation are those that are run frequently and require large amounts of data to perform the same action.You can get the most benefit out of your automated testing efforts by automating:


  1. Repetitive tests that run for multiple builds
  2. Tests that are highly subject to human error
  3. Tests that require multiple data sets
  4. Frequently-used functionality that introduces high risk conditions
  5. Tests that are impossible to perform manually
  6. Tests that run on several different hardware or software platforms and configurations
  7. Tests that take a lot of effort and time when doing manual testing.

Sunday, January 13, 2013

Recovery Scenario Manager


Recovery scenario is used to handle unexpected situations that might occur while the test is running like exceptions and Runtime Errors.

Recovery Scenario Manager provides a wizard that guides you through the process of defining a recovery scenario, which includes a definition of an unexpected event and the operations necessary to recover the run session. The Recovery Scenario Mechanism enables you to instruct Quickest to recover from unexpected events and errors that occur in your testing environment during a run session.

We can handle pop-up window, object state, test run error, application crash exceptions and many more. Recovery Scenario is used only for unpredictable events or events that you cannot synchronize with a specific step in your component. Predictable events and errors can be handled with user defined functional and conditional statements but in case of unpredictable events we need to rely on Recovery Scenarios.

A recovery scenario has three major steps:
  1. 1.Trigger Event: The event that interrupts your run session. E.g., a window that may pop up on screen, or a Quick Test run error, Object State or Application Crash.
  2. 2.Recovery Operations: The operations to perform to enable Quick Test to continue running the test after the trigger event interrupts the run session. For example, Keyboard or Mouse Operation (clicking an OK button in a pop-up window), Close Application Process, Function call or Restarting Microsoft Windows.
  3. 3.Post-Recovery Test Run Operations: The instructions on how Quick Test should precede after the recovery operations have been performed, and from which point in the test Quick Test should continue, if at all. For example, you may want to restart a test from the beginning, or skip a step entirely and continue with the next step in the test.


Recovery scenarios are saved in recovery scenario files. A recovery scenario file is a logical collection of recovery scenarios, grouped according to your own specific requirements.
Extension for recovery scenario files is .qrs

Saturday, January 12, 2013

How to handle dynamic objects in QTP?


QTP has a unique feature called Smart Object Identification/recognition. QTP generally identifies an object by matching its test object and run time object properties. QTP may fail to recognize the dynamic objects whose properties change during run time. Hence it has an option of enabling Smart Identification, wherein it can identify the objects even if their properties changes during run time.

If QuickTest is unable to find any object that matches the recorded object description, or if it finds more than one object that fits the description, then QuickTest ignores the recorded description, and uses the Smart Identification mechanism to try to identify the object.
While the Smart Identification mechanism is more complex, it is more flexible, and thus, if configured logically, a Smart Identification definition can probably help QuickTest identify an object, if it is present, even when the recorded description fails.

The Smart Identification mechanism uses two types of properties:
 
Base filter properties – The most fundamental properties of a particular test object class; those whose values cannot be changed without changing the essence of the original object. For example, if a Web link’s tag was changed from to any other value, you could no longer call it the same object.


Optional filter properties – Other properties that can help identify objects of a particular class as they are unlikely to change on a regular basis, but which can be ignored if they are no longer applicable.

Friday, January 11, 2013

Virtual Objects in QTP

Virtual Objects comes into picture if QTP is unable to recognize an object and we often see errors as Object not found. This is because even though you have recorded the actions during playback time QTP is unable to recognize an object and because of which the script fails.


Virtual object enable you to create and run tests on objects that are not normally recognized by QuickTest. Your application may contain objects that behave like standard objects but are not recognized by QuickTest. You can define these objects as virtual objects and map them to standard classes, such as a button or a check box. QuickTest emulates the user's action on the virtual object during the run session. In the test results, the virtual object is displayed as though it is a standard class object.


You define a virtual object using the Virtual Object Wizard (Tools > Virtual Objects > New Virtual Object). The wizard prompts you to select the standard object class to which you want to map the virtual object. You then mark the boundaries of the virtual object using a cross-hairs pointer. Next, you select a test object as the parent of the virtual object. Finally, you specify a name and a collection for the virtual object. A virtual object collection is a group of virtual objects that is stored in the Virtual Object Manager under a descriptive name.


Virtual Objects in QTP are created to resolve the object recognition problems in QTP. When an area of an application is not recognized by QTP we use the Virtual Object Wizard to map the area to a standard Class. These virtual objects are generally used to resolve the object recognition problems. All the Virtual Objects created are stored in the Virtual Object Manager. After we have learned an object as a Virtual Object we can record on that object successfully. You can create a Virtual Object by navigating to Tools, Virtual Objects, and New Virtual Object.

What is Virtual Object Collection?
 A virtual object collection is a group of virtual objects that is stored in the Virtual Object Manager under a descriptive name.


How to disable virtual Objects while recording?

In order to disable the Virtual Objects Navigate

Go to Tools-->Options--> General Tab--> Disable Recognition of virtual objects while recording

Check and uncheck this option to disable or enable virtual objects while recording.


Storage location of Virtual Objects

 If you create any virtual objects automatically those objects will be stored in

“QuickTest installation folder\ dat \ VoTemplate”


Extension of virtual objects file

 .VOT


How to use virtual objects on different machines?

After creation of virtual objects copy QuickTest installation folder\ dat \ VoTemplate Folder to other machines on which you want to use virtual objects.


Limitations and drawbacks of Virtual Objects

Ø  QuickTest does not support virtual objects for analog or low-level recording.

Ø  Not possible to apply a checkpoint on a virtual object

Ø  Not possible to add virtual objects using Object Repository

Ø  Virtual Objects doesn’t support all objects and methods.

Ø  We cannot use object spy on a Virtual Object.

Ø  We can only record on Virtual Objects.

Ø  Scroll Bars and Labels cannot be treated as Virtual Objects.

Ø  May not run perfectly on different screen resolutions if a test using Virtual Objects.

Ø  Virtual object uses the properties Name, Height, Width, X, Y which the properties are having maximum possibilities for frequent change.

Ø  During a run session, make sure that the application window is the same size and in the same location as it was during recording, otherwise the coordinates of the virtual object relative to its parent object may be different, and this may affect the success of the run session.

Ø  You can use virtual objects only when recording and running a test. You cannot insert any type of checkpoint on a virtual object, or use the Object Spy to view its properties.

Ø  To perform an operation in the Active Screen on a marked virtual object, you must first record it, so that its properties are saved in the test object description in the object repository. If you perform an operation in the Active Screen on a virtual object that has not yet been recorded, QuickTest treats it as a standard object.


When Object recognition Problem occurs in QTP during playback on the Application Under test there are ways to handle this Object Recognition Problem. It depends solely on the user as to what they want to use.

The different ways to handle Object Recognition Problem are:

1. By Creating Virtual Objects.
2. Using Low Level Recording.
3. Using Analog Recording.

Wednesday, January 9, 2013

QTP Automation Object Model (AOM)


Automation Object Model is a collection of objects, methods and properties. This collection is used for performing quick test operations. Any operation described in QTP interface can be performed throughout automation object model.

Every option in QTP interface have objects, methods and properties are exposed by Quick Test Automation Object Model along with standard programming elements like control structures.

You can use the QuickTest Professional Automation Object Model to write programs that automate your QuickTest operations. The QuickTest Automation Object Model provides objects, methods, and properties that enable you to control QuickTest from another application.

The new QuickTest Professional Automation Object Model enables you to automate test management.
You can now control virtually every QuickTest feature and capability using the objects, methods and properties included in the QuickTest Professional Automation Object Model. Automation scripts make it easy to perform any QuickTest operation multiple times in multiple tests without having to open the QuickTest application, for example,
  • You can write a script that modifies the test object description properties in the Object Identification dialog box and performs an update run on all tests in a specified file folder.
  • After installing a new add-in, an automation script can associate this add-in to all relevant tests.
  • You can write an automation script to run a selected batch of tests. For each test, you can retrieve the associated add-ins list. Then, if the necessary add-ins are not already loaded, you can close QuickTest, load the necessary add-ins, reopen QuickTest, and run the test.
  • You can define your settings for a test in QuickTest, then click “Generate Script” in the Generate tab of the Test Settings dialog box to generate an automation script based on the current test settings. You can then apply those same settings automatically to multiple tests using the whole automation script or excerpts from the generated file.
Example:
You can create and run an automation program from Microsoft Visual Basic that loads the required add-ins for a test, starts QuickTest in visible or minimized mode, opens the test, configures settings that correspond to those in the Options, Test Settings, and Record and Run Settings dialog boxes, runs the test, and saves the test. 


Dim App 'As Application
Set App = CreateObject("QuickTest.Application")
App.Launch
App.Visible = True
App.Test.Settings.Launchers("Web").Active = False
App.Test.Settings.Launchers("Web").Browser = "IE"
App.Test.Settings.Launchers("Web").Address = "http://www.gmail.com "
App.Test.Settings.Launchers("Web").CloseOnExit = True
App.Test.Settings.Launchers("Windows Applications").Active = True
App.Test.Settings.Launchers("Windows Applications").Applications.RemoveAll
App.Test.Settings.Launchers("Windows Applications").RecordOnQTDescendants = True
App.Test.Settings.Launchers("Windows Applications").RecordOnExplorerDescendants = False
App.Test.Settings.Launchers("Windows Applications").RecordOnSpecifiedApplications = True
App.Test.Settings.Run.IterationMode = "rngAll"
App.Test.Settings.Run.StartIteration = 1
App.Test.Settings.Run.EndIteration = 1
App.Test.Settings.Run.ObjectSyncTimeOut = 20000
App.Test.Settings.Run.DisableSmartIdentification = False
App.Test.Settings.Run.OnError = "Dialog"
App.Test.Settings.Resources.DataTablePath = "<Default>"
App.Test.Settings.Resources.Libraries.RemoveAll
App.Test.Settings.Resources.Libraries.Add("D:\ENCRYPTED\LearningQTP\Functions\My Functions.qfl")
App.Test.Settings.Resources.Libraries.Add("D:\ENCRYPTED\LearningQTP\Variable\My_Constant_Variable.vbs")
App.Test.Settings.Web.BrowserNavigationTimeout = 60000
App.Test.Settings.Web.ActiveScreenAccess.UserName = ""
App.Test.Settings.Web.ActiveScreenAccess.Password = ""
App.Test.Settings.Recovery.Enabled = True
App.Test.Settings.Recovery.SetActivationMode "OnEveryStep"
App.Test.Settings.Recovery.Add "D:\Phoenix Automation\Recovery\Pop Up Window Explorer.qrs", "Pop Up Window Explorer", 1
App.Test.Settings.Recovery.Item(1).Enabled = True
App.Test.Settings.Recovery.Add "D:\Phoenix Automation\Recovery\fnSessionExpire.qrs", "fnSessionExpire", 2
App.Test.Settings.Recovery.Item(2).Enabled = True
App.Test.Settings.Recovery.Add "D:\Phoenix Automation\Recovery\Production Support Error - Recovery.qrs", "Production Support Error_Recovery", 3
App.Test.Settings.Recovery.Item(3).Enabled = True
App.Test.Settings.Recovery.Add "D:\Phoenix Automation\Recovery\Security Information Pop Up window.qrs", "Security Information Pop Up", 4
App.Test.Settings.Recovery.Item(4).Enabled = True
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' System Local Monitoring settings
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
App.Test.Settings.LocalSystemMonitor.Enable = false


Key Elements in the QuickTest Window

The QuickTest window contains the following key elements:
Ø  QuickTest title bar: Displays the name of the active document. If changes have been made since it was last saved, an asterisk (*) is displayed next to the document name in the title bar.
Ø  Menu bar: Displays menus of QuickTest commands.
Ø  Standard toolbar: Contains buttons to assist you in managing your test.
Ø  View toolbar: Contains buttons to view the various panes that can assist you in the testing process.
Ø  Tools toolbar: Contains buttons to assist you in setting options, checking syntax, and working with the Object Spy.
Ø  Automation toolbar: Contains buttons to assist you in the testing process.
Ø  Insert toolbar: Contains buttons to assist you in inserting steps, actions, checkpoints, and output values into your test.
Ø  Edit toolbar: Contains buttons to assist you in editing your test steps.
Ø  Debug toolbar: Contains buttons to assist you in debugging tests. The Debug Viewer pane contains the Watch Expressions, Variables, and Command tabs.





How QTP recognizes Objects in AUT

QuickTest stores the definitions for application objects in a file called the Object Repository. As you record your test, QuickTest will add an entry for each item you interact with. Each Object Repository entry will be identified by a logical name (determined automatically by QuickTest), and will contain a set of properties (type, name, etc) that uniquely identify each object. Each line in the QuickTest script will contain a reference to the object that you interacted with, a call to the appropriate method (set, click, check) and any parameters for that method (such as the value for a call to the set method). The references to objects in the script will all be identified by the logical name, rather than any physical, descriptive properties.


Concept of how QTP identifies object

During recording qtp looks at the object and stores it as test object.For each test object QT learns a set of default properties called mandatory properties,and look at the rest of the objects to check whether this properties are enough to uniquely identify the object. During test run,QT searches for the run time obkects that matches with the test object it learned while recording.

Tuesday, January 8, 2013

Main phase of QTP Testing process


The QTP testing process consists of following phases:

1) Create your test plan: Prior to automating there should be a detailed description of the test including the exact steps to follow, data to be input, and all items to be verified by the test. The verification information should include both data validations and existence or state verification's of objects in the application.

2) Recording a session on your application: As you navigate through your application, QTP graphically displays each step you perform in the form of a collapsible icon-based test tree. A step is any user action that causes or makes a change in your site, such as clicking a link or image, or entering data in a form.

3) Enhancing your test:
  • Inserting checkpoints into your test lets you search for a specific value of a page, object or text string, which helps you identify whether or not your application is functioning correctly. Checkpoints can be added to a test as you record it or after the fact via the Active Screen. It is much easier and faster to add the checkpoints during the recording process.
  • Broadening the scope of your test by replacing fixed values with parameters lets you check how your application performs the same operations with multiple sets of data.
  • Adding logic and conditional statements to your test enables you to add sophisticated checks to your test.

4) Debugging your test: If changes were made to the script, you need to debug it to check that it operates smoothly and without interruption.

5) Running your test on a new version of your application: You run a test to check the behavior of your application. While running, QTP connects to your application and performs each step in your test.

6) Analyzing the test results: You examine the test results to pinpoint defects in your application.

7) Reporting defects: As you encounter failures in the application when analyzing test results, you will create defect reports in Defect Reporting Tool.