Thursday, 5 April 2012

QTP Script Performance Tips:

QTP script optimization techniques. It covers ideal QTP settings and VBScript coding best practices to improve script execution performance.

1.Always set QTP to run in Fast mode instead of Normal mode

QTP scripts can be executed in two modes Normal and Fast.

Normal (displays execution marker)—Runs your test or component with the execution arrow to the left of the Keyword View or Expert View, marking each step or statement as it is performed. If the test contains multiple actions, the tree in the Keyword View Item column expands to display the steps, and the Expert View displays the script, of the currently running action.

Fast—Runs your test or component without the execution arrow to the left of the Keyword View or Expert View (for tests) and does not expand the item tree or display the script of each action as it runs. This option requires fewer system resources.

We can save the time which QTP takes to display execution marker and enhance the performance of our scripts.

2. Always disable smart identification.

If QTP is unable to find an object using the learner object description it uses the smart identification mechanism for object identification. The Smart Identification mechanism takes more time to identify objects in case of failures as it checks all candidate objects and tries to find a match based on base and optional filter properties. If we turn off the Smart Identification mechanism then we can improve script performance in case of failures.

3. Always release all objects /variables after usage.

Function PrintValue()

Dim a

x=100

Print x

x=Null

End Function

The Nothing keyword in VBScript is used to disassociate an object variable from any actual object. Use the Set statement to assign Nothing to an object variable. For example: Set MyObject = Nothing

4. Use QTP's inbuilt dictionary object instead of arrays for storing values wherever possible.

5. Always use WaitProperty statements instead of hardcoded wait.

A wait statement will halt the script execution for the specified time irrespective of whether the object we are waiting for has loaded or not. On the other hand Wait Property waits until the specified object property achieves the specified value or exceeds the specified timeout before continuing to the next step.

6. Provide a specific time limit for the exist command.

7. Set Global sync time to 1 sec

8. Launch QTP by creating a script that uses the Automation Object model. The script can be used to launch QTP with the required add-ins and QTP settings.

9. Create object references for frequently used objects in scripts.

e.g: Set UName = Browser("Br").Page("Pg").WebEdit("login").Object

10. Use select case construct instead of if.. elseif where it is possible to convert the if..elseif into the select case.

11. Use with statements to combine statements into a group that use the same parent hierarchy.

12. If we need to use the data stored in an excel file we should use code to connect to excel as a database and query on it as you would any other database rather than using the QTP datatable object.

13. Use custom error/exception handling instead of Recovery scenarios to handle exceptions in scripts

14. Use a judicious mix of Actions and functions

15. Declare all variables and functions in a separate .vbs file and associate it with your action.

16. Turn off image downloads incase they are not validated.

17. Tune your object identification settings for the environments and objects you are dealing with to best reflect your application objects identification.


Recovery Scenarios in QTP

Recovery Scenarios in QTP
Recovery scenario - a definition of an unexpected event or error or application crash and the operation(s) necessary to recover the run session
A recovery scenario consists of the following:
a. Trigger Event—The event that interrupts your run session
b. Recovery Operation(s)—The operation(s) that need to be performed in order to continue running the test or component
c. Post-Recovery Test Run Option—The instructions on how Quick Test should proceed once the recovery operations have been performed, and from which point in the test or component Quick Test should continue, if at all.
Recommendations for using Recovery Scenarios
1. If you can predict that a certain event may happen at a specific point in your component, it is highly recommended to handle that event directly within your component by adding steps such as If statements or “Optional Steps” in user-defined functions, rather than depending on a recovery scenario.
2. You should use recovery scenarios only for unpredictable events, or events that you cannot synchronize with a specific step in your component.
3. Handling an event directly within your component enables you to handle errors more specifically than recovery scenarios. It also enables you to control the timing of the corrective operation with minimal resource usage and maximum performance.
Different Recovery Operations:
1. Keyboard or mouse operation—Quick Test simulates a click on a button in a window or a press of a keyboard key.
2. Close application process—Quick Test closes specified processes.
3. Restart Microsoft Windows—Quick Test restarts Microsoft Windows.
Note: If you use the Restart Microsoft Windows recovery operation, you must ensure that any component associated with this recovery scenario is saved before you run it. You must also configure the computer on which the component is run to automatically log in on restart.
4. Function call—Quick Test calls a VBScript function.
Note: User can select an existing function from a library file or he can define a new function. The new function is added to the function library you selected.
Different Post-Recovery Test Run Options:
1. Repeat current step and continue
The current step is the step that Quick Test was running when the recovery scenario was triggered.
Note: If you are using the On error activation option for recovery scenarios, the step that returns the error is often one or more steps later than the step that caused the trigger event to occur. Thus, in most cases, repeating the current step does not repeat the trigger event.
2. Proceed to next step
Skips the step that Quick Test was running when the recovery scenario was triggered.
Note: Keep in mind that skipping a step that performs operations on your application may cause subsequent steps to fail.
3. Restart current test run
Stops performing steps and re-runs the component from the beginning.
4. Stop the test run
Stops running the component.
Note: If you chose Restart Microsoft Windows as a recovery operation, you can choose from only the last two test run options listed above.
Associating Recovery Scenarios with the test:
a. While associating the recovery with your test, you need to specify when you want to activate recovery scenarios i.e. only on errors or on every steps.
b. If you select On errors option then keep in mind that by default, recovery operations will be activated only when a step returns an error, which can potentially occur several steps after the one that actually caused the error. So you may get the advantages of using recovery scenarios.
Note: If you are using “On Error Resume Next” statement in a script which has some activated Recovery scenarios then keep in mind that “On Error Resume Next” is NOT going to affect the triggering of Recovery Scenario i.e. Recovery Scenarios has priority over “On Error Resume Next”.
c. If you select on every steps then keep in mind that checking for trigger events after every step, may slow performance.
Note: In this case, use “Recovery Object” which enables you to control the recovery scenario mechanism programmatically during the run session. “Activate Method” is an useful method associated with Recovery Object which explicitly activates the recovery scenario mechanism at a specific point in the run. Activating recovery scenarios in particular section of the script, where the probability of unexpected event or error or application crash is high, can improve performance.

