Monday 3 February 2014

Reporter Object in QTP

In this post, we'll see how to customize QTP Test Result file. Test Result is automatically generated after every test run.


Reporter Object is used for sending information/message to the QTP test results. Following are the method/properties we can use with ‘Reporter’..


•    ReportEvent Method
•    Filter Property
•    ReportPath Property
•    RunStatus Property


ReportEvent Method


ReportEvent is commonly used method od Reporter and most of you must be already aware of this!

As the name suggest, it reports an event to the test results. Means, by using Reporter.ReportEvent, you can send information to QTP test result file.

Syntax:
Reporter.ReportEvent EventStatus, ReportStepName, Details

As we can see above, ReportEvent takes 3 arguments. Let's discuss these briefly-


EventStatus:  Status of the step i.e. Pass/Fail etc. It can be four types..


0 or micPass: Causes the status of this step to be passed and sends the specified message to the report.

1 or micFail: Causes the status of this step to be failed and sends the specified message to the report. When this step runs, the test fails.

2 or micDone: Sends a message to the report without affecting the pass/fail status of the test.

3 or micWarning: Sends a warning message to the report, but does not cause the test to stop running, and does not affect the pass/fail status of the test

ReportStepName: It is name of step. It can be any string value.

Details: It is also any string value. You should provide description of steps here.

Example of ReportEvent Method

The following examples use the ReportEvent method to report a failed step.

Reporter.ReportEvent 1, "Custom Step", "The user-defined step failed."
or
Reporter.ReportEvent micFail, "Custom Step", "The user-defined step failed."

You can leave last two arguments (ReportStepName and Details) blank as well but recommended.

Reporter.ReportEvent 1, "", ""
or
Reporter.ReportEvent micFail, "", ""


Okay friends! It was most important part of Reporter. Let's quickly discuss remaining three properties also.


Filter property

Using Filter property, you can instruct QTP about what type of event to display in result.

Syntax:
Reporter.Filter = Mode


What is the use of Filter property?

Filter is used if you dont want to display all types of events in QTP test result. For example you have 10 Reporter.ReportEvent statements in your test (5-
Pass and 5-fail). By default all 10 will be displayed in result. You can use Filter if you want to display only pass or only fail events in the result. One way to comment all unwanted type of Reporter statements, but what if you can do that by writing just one line in your code!


There can be four types of Filter modes..

0 or rfEnableAll: This is the default mode. All reported events are displayed in the Test Results.

1 or rfEnableErrorsAndWarnings: Only those events with a warning or fail status are displayed in the Test Results.

2 or rfEnableErrorsOnly: Only those events with a fail status are displayed in the Test Results.

3 or rfDisableAll: All events in the Test Results are disabled. No event will be displayed in result file.


Example of Filter Property: 

Reporter.Filter = 2
or
Reporter.Filter = rfEnableErrorsOnly



ReportPath Property


It simply retrieves the folder path in which the current test's results are stored.

Do not use this property to attempt to set the results folder path. Means it is used for 'getting the path' not 'setting the path'.

Syntax:
Path = Reporter.ReportPath


Example of RepoertPath Property

Dim Path
Path = Reporter.ReportPath
MsgBox (Path)

Above example uses the ReportPath property to retrieve the folder in which the results are stored and displays the path in a message box.


RunStatus Property

It retrieves the run status at any particular point during the run session. For tests, it returns the status of current test during the test run.

Syntax:
Reporter.RunStatus

Example of RunStatus Property

If Reporter.RunStatus = micFail Then
Msgbox "Run Status is fail. Exiting from Action iteration."
ExitAction
End If


Above example uses the RunStatus property to retrieve the status of the run session at a specific point and exit the action if the test status is fail. If the test status is not fail, the test run continues.

In case of any queries/suggessions, please post your comments.


Debugging in QTP


Debugging is the process of locating and fixing bugs in computer program code.
Before starting debugging in QTP, We need to install Microsoft debugger for debugging. This can be done during QTP Installation. Also we can install this manually later.
Pause:
To detect and isolate defects in a test or function library, you can control the run session using the Pause command. This will pause the test run and you can check for state of objects, and values in variable at the moment of pause to get the issue. You can temporarily suspend a run session by choosing Debug > Pause or clicking the Pause button. The Test execution will resume if we Press F5 or Run.

