Pages

Friday 12 July 2013

QTP Tutorial #24 – Using Virtual Objects and Recovery Scenarios in QTP Tests


Virtual Objects in QTP

Do you see Object not found error while running QTP tests? Well, this is because during playback QTP can’t recognize non-standard objects. To solve this object recognition problem we use Virtual Objects. Using Virtual Object Wizard we can map these unrecognized objects to standard class which then can be used as standard object to record the test.
How to solve object recognition problem in QTP?
Example of Virtual Object:
Here is a scenario: I am recording a test on a Microsoft word document. I activate the already opened MS word doc and I click on any of the icons in the top menu. For example, I click on “Format Painter”. The code that gets recorded into QTP is:
1
2
Window("Microsoft Word").WinObject("NetUIHWND").Click 132,120
Window("Microsoft Word").WinObject("NetUIHWND").Click 672,101
In cases like this we would go for a virtual object. By definition, a virtual object is an object that is recognized by QTP as non-standard but is instructed explicitly by the tester to behave like a standard object.
Virtual Object Wizard Steps:
Step #1) Go to the menu option “Tools->Virtual Objects-> New Virtual Object” and click “Next” in the following window.
Before you hit “Next” take a minute the read what this wizard will do.
Virtual Objects
Step #2) Here you will find a list of classes. You can choose any class depending on how the object in your application is behaving like. In our case, the “Format Painter” Icon is more like a button. So I am going to choose “Button” from the list.
Virtual Objects
Step #3) In this screen you can mark the screen where the object is on your AUT. Click “Mark Object” and choose the object from your AUT.
Virtual Objects
Step #4) The width and height values for the marked object will be populated once the selection is made. Hit “Next”
Virtual Objects
Step #5) You can now configure the way in which you would want the selected object to be recognized with reference to its parent. As you can see, you have a choice to see identify it based on its parent alone or the entire hierarchy. I am just going to keep the default values and click “Next”
Virtual Objects
Step #6) Give your virtual object a name and add it to a collection (nothing but a consolidated list of Virtual objects). I keep the default  values and click “Finish”
Virtual Objects
This completes the process for creation of a Virtual object.
Step #7) Go to “Tools->Virtual Objects->Virtual Object Manager”. Here you can see all the collections that are available and the objects within them.
Virtual Objects
Clicking on “New” will take you back to the creation process that we have just seen. You can delete a collection using the “Delete” button.
Once you are done creating the virtual object, repeat the recording process on your AUT for the same object. This is how the code looks:
1
Window("Microsoft Word").WinObject("NetUIHWND").VirtualButton("button").Click
Now you will be able to perform all the operations on this VirtualButton that you can on a standard button object.
A few points to note:
1) This feature is not available for Analog and low level recording modes.
2) From the example you can see that the virtual object completely relies on the width and height factors, so it is not highly reliable.
3) To disable QTP from recognizing the virtual objects while recording, choose the option “Disable recognition of virtual objects while recording’ under “Tools->Options->General”.
Virtual Objects

Recovery Scenario in QTP

At times when you are trying to login to your Gmail account, assume a pop-up window comes up and you will be asked to confirm your security information. This does not happen every time you login. If your test is to login to Gmail account and as soon as you enter the user ID, password, hit the Sign In button and if your QTP test is expecting to arrive at your inbox your test is going to fail if the security information screen comes up randomly.
To handle cases like this we use the ‘Recovery Scenarios”.
Steps to create Recovery Scenario in QTP:
Step #1) Go to “Resources -> Recovery scenario manager” , click on the “New Scenario” icon.
Recovery scenario
Step #2) Click Next
Recovery scenario
Step #3) The trigger for this to start could be one of the following options. Choose according to your scenario. In our case I will choose, Pop-up window. The other options are self explanatory.
Recovery scenario
Step #4) Using the “Pointed hand” option, choose the window that you would want to add.
Recovery scenario
Step #5) Define the recovery option by clicking on the “Next” icon below:
Recovery scenario
Step #6) Choose one from the list. I am going to choose “Keyword or mouse operation”. The options in this screen are really easy to understand. So choose accordingly.
Recovery scenario
Step #7) I am going to go with the default settings and click Next. The recovery operation gets added to the list. If you need to add more than one recovery operation you can keep the corresponding checkbox checked and click Next. It will take you back to the screen in Step number: 5. Or if you are done, you can simply uncheck the checkbox and click on “Next”. That is what I am going to do.
Recovery scenario
Step #8) Now you will have to define the post recovery operations.  All the options are as their names indicate. I am going to choose “Proceed to next step”. Click Next
Recovery scenario
Step #9) Enter the scenario name, description and click Next
Recovery scenario
Step #10) It provides a gist of your scenario. As you can see, there are 3 parts to a recovery scenario. The Trigger, Recovery operation and post recovery operations. You can choose to add this scenario to the current test or to all tests by choosing the relevant checkboxes. I am going to keep them unchecked at this point because I want to show how a tester can associate them to a test explicitly. Click “Finish”
Recovery scenario
Step #11) The scenario we just created will appear in the list. Save and close.
Recovery scenario
Step #12) Associating the recovery scenario. Open a test, in the “Resources” pane, right click on “Associated Recovery scenarios”, right click and choose “Associate recovery scenario”. Browse for the scenario and click “Add Scenario”. The chosen scenario will appear in the list in the Resources pane.
Recovery scenario
Step #13) Also, you can go to “File->Settings->Recovery” and add the scenarios you would like. Here you can also choose the options as to how often you would like it to run. You can choose to run it, On Error, On Every Step or Never.
Step #14) The extension for a recovery scenario file is “.qrs”
This concludes our discussion on Virtual Objects and Recovery scenarios. I would recommend the tester to use various combinations of Trigger, Recovery and post recovery operations when practicing the recovery scenarios.

No comments: