Users can use these objects, and their associated methods and properties, to write theprograms that automatically configure Quick Test options and run tests
Explanation and demo on Object model


QTP Utility Functions - Database
msgbox My_dbquery(10)
Dim My_Query, MyDesc
Public Function My_dbquery(ord_no)
DataConn = "C:\Das\tozip\lib\flight32.mdb"
Set Conn = CreateObject("ADODB.Connection")
ConStr = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & DataConn & ";"
Conn.open(ConStr)
SQL = "SELECT Orders.Customer_Name FROM Orders Orders WHERE (Orders.Order_Number=" &ord_no &")"
Set recordset = Conn.execute(SQL)
while not recordset.EOF
My_Query = recordset("Customer_Name")
recordset.movenext
wend
recordset.close
Conn.Close
set recordset = Nothing
Set Conn = Nothing
My_dbquery = My_Query
End Function
QTP Utility Functions – File Operations
File Creation
CreateFile "C:","mytextfile.txt","hi how are you?"
Public Function CreateFile(filpath,filname,filcontent)
xml_file = filpath & "\" & filname
Dim fileobject, tf
Set fileobject = CreateObject("Scripting.FileSystemObject")
Set tf = fileobject.CreateTextFile(xml_file, True)
tf.Write (filcontent)
tf.Close
End Function
File Operations
Function CheckFileExists (FilePath)
' check if file exist
CheckFileExists = oFSO.FileExists(FilePath)
End Function
' *************************************************************************************
' Write data to file
' Parameters:
' FileRef - reference to the file
' str - data to be written to the file
' *************************************************************************************
Function WriteToFile (byref FileRef,str)
' write str to the text file
FileRef.WriteLine(str)
End Function
' *************************************************************************************
' Read line from file
' Parameters:
' FileRef - reference to the file
' *************************************************************************************
Function ReadLineFromFile (byref FileRef)
' read line from text file
ReadLineFromFile = FileRef.ReadLine
End Function
' *********************************************************************************************
' Closes an open file.
' Parameters:
' FileRef - reference to the file
' *********************************************************************************************
Function CloseFile (byref FileRef)
FileRef.close
End Function
'*********************************************************************************************
' Opens a specified file and returns an object that can be used to
' read from, write to, or append to the file.
' Parameters:
' FilePath - location of the file and its name
' mode options are:
' ForReading - 1
' ForWriting - 2
' ForAppending - 8
' *************************************************************************************
Function OpenFile (FilePath,mode)
' open the txt file and retunr the File object
set OpenFile = oFSO.OpenTextFile(FilePath, mode, True)
End Function
' *********************************************************************************************
' Closes an open file.
' Parameters:
' FilePathSource - location of the source file and its name
' FilePathDest - location of the destination file and its name
' *********************************************************************************************
Sub FileCopy ( FilePathSource,FilePathDest)
' copy source file to destination file
oFSO.CopyFile FilePathSource, FilePathDest
End Sub
' *************************************************************************************
' Delete a file.
' Parameters:
' FilePath - location of the file to be deleted
' *************************************************************************************
Sub FileDelete ( FilePath)
' copy source file to destination file
oFSO.DeleteFile ( FilePath)
End Sub
' ************** Example of calling the file functions **********************
FilePath1 = "D:\temp\FSO\txt1.txt"
FilePath2 = "D:\temp\FSO\txt2.txt"
FilePathDiff = "D:\temp\FSO\txt_diff.txt"
FilePath = "D:\temp\FSO\txt.txt"
set fold = FolderCreate ( "D:\temp\FSO\new")
set f = OpenFile(FilePath,8)
' = WriteToFile(f,"test line")
d = CloseFile(f)
set f = CreateFile(FilePath)
Fexist= CheckFileExists(FilePath)
d = WriteToFile(f,"first line")
d = WriteToFile(f,"second line")
d = CloseFile(f)
FileCopy "D:\temp\FSO\txt.txt","D:\temp\FSO\txt1.txt"
FileDelete "D:\temp\FSO\txt1.txt"
Excel Sheet Operations
Set objExcel = CreateObject("Excel.Application")
strPathExcel = "C:\Documents and Settings\Anandana\Desktop\QTPSamples\Reading From Excel Sheets\test.xls"
objExcel.Workbooks.open strPathExcel
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
For i=1 to 3
'For j=1 to 2
'msgbox Trim(objSheet.Cells(i, j).Value)
' Next
DataTable.SetCurrentRow i
DataTable("SL_No", dtGlobalSheet)=Trim(objSheet.Cells(i, 1).Value)
DataTable("Name", dtGlobalSheet)=Trim(objSheet.Cells(i, 2).Value)
Next
objExcel.Application.Quit
Set objExcel=Nothing
Email Operations
SendMail "cssdas@hp.com","hi","how r u",""
Function SendMail(SendTo, Subject, Body, Attachment)
Set ol=CreateObject("Outlook.Application")
Set Mail=ol.CreateItem(0)
Mail.to=SendTo
Mail.Subject=Subject
Mail.Body=Body
If (Attachment <> "") Then
Mail.Attachments.Add(Attachment)
End If
Mail.Send
ol.Quit
Set Mail = Nothing
Set ol = Nothing
End Function
XML
Option Explicit
Dim xmlFilePath
Dim xmlDoc
Dim nodeBook, nodeId, sIdXml, currNode
msgbox GetXMLAttribute("C:\QTP 8.2\Day 4\database.xml", "database/contact", "company")
msgbox GetXMLElement("C:\QTP 8.2\Day 4\database.xml", "database/contact[4]", "phone")
'********************************************************************************
' Function UpdateXMLAttribute
'********************************************************************************
Public Function UpdateXMLAttribute(xmlFilePath, xmlElement, xmlAttribute, NewXMLValue)
LoadXMLFile(xmlFilePath)
ReplaceAttributeValue xmlElement, xmlAttribute, NewXMLValue
SaveXMLFile (xmlFilePath)
Set xmlDoc = Nothing
End Function
'********************************************************************************
' End of Function UpdateXMLAttribute
'********************************************************************************
'********************************************************************************
' Function UpdateXMLElementData
'********************************************************************************
Public Function UpdateXMLElementData(xmlFilePath, ElementPath,ElementName, ElementIndex, NewElementData)
Dim CurrentNode, CurrentValue
LoadXMLFile(xmlFilePath)
Set CurrentNode = xmlDoc.selectSingleNode(ElementPath)
Set CurrentValue = CurrentNode.getElementsByTagName(ElementName)
CurrentValue.item(ElementIndex).text = NewElementData
SaveXMLFile (xmlFilePath)
Set xmlDoc = Nothing
End Function
'********************************************************************************
' End of Function UpdateXMLElementData
'********************************************************************************
' Function GetXMLAttribute
Public Function GetXMLAttribute(xmlFilePath, xmlElement, xmlAttribute)
Dim AttributeValue
LoadXMLFile(xmlFilePath)
AttributeValue = GetAttributeValue(xmlElement, xmlAttribute)
Set xmlDoc = Nothing
GetXMLAttribute = AttributeValue
End Function
'********************************************************************************
' End of Function GetXMLAttribute
'********************************************************************************
' Function LoadXMLFile
'********************************************************************************
Public Function LoadXMLFile(Path)
Set xmlDoc = CreateObject("Msxml2.DOMDocument.3.0")
xmlDoc.validateOnParse = False
xmlDoc.async = False
xmlDoc.load(Path)
End Function
'********************************************************************************
' End of Function LoadXMLFile
'********************************************************************************
'********************************************************************************
' Function GetXMLElement
'********************************************************************************
Public Function GetXMLElement(xmlFilePath, xmlNode, xmlElement)
Dim CurrentNode, CurrentValue
LoadXMLFile(xmlFilePath)
Set CurrentNode = xmldoc.selectSingleNode(xmlNode)
Set CurrentValue = CurrentNode.getElementsByTagName(xmlElement)
GetXMLElement = CurrentValue.item(0).Text
End Function
'********************************************************************************
' End of Function GetXMLElement
'********************************************************************************
MSDN Integration
extern.Declare micLong,"GetForegroundWindow","user32.dll","GetForegroundWindow"
hwnd = extern.GetForegroundWindow()
If hwnd = 0 Then
Msgbox "Window Not Found"
ExitRun
Else
Msgbox "Window Found with Handle ”&hwnd
End if
Timed Msg-Box
MsgBoxTimeout (“Sample Text”,”Timed MsgBox”, 10)
Public Sub MsgBoxTimeout (Text, Title, TimeOut)
Set WshShell = CreateObject("WScript.Shell")
WshShell.Popup Text, TimeOut, Title
End Sub
Text Location
l = -1 ‘Left
t = -1 ‘Top
r = -1 ‘Right
b = -1 ‘Bottom
Succeeded = TextUtil.GetTextLocation("16",0,l,t,r,b)
If Not Succeeded Then
MsgBox "Text not found"
else
x = (l+r) / 2
y = (t+b) / 2
Set dr = CreateObject("Mercury.DeviceReplay")
dr.MouseClick x, y, 0
End If
Keystroke Functions
'An example that presses a key using DeviceReplay.
Set obj = CreateObject("Mercury.DeviceReplay")
Window("Notepad").Activate
obj.PressKey 63
keyboard Values