Step Into:
While navigation into a function definition from the function called in script. We can use Debug>Step Into or Press F11 or click on Step into button. Using Step Into, we can debug in the function called in script and run each step line by line in the function.

Step Over:
Step Over does debugging line by line in the script, but does not debug into a function called within script, but executes the function as a step and moves to next step in the Test. We can use Step Over using F10 Key or Debug>Step Over or icon for Step Over in the QTP.

Step Out:
Suppose we have step into a function as explained in Step To, and wants to come out of the function or Action, we use Step Out Option. We can use Step Over using shift + F11 Key or Debug>Step Out or icon for Step Out in the QTP IDE.

Run To Step:
You can instruct QTP to run from the beginning of the test or action or from the current location in the test and to stop at a particular step. This is similar to adding a temporary breakpoint to a step.
To select Run to Step, Insert your cursor in the step in which you want QuickTest to stop the run and choose Debug > Run to Step or press Ctrl+F10

Debug from Step
You can instruct QuickTest to begin your debug session from a particular step instead of beginning the run at the start of the test or action. Before you start debugging from a specific step, make sure that the application is open to the location.
To debug a test from step, Insert your cursor in the step where you want QuickTest to start the run and choose Debug > Debug from Step.

Using BreakPoints in QTP:
We can use breakpoints to suspend a run session and inspect the state of your application or mark a point from which to begin stepping through a test or function library.
Breakpoints are applicable only to the current QuickTest session and are not saved with the tests.
Test execution pauses at the step where we provide breakpoint. A breakpoint can be inserted using Debug > Insert/Remove Breakpoint or clicking Insert/Remove Breakpoint button or pressing F9 key on the step.
To enable\disable a breakpoint, i.e enable a checkpoint if disabled below or vice versa, Press Ctrl + F9 or select Debug > Enable/Disable Breakpoint
We can enable disable all breakpoint by selecting Debug > Enable/Disable All Breakpoints or click the Enable/Disable All Breakpoints button.

Debug Viewer in QTP:
We can use debug Viewer in QTP to view, set, or modify the current value of objects or variables in your test or function library, To view Debug Viewer, Choose View > Debug Viewer or click the Debug Viewer button.

Below are the tabs in Debug Viewer:

Watch Tab
This is used to view the value of any variable or an expression in the test. To add an expression, Click the expression and choose Debug > Add to Watch.

Variables Tab
QTP automatically displays the current value of all variables in the current action or function in the Variables tab—up to the point where the test or function library is stopped or paused. We can change value of a variable manually also in variables tab. This will be reflected in test.

Command tab
This is to execute a line of script to set or modify the current value of a variable or VBScript object in your test or function library.



Sunday 2 February 2014

How to get distinct value from an array

In this post, we will check, how to get unique/distinct values from an array.
We will do that using very useful and optimum feature of QTP - Dictionary Object.

Here is the code--

Dim objDict, oldArray, val
Array1 = Array ("101", "201", "101", "301", "201", "401", "301")


Set objDict = CreateObject("Scripting.Dictionary")
objDict
.CompareMode = vbTextCompare

For each val in Array1
objDict
(val) = val
Next

Array2 = 
objDict.Items

For i=0 to ubound(Array2)
msgbox
 Array2(i)
Next 

How to get User Name of the machine

Here in this post we will quickly see how to get the User Name of the machine you are executing your test. It's useful specially in results of your test. If you have have more then one users/machines involved in the test execution, it'll help you to differentiate which tests have been executed on which machine..  

Function f_GetSystemUserName()
    Set oSystemInfo = CreateObject("ADSystemInfo") 
    Set oUser=GetObject("LDAP://" & oSystemInfo.UserName)
    f_GetSystemUserName= oUser.DisplayName
    Set oSystemInfo = Nothing
    Set oUser = Nothing
End Function    'End of function f_GetSystemUserName()

'Calling above function 
Msgbox f_GetSystemUserName()