Pages

Wednesday, April 27

QTP Reporter Object - Send messages to QTP Result file

Hello Friends!


In this post, we'll see how to customize and manage the information in QTP Test Result file. Test Result is automaticaly 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 ReportPath 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.

Tuesday, April 19

Running vbscript code without QTP

Hello friends!

Here we will see how to execute vbscipt code without having QTP on your machine!!

Really?? How??

Okey..

1. Just copy n paste below code to new notepad...


'To extract a number from alphanumeric text string
Dim mystring, myLength
mystring = "qtpschools567test12398ing"
myLength = Len(mystring)

For i = 1 To myLength
    If Asc(Mid(mystring, i, 1)) <> 32 Then
        If Asc(Mid(mystring, i, 1)) >= 48 And Asc(Mid(mystring, i, 1)) <= 57 Then
            myNumber = myNumber & Mid(mystring, i, 1)
        End If
    Else
        msgbox("no numeric")
    End If
Next

msgbox(myNumber)


2. Save file as "test.vbs" (with double quotes)
3. It'll display like this on your hard drive..
4. Now close the file and double click on the file icon.
and it's done!! :)

Please note that it'll work only if you don't have any object in your script.
In case of any queries, plz post your comments.

Monday, April 18

QTP - General Tips


Hello Friends,

In this post we will see few common things we should do while working on QTP..

Make sure few points before you start creating a test in QTP-

1. Load only required Add-inns i.e. if working on window based application, do not load Web, Java etc. More then 3 Add-inns may slow down QTP.

2. Make sure that MS Script Debugger is installed on machine. Although it's part of QTP installation but  if your Internet connectivity is not proper while installing QTP, it might not be installed. It enables you to debug scripts and using breakpoints.

3. Open QTP first and then application. If application is already open, close it. Open QTP and then open application. (not true in few specific cases)

4. Check record settings..

a) Go to Test >> Record and Run Settings.
b) Select the Windows Applications tab.
c) Select the "Record and run test on any open Windows-based application" option.

5. Close all browsers before start recording.

6. Clear AutoComplete option in IE for user names and passwords

a) Tools >> Internet Options >> Content tab >> AutoComplete >> Settings
b) Clear "Usernames and passwords on form" checkbox

7. DataTable, Function Library and Object Repository are most common resources used in QTP. Make sure all these attached/managed properly.


8. By default, result is automatically open on completion of test execution. Close automatic result if you don't specifically require this.

a) Go to >> Tools >> Option >> Run
b) Clear "Display Results after Iteration" checkbox

You can still open results whenever you want by clicking View >> Results.

9. Try to get good RAM (at least 2GB) on the machine. Better RAM you have, faster your QTP will be.

10. Clear temporary files frequently (once in a week).
a) Restart Machine
b) Go to Start >> Run >> type -  %temp% >> Enter
c) Delete all files and folders


Please share your views by posting comments

Thursday, April 14

Absolute and Relative Paths in QTP

Hello Friends,

In this post, we will learn how to make life easy using Relative Paths!!

First thing first..

What is Absolute Path?


Suppose you have a qtp test stored at "C:\MyQTPProject\release1\MainTest"
You have some external actions, shared object repository, function libraries, recovery scenarios and data files stored in different folders in above path..

your external action is in - "C:\MyQTPProject\release1\MainTest\CallTest\Action1"
your OR is in - "C:\MyQTPProject\release1\MainTest\OR"
your Lib is in - "C:\MyQTPProject\release1\MainTest\Lib"

and so on..

When you attach all these recourses to you test, you can do it by using either Absolute path or relative path.
By default we use Absolute path.


General steps we follow are, Go to - Insert >> Call to existing Action >> Select Path >> select the reusable action from the drop down list.
Here the path we will select is - "C:\MyQTPProject\release1\MainTest\CallTest" as our external action is stored here. This is Absolute path.


What is Relative Path and why I need this?

Now suppose you need to move (cut & paste) you folder to some different location/machine!
When you move your folder, all the Absolute paths mentioned above will not be found as path will not exist.

If you try to run your test, you will see missing recourse error. To make it work you need to do a lot of rework which sucks!

Here we need something like - Relative Paths!!!



How to use Relative Paths?

Instead of giving full/absolute path, we can use relative paths.

Go to - Insert >> Call to existing Action >>



In "From Test" , instead of browsing to path, simply type - "..\CallTest" (without quotes) and all reusable action will be displayed in drop down list.


Same process for function lib, object repository etc.


Now even if you folder has been moved, you don't need to do any extra effort. Life is easy!! :)


Sunday, April 10

File Handling in QTP - Part 2

Click here for Part-1 of this post.

Using FSO we can create, modify, delete files and folders. Here we'll cover, how to--

- Create Folder
- Write a line to file
- Append line to file
- Reading Data from existing file
- Search a particular string in a file
- Delete a file


Create Folder


Following code checks whether a folder or file exists or not. If not, then create it.

Dim oFSO, oFolder, oShell, oTxtFile, oFile
Dim strFolder, strFile


' Create File System Object
Set oFSO = CreateObject("Scripting.FileSystemObject")

'Set folder and file name
strFolder = "C:\QTPSchools"
strFile = "\abhi.txt"

' Check if strFolder folder exists. If not, create it.
If oFSO.FolderExists(strFolder).Exist = False Then
    Set oFolder = oFSO.CreateFolder(strFolder)
    Print "Folder did't exist already. Created now -  " & strFolder
End If

' Check if oFile folder exists. If not, create it.
If oFSO.FileExists(strFolder & strFile).Exist = False Then
    Set oFile = oFSO.CreateTextFile(strFolder & strFile)
    Print "File did't exist already. Created now -  " & strFolder & strFile
