Friday, September 9, 2011

Recovery Scenario Manager in QTP

Recovery scenario manager is used to handle unexpected events/errors during the execution of QTP script.
This is the general definition which we are seeing for a long time. But the question that arises is when should we use Recovery scenario manager and when not? And why is recovery scenario manager not used for all the events that gets raised while QTP execution?
Recovery scenarios are used only when the event occurrence is not predictable. That is if we are not sure at what place the event may get raised, then we go for recovery scenarios. If the event occurrence is predictable, then it is always recommended to use programmatic handling by using statements such as IF conditions or On error resume next. Because using Recovery scenario manager will slower down the performance of QTP. 

For example, assume that Ads are getting raised when Application is opened in a browser or while accessing the application. Assume that sometimes they get raised immediately as soon as you open a browser and sometimes they get raised only after you are accessing something on the page. And also assume that you are not able to predict how many ads get displayed. In such situations we go for Recovery Scenarios. 

One of the other best example and common example is Printer out of Paper example. It is not possible to predict at what time Network would return the printer error. Hence Recovery scenario is used in this example because by the time the printer error message gets raised QTP might have progressed several steps before the error is displayed.
Some of the examples where we don’t go for Recovery scenarios:

The dialog box that gets prompted for remembering the password. This can be handled using If statements.

The dialog box that gets raised to overwrite the existing file during the process of saving a file.
How to create a Recovery Scenario:
Select Recovery Scenario Manager from Resources>>Recovery Scenario Manager.
Click on New to create a new Recovery Scenario and it takes the user to ‘Recovery Scenario Wizard’.
Recovery Scenario involves following steps: 

1. Define the trigger event that interrupts the test run 

Define the trigger event that interrupts the test run such as Popup window, QTP error or Application error.
2. Specify the recovery operation(s) required to continue
Specify the recovery operations that need to be performed on the trigger event such as closing the Popup through keyboard operation/mouse operation, closing the application, Calling a function or restarting windows.





3. Choose a post-recovery test run operation
Instructing QTP what should be done after performing the recovery operations on Triggered event. For example like re-executing the same step in the test or moving to next step in the test or moving to the next test or close the execution of qtp script etc.,



4. Specify a name and description for the recovery scenario
Specify the name and description for the recover scenario.






5.Specify whether to associate the recovery scenario to the current test and/or to all new tests
Specify whether to add the recovery scenario to current test or make it a default to all the new tests that are created here after.



Reference: Quick Test User guide - Defining Recovery Scenarios

Regular Expressions in QTP

Sometimes there might be situations where values of objects keep on varying and QTP may fail to recognize them or checkpoints might get failed as the expected and actual data are not matching. In such cases Regular expressions come into picture. It enables QTP to identify objects and text strings with varying values.Regular expressions can be used:

1. In identifying the property values of an object

2. In parametrization

3. For creation of checkpoints on objects with varying values

1. In identifying property values of an object:

In this concept, let’s see how to handle QTP when a value of an object is varied. Let’s walk through this section with a small example:

Assume that you have to record a scenario of verifying the mails in a mail account. Open the mail with valid username and password. Now click on Inbox. When you have opened your mail account assume that there are 5 mails. So the inbox would be showing Inbox (5). When you click on that, the mails get displayed. Following would be the statement that gets generated when clicked on Inbox:

Browser(“Gmail”).Page(“Gmail”).Link(“Inbox(5)”).click

Now when the same script is run again, QTP fails at the same step as the number of mails in the inbox is 4. Or even more if some new mails have come. The script execution doesn’t get passed unless the total number of new mails is again 5. We make use of regular expression here as the value for the number of mails keeps varying.

For creating a regular expression, go to object repository from ResourcesàObject repository. Select the link object Inbox (5) from object list displayed in the left side.


Go to the object properties screen displayed in the right side of the screen. Select the property that has the value Inbox (5). Click on the configure icon that gets displayed there.



‘Value configuration Options’ dialog box gets displayed.



Check the Regular expressions check box. A dialog box gets raised asking the user whether to add a back slash before each special character in order to treat it literally. As we have special characters ‘(‘ and ‘)’ we need to click on Yes and QTP adds a slash character before both the Parenthesis characters.

If you want qtp script to be successful for 0-9 mails then give the following value in Constant edit box:

Inbox\([0-9]\)

For 0-99 mails

Inbox\([0-9][0-9]\)

For 0-999 mails

Inbox\([0-9][0-9][0-9]\)


Click on OK and close the object repository window. For first option QTP executes the scripts successfully if the Inbox has 0-9 mails. Second option executes successfully for 0-99 mails and third option for 0-999 mails.

2.
In Parameterization:
In some cases, we need to parameterize the varying values in the script. In such cases we parameterize the values and use the regular expression in parameterized value i.e., in data table sheet where the value is present. Using the same example mentioned above lets discuss how can be regular expressions in parameterization process.

Select the link object Inbox (5) from object list displayed in the left side of Object repository.

Go to the object properties screen displayed in the right side of the screen. Select the property that has the value Inbox (5). Click on the configure icon that gets displayed there.

‘Value configuration Options’ dialog box gets displayed. Set the Parameter Radio button and select the location of data table (global/local). Enter a title for the column.Now check the Regular expressions checkbox in advanced configurations option. Click on Yes in the dialog box that gets displayed.



Click on OK and close the object repository window. If you want QTP script to be successful for 0-9 mails then give the following value in Data table sheet of QTP:

Inbox\([0-9]\)

For 0-99 mails

Inbox\([0-9][0-9]\)

For 0-999 mails

Inbox\([0-9][0-9][0-9]\)

For first option QTP executes the scripts successfully if the Inbox has 0-9 mails. Second option executes successfully for 0-99 mails and third option for 0-999 mails. Using this example, the values can be changed directly in the data table sheet.

