QTP Reusable functions

上一篇 / 下一篇  2012-07-03 15:40:20 / 个人分类:QTP

Automation testing using QTP has always been talked upon the most in the testing industry. There are numerous features available within this very toolset, but most of us might not be aware of this very feature which has been discussed in this very articles on automation testing using QTP which is functions created in Vbscript , and is very useful in code customization to meet our automation testing requirements , as it reduces the script. maintenance effort..
 
1. BEEP from QTP

' Three methods of issuing a BEEP from QTP

' Method 1
Extern.Declare micLong,"MessageBeep","User32","MessageBeep",micLong
retVal = Extern.MessageBeep(&HFFFFFFFF)
MsgBox retVal 'The return value should be 1.

' Method 2
Err.Number = 0
On Error Resume Next
Extern.Declare micLong,"MessageBeep","User32","MessageBeep",micLong
Extern.MessageBeep(&HFFFFFFFF)
If (Err.Number <> 0) Then
MsgBox Err.Description
End If

' Method 3
Extern.Declare micLong,"Beep","Kernel32","Beep",micLong,micLong
retVal = Extern.Beep(1000,250) 'The first number is frequency the second is duration.

MsgBox extract_number("This user belongs to 10 groups")
MsgBox extract_number("206 features assigned to the user.")

2. Check for Required URL

startURL = "www.google.com" ' Amend this to your required URL

' If no browser open, open browser and navigate to required URL
If Not Browser("CreationTime:=0").Exist Then
   Call launch(startURL)
Else

' Get the URL of the current open browser
currentURL=Browser("CreationTime:=0").GetROProperty("OpenURL")
' If not correct URL navigate to required URL
If Not currentURL = "www.google.com" Then
   Call launch(startURL)
End If
End If

Function launch(startURL)
 ' Create IE object and navigate
 ' to required URL
 set IE = CreateObject("InternetExplorer.Application")
 IE.Visible = true
 IE.Navigate startURL
End function

3. Close QTP with QTP script

Private Function CloseQTP
   Set bjWMIService = GetObject("winmgmts:\\.\root\CIMV2")
   Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'QTPro.exe'")
   For Each objProcess in colProcess
    objProcess.Terminate()
   Next
   Set bjWMIService = Nothing
   Set colProcess = Nothing
End Function

4. Open any Appllication through QTP

   ' Function OpenApp
   ' ------------------
   ' Open a specified application
   ' Parameter: application - the application full name (including location)
   '@Description Opens an application
   '@Documentation Open the application.

   Function OpenApp (application)
     systemUtil.Run application
   End Function

5. Adding any information to QTP Results

   ' AddToTestResults
   ' --------------
   ' Add a Report.Event step to the Test Results
   'Parameters:
   ' status - Step status (micPass, micFail, micDone or micWarning)
   ' StepName - Name of the intended step in the report (object name)
   ' details - Description of the report event    '
   '@Description Reports an event to the Test Results
   '@Documentation Report an event to the Test Results.

   Public Function AddToTestResults (status, StepName, details)
       Reporter.ReportEvent status, StepName, details
   End Function

6. Enable Doubleclick Event in QTP

Enable the ondblclick event in the Web Event Recording Configuration

To enable special event handling, use the Web Event Recording Configuration utility.
1. Go to Tools -> Web Event Recording Configuration.
2. Click .
3. Expand the Standard Objects branch.
4. Select the object you want to configure.
5. Go to Event -> Add -> ondblclick. The event will be added to the list on the right.
6. Select "If Handler" in the Listen column.
7. Select "Enabled" in the Record column.
8. Click to close the dialogs.

7. DSN - Less Connection

Public sconn
uid = "UID"
pwd = "PWD"
ServerName = "Server Name"

Connection()

Function Connection()
Set sconn = CreateObject("ADODB.connection")
sconn.Open "Driver=Microsoft ODBC for Oracle;Server="& ServerName &";Uid="& uid &";Pwd="& pwd &""
End Function

8. High-Level Automation Framework Information

At a very high level the framework components can be divided into two categories software part and documentation part. Here is my list of components ? you can add your item if anything is missing.

---------------------------------------------
All software code part
---------------------------------------------
Supporting libraries for Logging, error handling, Execution management
1. Tool specific code
2. Setup and configuration scripts
3. Test management code
4. Test data and Test data management code
5. Platform/OS specific scripts

---------------------------------------------
Non software part
---------------------------------------------
Folder structure
Documents
i. Coding guidelines
ii. Procedure for creating scripts
iii. Planning, design and review procedures
iv. Approach for automation
v. Approach for Testing automation code
vi. Automation documentation - how scripts work
vii. Setup and implementation procedures
viii. Defect tracking procedure for Automation scripts
ix. Source control procedures
x. Project plan
xi. Templates for effort estimation
xii. Template for test case categorization
xiii. Template for ROI calculation

9. Get Input Parameter Data and Use in Script

Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object
Set pDefColl = qtApp.Test.ParameterDefinitions
Set pDef = pDefColl.Item(1)
MsgBox pDef.DefaultValue

10. Pass Test Data from MQC to QTP?

