How to work with Reporter Object in QTP or UFT?
This post talks about reporter utility object in UFT,reporter object in UFT,UFT Reporter Objects with Examples,how to use Reporter.ReportEvent,syntax for reporter object in UFT.
A reporter object in UFT responsible for storing information related to test outcome to the run result.Reporter Object in UFT supports various methods and properties that are used in runtime to showcase the results.A reporter Object can change the control of the flow depending on outcome.
So a Reporter Object is responsible for the following:
- Report test status
- Enable or disable reporting
- Get the folder path from where the test is running.
- Fetch the run status.
- Report custom test result and update the run result
Methods available in Reporter object in UFT:
- ReportEvent() method
- ReportNote() method
- ReportHtmlEvent() method
Properties available in Reporter object in UFT:
- Filter property
- Run status property
- Reportpath property
Details of ReportEvent() method:
ReportEvent() method accepts four parameters
- stepDetails or stepDescription
The syntax of ReportEvent() method or syntax for reporter object in UFT:
Reporter.ReportEvent EventStatus, stepName, stepDetails,pathToSaveTheScreenshot
Now EventStatus can be of 2 types(Primarily) and can be expressed as a constant or a number.
|micPass||0||The step is passes and sends a pass signal to the run result.|
|micFail||1||The step is failed and sends a pass signal to the run result.|
|micDone||2||The step is completed and sends a completion signal to the run result.|
|micWarning||3||The step is completed with a warning and sends a warning signal to the run result.|
It accepts a String parameter and accepts the custom step name that the user wants to display in the run result.
stepDetails or stepDescription:
It also accepts a String parameter and accepts the custom step details that the user wants to display in the run result.
It also accepts a String parameter that specifies if the coder wants to save the screenshot where UFT will place the result.
The image format supported by UFT are as follows:
However, it is an optional parameter.It is also not a good practice to capture screenshot in every step. That process may end up accumulating lots of screenshots. This will also have a impact on space and reporting time. So it is best when we only take screenshot if a test failed.
How to capture a screenshot and use in the Reporter.ReportEvent()?
errorFile="D:\Test\"&testName&".png" Browser("Login").Page("Login").CaptureBitmap errorFile,True 'True will overwrite an existing file' Reporter.ReportEvent 1,"login test","Check if the page has id field",errorFile
We can also write a method to do the same by passing the file name and object name.
so the reporting will be ..
syntax for reporter object in UFT
Reporter.reportEvent 1,"User can not see the image" ,"Test case failed due to unavailability of the image"
Reporter.reportEvent micPass,"User can not see the image" ,"Test case failed due to unavailability of the image"
Example of ReportEvent method or how to use Reporter.ReportEvent?
Option Explicit Dim myVar,a,b a=10 b=15 myVar=a+b If(myVar=25) then Reporter.ReportEvent micPass,"verify Addition","Calculator is working" else Reporter.ReportEvent micFail,"verify Addition","Calculator is not working" End if
Below are the reporter utility object in UFT:
Reporter.RunStatus object provides the insights of the status of the current test run.We can further control the execution based on the outcome of the Reporter.RunStatus value.
Example of Reporter.RunStatus/UFT Reporter Objects with Examples
If Reporter.RunStatus=micFail Then ExitTest End If
This code will Exit the current test if the test status is failed.
How we can change the Reporter.RunStatus from code ?
From our code we can use the following code to change the status of the Reporter.RunStatus.
How to change the the mode of the test?
To change the mode of the test we need to use Reporter.Filter property.
The code will be:
How to get the current mode of the test run?
To get the current mode of the test run, we can use the following code.
UFT provides the following modes for the use of Filter property.
|rfEnableAll||0||It is the default mode that displays all elements in the run result.|
|rfEnableErrorsAndWarnings||1||It filters out all other modes and displays only Errors and warnings.|
|rfEnableErrorsOnly||2||Displays only fails on the run result|
|rfDisableAll||3||Displays no events in the run result.|
What if you are using Reporter object on Adobe Flex then reporter object may fail intermittently or if you want to track the exact line of failure than you can override the ReporterEvent.
Can we use ReporterEvent in Keyword view or how to use Reporter.ReportEvent in keyword view?
Yes,we can use ReporterEvent() method in keyword view.Here is the navigation:
- Select the desired step and choose Insert -> Step -> Report or right-click a step and choose Insert Step -> Report.
Reporter.ReportHtmlEvent() method is very similar to Reporter.ReportEvent(how to use Reporter.ReportEvent) with an exception that here we can insert custom HTML element to beatify the run result. It is a nice feature to customize the traditional reporting.
Reporter.ReportHTMLEvent micDone, "<b><i><font face='Arial' color='black'>Test ReportHTMLEvent</font></i></b>", "<b><font face='Arial bold' color='green'>Reporting Some Text with Color</font></b>"
This method enables us to send a special to note to the run result that will be visible in the executive summary Note section.It is only visible if the root node is selected.
How to control Reporter object via code?
Reporter object in UFT can be controlled by code. Here is how-Joe has mentioned.
- Enter the following code in the Expert View of your tests:
Set oEventDesc = CreateObject("Scripting.Dictionary") oEventDesc("ViewType") = "Sell.Explorer.2" oEventDesc("Status") = micPass oEventDesc("EnableFilter") = False oEventDesc("NodeName") = "My Blog" oEventDesc("StepHtmlInfo") = "<a href='https://techtravelhub.com>TechTravelHub</a>" newEventContext = Reporter.LogEvent ("Replay",oEventDesc,Reporter.GetContext)
- Run your test and look at the HP Run Results Viewer.
This is how to use Reporter.ReportEvent in UFT.