Mouse Click Events
Solution: Use the custom user-defined sub RightMenuSelect
NOTE:
This function/sub is not part of Astra QuickTest/QuickTest Professional. It is not guaranteed to work and is not supported by Mercury Interactive Technical Support. You are responsible for any and all modifications that may be required.
The RightMenuSelect function selects the menu item at index "idx" from the pop-up menu that appears when right-clicking on an object.
Sub RightMenuSelect (menu, idx)
Set obj = CreateObject("Mercury.DeviceReplay")
Set WshShell = CreateObject("WScript.Shell")
menu.MakeObjVisible
x = menu.QueryValue("abs_x")
y = menu.QueryValue("abs_y")
obj.MouseClick x+5, y+5, 2
For i = 1 To idx
WshShell.sendKeys "{DOWN}"
Next
WshShell.sendKeys "{ENTER}"
set WshShell = nothing
Set obj = nothing
End Sub
Device Replay object to perform a right click operation on any object by retrieving the coordinates of the object.
Sub RightClickObj(Obj, Offset_x, Offset_y)
x_coord = Obj.GetROProperty("abs_x")
y_coord = Obj.GetROProperty("abs_y")
Set dr = CreateObject("Mercury.DeviceReplay")
dr.MouseClick x_coord + Offset_x, y_coord + Offset_y, 2
End Sub
HTML Functions
Syntax:
Browser(“Browser”).Page(“Page").Object.documentElement.innerHTML
Example:
htmlSrc = Browser("Welcome to HP-GDIC").Page("Welcome: Mercury Tours").Object.documentElement.innerHTML
Msgbox htmlSrc
System Operations
Running and Closing Applications Programmatically
Syntax:
SystemUtil.Run “file, [params], [dir] “
Example:
SystemUtil.Run “notepad.exe myfile.txt “
Clipboard Objects
The object has the same methods as the Clipboard object available in Visual Basic:
Clear
GetData
GetFormat
GetText
SetData
SetText
Set cb = CreateObject("Mercury.Clipboard")
cb.Clearcb.SetText "TEST"
MsgBox cb.GetText
1 comment:
Hi this is a nice info.
In XML Part you use an function "GetAttributeValue(xmlElement, xmlAttribute)".
But this function is not implemented.
Can you say how the function gets the attributes.
thanks
ralf
Post a Comment