When To Use Recovery Scenarios
Recovery scenarios are intended for use only with events that you cannot predict in advance, or for events that you cannot otherwise synchronize with a specific step in your test. For example, you could define a recovery scenario to handle printer errors. Then if a printer error occurs during a run session, the recovery scenario could instruct QuickTest to click the default button in the Printer Error message box.
You would use a recovery scenario in this example because you cannot handle this type of error directly in your test. This is because you cannot know at what point the network will return the printer error. Even if you try to handle this event by adding an If statement in your test immediately after a step that sends a file to the printer, your test may progress several steps before the network returns the actual printer error.
If you can predict that a certain event may happen at a specific point in your test, it is highly recommended to handle that event directly within your test by adding steps such as If statements or optional steps, rather than depending on a recovery scenario. For example, if you know that an Overwrite File message box may open when a Save button is clicked during a run session, you can handle this event with an If statement that clicks OK if the message box opens or by adding an optional step that clicks OK in the message box.
Handling an event directly within your test enables you to handle errors more specifically than recovery scenarios, which by nature are designed to handle a more generic set of unpredictable events. It also enables you to control the timing of the corrective operation with minimal resource usage and maximum performance. By default, recovery scenario operations are activated only after a step returns an error. This can potentially occur several steps after the step that originally caused the error. The alternative, checking for trigger events after every step, may slow performance. For this reason, it is best to handle predictable errors directly in your test.

Test Automation Guidelines

Test Automation guidelines

Test Automation now an integrated part of software testing activity. It defines the quality of product by regressive testing the areas in short time. Proper guidance, strategy & framework make the automation effective and right investment for testing.

The main factors are:

1.Basics of Test Automation
2.Need for Automation
3.Relationship with Regression Testing
4.Industry Facts
5.Automation requirements Analysis
6.Guideline for Test Automation
7.Selection of tools
8.Optimum usage of test automation in test life cycle
9.Automation Framework
10.Estimation for Automation & Factors affecting estimation
11.Automation Entry and Exit Criteria
12.Test Plan development
13.Automation test plan review and understanding
14.Managing test data and environment
15.Script Design, Development and Execution
16.Reporting
17.Case study

Wednesday, 25 January 2012

Automation Benefits

Automation Benefits:

1. Run existing(Regresson) tests on a new version of a program

2. Run more tests more often

3. Perform tests which would be difficult or impossible to do manually

4. Better use of resources

5. Consistency and repeatability of tests

6. Reuse of tests

7. Earlier time to market

8. Increased confidence

Friday, 6 January 2012

Xpath Usage in QTP 11


Dim ObjPage

Set ObjPage=Browser("name:=Brw").Page("title:=Pge")

ObjPage.WebEdit("xpath:=//INPUT[@name='username']").Set "FirstUser"

ObjPage.WebEdit("xpath:=//INPUT[@name='password']").Set "Password"

ObjPage.WebButton("xpath:=//INPUT[@name=Login]").Click

Monday, 21 November 2011

Round figured Random Value creation in QTP

WEValue=RandomNumber(1,9) & "000"
WEValue=Int(WEValue)
Browser("Browser").Page("Page").WebEdit("MyNumber").Set WEValue

Monday, 18 July 2011

Finds all the checkboxes in the parent object and clicks on them