End If

set oFile = Nothing    'Relese object
set oFolder = Nothing    'Relese object



Write a line to file

Following code demonstrate how to open an existing file and write a line to file and close.

Set oFSO = CreateObject("Scripting.FileSystemObject")

Const ForWriting = 2
Set oTxtFile = oFSO.OpenTextFile("C:\QTPSchools\abhi.txt", ForWriting, True)
oTxtFile.WriteLine "This is the new line - ForWriting"     

oTxtFile.Close

Append a line to file

Const ForAppending = 8
Set oTxtFile = oFSO.OpenTextFile("C:\QTPSchools\abhi.txt", ForAppending, True)
oTxtFile.WriteLine "This is the new line - ForAppending"    
oTxtFile.Close

Please note that if you open file in Writing mode, all the existing data will be overridden by new data. But if you open file in Appending mode then all existing data will be preserved and new data will be appended at bottom.


Reading Data from existing file

Const ForReading = 1
Set oTxtFile = oFSO.OpenTextFile("C:\QTPSchools\abhi.txt", ForReading, True)
Print oTxtFile.Readline 'it will read first line from the file
oTxtFile.Close


Search a particular string in a file

Following code demonstrate how to search a particular pattern/string in file. It will print all the line in which string found.

Const ForReading = 1

Set oRegEx = CreateObject("VBScript.RegExp")
oRegEx.Pattern = "new.*" ' Enter pattern/string you want to search

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile("C:\QTPSchools\abhi.txt", ForReading)

Do Until oFile.AtEndOfStream
    strSearchString = oFile.ReadLine
    Set colMatches = oRegEx.Execute(strSearchString)
    If colMatches.Count > 0 Then
        print strSearchString
    End If
Loop
oFile.Close

Find and replace a pattern string (regular expression) in a file

Function f_ReplacePatternString(Corr_Id)
 strFilename = "C:\DIR\all_Files\try.txt"
 Const ForReading = 1
 Const ForWriting = 2
 Set oRegEx = CreateObject("VBScript.RegExp")
 oRegEx.Pattern = "requestId"":\""\d{3}"

 Set oFSO1 = CreateObject("Scripting.FileSystemObject")
 Set oFile = oFSO1.OpenTextFile(strFilename, ForReading)
 strOldString = oFile.ReadAll
 Set colMatches = oRegEx.Execute(strOldString)
 For each match in colMatches
 msgbox match.value
 strNewString = Replace(strOldString, match.value , "requestId"":""" &Corr_Id&"")
 Next
 oFile.Close
 Set oFSO1 = Nothing
 Set oFSO2 = CreateObject("Scripting.FileSystemObject")
 Set oFile = oFSO2.OpenTextFile(strFilename, ForWriting,True)
 oFile.WriteLine strNewString
 oFile.Close
End Function
 
Call f_ReplacePatternString("222")
 
 
Delete a file

Following code demonstrate how to delete a particular file.

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.DeleteFile("C:\QTPSchools\abhi.txt")



Access file information


   filename="e:\qtp_temp\file1.txt"
   Dim fso, f, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFile(filename)
   s = "Path: " & f.Path & vbnewline
   s = s & "Created: " & f.DateCreated & vbnewline
   s = s & "Last Accessed: " & f.DateLastAccessed & vbnewline
   s = s & "Last Modified: " & f.DateLastModified  
   ShowFileAccessInfo = s
   msgbox ShowFileAccessInfo

Here is the output - 


In case of any queries, please leave your comments.

Saturday, April 9

How to right-click on any object using QTP

Hello Friends!
Following code demonstrate how to right click on a particular object using QTP.
We'll use DeviceRepaly object to do that..


Dim oDR
Set oDR=CreateObject("Mercury.DeviceReplay")
Set obj=Browser("name:=Yahoo.*").Page("title:=Yahoo.*").Webedit("name:=p")
obj.click
wait 3
x1=obj.GetroProperty("x")
y1=obj.GetroProperty("y")
oDR.MouseClick x1,y1,2


Please note that 2 in above line is for Right click. [0=left; 1=middle; 2=right]

Sunday, April 3

Descriptive Programming in QTP - Part 4



Hello Friends!


In Part-1, Part-2, Part-3 of this post we learned basics, type, use and various methods of descriptive programming.


In this part we will continue with more good stuff.. :)


Apart from GetROPropety, these are some other methods which are very useful in DP.


- GetTOProperty
- GetTOProperties

- SetTOProperty



GetTOProperty returns the value of the specified property from the test object description.


Example:
Msgbox Browser("MyBrowser").Page("MyPage").GetTOProperty("html tag")




GetTOProperties returns the collection of properties and values used to identify the object.


Example:
Set Propset = Browser("MyBrowser").Page("MyPage").GetTOProperties
Above statement will give you all the properties used in object identification by QTP to an array Propset. All these properties and values can be retrieved using below code..


For i = 0 to Propset.Count-1
    Print Propset(i).Name & ":=" & Propset(i).Value
Next

SetTOProperty is used to set the value of the specified property in the test object description. It is used when you require to change the any particular property at run time.


For Example you need to work open/work on two instances of an application at same time. Obviously, all the properties will be same. In this situation you can use SetTOProperty to change properties at run-time.


Please note that changes made by SetTOProperty will be applicable at runtime only. Once your execution is finished, no change will be saved. Also changes will be applicable to that action only.



Example:
Browser("MyBrowser").Page("MyPage").WebElement("my text").SetTOProperty "Style", "'color:blue'"


Feel free to post comments if you have any question from this or last three posts.