1) Create Test parameters in QTP (via file>Settings>Parameters) [These are the parameters that you see in QC, on the automated tab of the Configuration page in the Test Instance Propertirs.]

2) Create action parameters (Action Properties> Parameters)

3)Link action & test parameters together. To do this, go into Action Call Properties>Parameters. You'll see your action parameters here. Click in the Value column for a parameter and press the Configure Value button. This will open up the Value Configuration Options dialogue. In the parameter drop down list, select Test/action parameter and then, from the following Test parameters; Parameter drop down list, select the Test parameter that you want to link to your Action parameter.

This should then link the Test parameters to the Action parameters.

Then in your script, you'll use the parameters as follows: -

Browser("Browser_Name").Page("Page_Name").WebEdit("Edit_Name").Set Parameter("Parameter_Name")

11. Get the Attachement from QC to local machine

To get attachment from test

Public Function GetTestAttachmentPath(TDAttachmentName) 'Returns File Path
'Test Director TDAPIOLELib object variables
Dim otaAttachmentFactory 'As TDAPIOLELib.AttachmentFactory
Dim otaAttachment 'As TDAPIOLELib.Attachment
Dim otaAttachmentList 'As TDAPIOLELib.List
Dim otaAttachmentFilter 'As TDAPIOLELib.TDFilter
Dim otaExtendedStorage 'As TDAPIOLELib.ExtendedStorage
Dim strPath 'As String

Set taAttachmentFactory = QCUtil.CurrentTest.Attachments
Set taAttachmentFilter = otaAttachmentFactory.Filter
otaAttachmentFilter.Filter("CR_REFERENCE") = "'TEST_" & QCUtil.CurrentTest.Id & "_" & TDAttachmentName & "'"
Set taAttachmentList = otaAttachmentFilter.NewList
If otaAttachmentList.Count = 1 Then
Set taAttachment = otaAttachmentList.Item(1)
otaAttachment.Load True, ""
strPath = otaAttachment.FileName
ElseIf otaAttachmentList.Count > 1 Then
Reporter.ReportEvent micFail, "Failure in library function 'GetTestAttachmentPath'", _ "Found more than one attachment '" & TDAttachmentName & "' in test '" & _ QCUtil.CurrentTest.Name & "'."
strPath = ""
ElseIf otaAttachmentList.Count < 1 Then
Reporter.ReportEvent micFail, "Failure in library function 'GetTestAttachmentPath'", _ "Found 0 attachments '" & TDAttachmentName & "' in test '" & _ QCUtil.CurrentTest.Name & "'."
strPath = ""
End If

GetTestAttachmentPath = strPath
Set taAttachmentFactory = Nothing
Set taAttachment = Nothing
Set taAttachmentList = Nothing
Set taAttachmentFilter = Nothing

End Function

To get attachment from folder

Public Function GetFolderAttachmentPath(TDAttachmentName, TDFolderPath) 'Returns File Path

'Test Director TDAPIOLELib object variables
Dim otaAttachmentFactory 'As TDAPIOLELib.AttachmentFactory
Dim otaAttachment 'As TDAPIOLELib.Attachment
Dim otaAttachmentList 'As TDAPIOLELib.List
Dim otaAttachmentFilter 'As TDAPIOLELib.TDFilter
Dim otaTreeManager 'As TDAPIOLELib.TreeManager
Dim otaSysTreeNode 'As TDAPIOLELib.SysTreeNode
Dim otaExtendedStorage 'As TDAPIOLELib.TreeManager
Dim intNdId
Dim strPath 'As String
Set taTreeManager = QCUtil.TDConnection.TreeManager
Set taSysTreeNode = otaTreeManager.NodeByPath(TDFolderPath)
Set taAttachmentFactory = otaSysTreeNode.Attachments
Set taAttachmentFilter = otaAttachmentFactory.Filter
intNdId = otaSysTreeNode.NodeID
otaAttachmentFilter.Filter("CR_REFERENCE") = "'ALL_LISTS_" & intNdId & "_" & TDAttachmentName & "'"
Set taAttachmentList = otaAttachmentFilter.NewList
If otaAttachmentList.Count > 0 Then
Set taAttachment = otaAttachmentList.Item(1)
otaAttachment.Load True, ""
strPath = otaAttachment.FileName
Else
Reporter.ReportEvent micFail,"Failure in library function 'GetFolderAttachmentPath'", _ "Failed to find attachment '" & TDAttachmentName & "' in folder '" & TDFolderPath & "'."
End If
GetFolderAttachmentPath = strPath
Set taAttachmentFactory = Nothing
Set taAttachment = Nothing
Set taAttachmentList = Nothing
Set taAttachmentFilter = Nothing
Set taTreeManager = Nothing
Set taSysTreeNode = Nothing

End Function




Learn More On QTP Reusable Function creation codes :

QTP-reusable-functions-codes-5
QTP-reusable-functions-codes-4
QTP-reusable-functions-codes-3
QTP-reusable-functions-codes-2
QTP-reusable-functions-codes-1

TAG:

 

评分:0

我来说两句

Open Toolbar