'Finds all the checkboxes in the parent object and clicks on them all.

'Example : ClickAllCheckBoxs(Browser("BrwNAME"),"on")

Sub ClickAllCheckBoxs(object,to_do)

Rem: Creates the checkbox description object

set CheckDesc = Description.Create()

CheckDesc("micclass").Value = "WebCheckBox"

'More values can be added here such as

CheckDesc("disabled").Value = 0

'Check on for checkbox

if (to_do = "on") then

to_do ="on"

Else

to_do = "off"

End if

Set list = object.ChildObjects(CheckDesc)

'Run on all enabled checkbox's and click on them

For i=0 to list.count -1

list(i).set to_do

Next

End Sub

Usage of Classes

'Usage of Classes

Class MyClass

Function Add1(a,b)

add1=a+b

End Function

Function sub1(a,b)

sub1=a-b

End Function

End Class


Set mat=New MyClass

val=mat.add1( 2,3)

msgbox val


val1=mat.sub1(5,3)

msgbox val1

Friday, 29 October 2010

Add all Recovery scenarios to new test through code

Add all Recovery scenarios to new test through code

Dim qtApp 'As QuickTest.Application ' Declare the Application object variable

Dim qtTestRecovery 'As QuickTest.Recovery ' Declare a Recovery object variable

Dim intIndex ' Declare an index variable
' Open QuickTest and prepare objects variables

Set qtApp = CreateObject("QuickTest.Application") ' Create the Application

objectqtApp.Launch ' Start QuickTest

qtApp.New ' Open a new test

qtApp.Visible = True ' Make the QuickTest application visible

Set qtTestRecovery = qtApp.Test.Settings.Recovery ' Return the Recovery object for the current test
If qtTestRecovery.Count > 0 Then ' If there are any default scenarios specified for the test qtTestRecovery.RemoveAll ' Remove them
End If

' Add recovery scenarios

qtTestRecovery.Add "C:\Recovery.qrs", "ErrMessage", 1
' Add the "ErrMessage" scenario as the first scenario

qtTestRecovery.Add "C:\Recovery.qrs", "AppCrash", 2
' Add the "AppCrash" scenario as the second scenario

qtTestRecovery.Add "C:\Recovery.qrs", "ObjDisabled", 3
 ' Add the "ObjDisabled" scenario as the third scenario

' Enable all scenarios
For intIndex = 1 To qtTestRecovery.Count ' Iterate the scenarios

qtTestRecovery.Item(intIndex).Enabled = True
' Enable each Recovery Scenario (Note: the 'Item' property is default and can be omitted)

Next

' Enable the recovery mechanism (with default, on errors, setting)

qtTestRecovery.Enabled = True
'Ensure that the recovery mechanism is set to be activated only after errors

qtTestRecovery.SetActivationMode "OnError"

'OnError is the default, the other option is "OnEveryStep".
Set qtApp = Nothing ' Release the Application object

Set qtTestRecovery = Nothing ' Release the Recovery object

Work Arounds for Retrieve Excel Formated Data

'While you read from excel, cells which contain formula's like =Today(), =Date()
'may not reflect the same text when you retreive them.

'Workarounds
'1. So you might change in the cells in Excel as follows:
'=Text(TODAY())

dtmCellValue=Datatable.GetSheet("sheetname").getParameter("ColumnName").RAWVALUE

RegularExpression Testing

'RegularExpression Testing

Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches ' Create variable.
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.Global = True ' Set global applicability.
Set Matches = regEx.Execute(strng) ' Execute search.
For Each Match in Matches ' Iterate Matches collection.
RetStr = RetStr & Match.Value' & vbCRLF
Next
RegExpTest = RetStr
End Function

a=(RegExpTest("[a-z]", "wipro123xyz45"))

'Regular expression for date formate (mm/dd/yy)
[1-9]|1[0-2]/[1-9]|[1-2][0-9]|3[0-1]/[0-9][0-9] ------12/10/99

"|" Indicates a choice between two items.

How do you return two values from a function

'How do you return two values from a function

result=example(2,3)

Function example(x,y)
dim myarray(2)
myarray(0) = x+y
myarray(1)=x*y
example=myarray
End function
msgbox result(0)
msgbox result(1)

Wednesday, 9 June 2010

Calling Actions at runtime – QTP

This functionality was one of the most requested enhancements which has been delivered in QTP 10.0. So now user can call an action using LoadAndRunAction and the called action can be executes just like any other function call. The called action does not becomes a part current test.

Syntax : LoadAndRunAction “D:\Auotmation\Test”,“Action1”

Tuesday, 27 April 2010

Find Column Count in Datasheet

Set Colmn=datatable.GetSheet(“Details”)
ColumnCount= Colmn.getparametercount
msgbox ColumnCount