3.
For creation of checkpoints on objects with varying values

Assume that you have to create a text check point on some text which gets varied continuously. In the same example mentioned above, you need to insert a text checkpoint on Inbox (5) which gets varied basing on the number of mails available in the inbox. When the checkpoint is created, it takes Inbox (5) as the expected value and when the same script is run again when there are 4 mails in the inbox, and then the checkpoint fails. To overcome such situations, regular expressions is used again.

Insert a checkpoint by selecting Insert-->Checkpoint-->Text Checkpoint. (Make sure that QTP is in recording mode)


Select the text on which you want to insert text checkpoint (i.e., Inbox (5)). Text checkpoint properties window gets opened.Make sure that the radio button is set to constant dialog box. Click on Constant Value options icon that gets displayed there.

‘Constant Value options’ dialog box gets displayed. Now check the regular expressions check box and Click on yes in the dialog box that gets displayed to treat ‘(‘ and ‘)’ as literal characters. Now in the same way as mentioned in the above two examples enter the data as per your requirement.


For 0-9 mails

Inbox\([0-9]\)

For 0-99 mails

Inbox\([0-9][0-9]\)

For 0-999 mails

Inbox\([0-9][0-9][0-9]\)

Click on OK and close the Text checkpoint properties window. For first option QTP checkpoint gets passed if the Inbox has 0-9 mails. Second option gets passed for 0-99 mails and third option for 0-999 mails.

Commonly used Regular expressions in QTP and their explanations:
  • Backslash \ - treat the next character as a literal character
  • Period . - Matches any single character. For example “New.” Matches “News” or New followed by any single character such as “New1”, “New2”
  • Question mark ? - Matches zero or one specific occurrence.For example “Lo?ng” matches “Lng”, “Long”
  • Asterisk * - Matches zero or more occurrences. For example “Lo*ng" matches “Lng”, “Long”, “Loong”, “Looong”, “Loooooong” etc.,
  • Plus + - Matches one or more occurrences. For Example “Lo+ng” matches “Long”, “Loong”, “Looong”.
  • Caret ^ – Matches the beginning of the input. For example“^Small” matches “Small”, “Small town”, “Small City”
  • Dollar $ - Matches the end of input. For example “City$” matches “Small City”, “Beautiful City”.
  • Square brackets [ ] - Matches any single character within a list. For example 198[123] matches “1981”,”1982”,”1983”
  • Carat is the first character inside the square brackets [^] - Matches any single character in the list except for the specified ones inside the string. For example “[^a1]” matches any single character other than “a” and “1”
  • Square brackets with hyphen [a-z] – Matches any single character with in the specified range. For example [a-z] matches any character between alphabets a to z lowercase. 199[0-9] matches any year from 1990 to 1999
  • Vertical line | - Matches one of several regular expressions. For example “Foot|Ball” matches either “Foot” or “Ball”. “Foo(t|B)all” matches “Footall” or “FooBall”.
  • Alpha numeric character \w - Matches any alphanumeric character including underscore. For example: “\w” matches any alpha numeric character such as “a”, “9”,”_” and “\w*” matches “ab9”, “king_queen_123”, “1700BC_NEWS123”. “\w{5} matches “abc12”, “a12_b”
  • Non Alpha numeric character\W – Matches any character other than Alpha numeric and underscore. For example “\W” matches “@”,“#”, “%”, “<”.

Reference: Quick Test user guide - Understanding and Using Regular Expressions

Friday, August 26, 2011

QTP - How to get the color of an object using QTP?

One quick answer for this question what I heard/read was to use GetRoProperty("Color"). But when noticed carefully, using this method always returns a value #0000ff no matter what the actual color of the object(say for example a link) is. #0000ff is an RGB value for Blue color. Most of the times the color of a link would be blue and using this would pass your test case. But what happens when you have a link in a color other than Blue?

Take a look at the following Screen Shots:


Example 1:





Example 2:



Also the same method can’t be used against web elements as you wouldn't find any color property in the list of available properties for web elements when you do an object spy on that element.





So to get the accurate results, it is always recommended to use Object.Current Style method to get the color of any object using QTP. 

How do you use this method?

See the below code:

Dim oColor

oColor= Browser("BrowserName").Page("PageName").Link("LinkName").Object.currentStyle.color

msgbox oColor

Now assume that the color of the link changes when hovered on that link, how do you get that changed color?





To do this you need to switch QTP replay mode to Mouse replay mode, fire an OnMouseOver event, get the color of the object using current style method, and then switch back the QTP reply mode Event Replay mode. Please look at the following code:

Dim oColor, oColorMouseOver

oColor=Browser("CNN.com - Breaking News,").Page("CNN.com - Breaking News,").Link("How journalist won over").Object.currentStyle.color

Setting.WebPackage("ReplayType") = 2

Browser("CNN.com - Breaking News,").Page("CNN.com - Breaking News,").Link("How journalist won over").FireEvent "onMouseOver"

oColorMouseOver=Browser("CNN.com - Breaking News,").Page("CNN.com - Breaking News,").Link("How journalist won over").Object.currentStyle.color

Setting.WebPackage("ReplayType") = 1

msgbox "Color of the object is: " & oColor & ". And the Color of the object on MoouseOver is: " & oColorMouseOver




You can also use this CurrentStyle Object method to get the FontSize, FontStyle, BackGroundColor etc., in the following way:


oFontSize= Browser("BrowserName").Page("PageName").Link("LinkName").Object.currentStyle.fontSize
oFontStyle= Browser("BrowserName").Page("PageName").Link("LinkName").Object.currentStyle.fontStyle
oBackGroundColor= Browser("BrowserName").Page("PageName").Link("LinkName").Object.currentStyle.backgroundcolor