You are on page 1of 32

QTP Training

(Advanced)

Shanmugadas.C.S
Created On : 04-10-2005

© 2005 Hewlett-Packard Development Company, L.P.


The information contained herein is subject to change without notice
Agenda
• Summary of Day 3 QTP Advanced
Training
• Continuation of Day – 3 Demo
− Exception handling
− Descriptive programming
− Check point
− AOM (Automation Object Model)
• QTP Utility Functions

October 15, 2008 2


Summary of Day 3
• Object Identification
• Synchronization Points
• Exception Handling
• User Defined Objects & Virtual Objects
• Actions

• Parameterization

October 15, 2008 3


Continuation of Day – 3 Demo
• Demo on
− Exception handling using function calls
− Descriptive Programming
− Check points
− AOM (Automation Object Model)

October 15, 2008 4


Automation Object Model
(Contd.,)

October 15, 2008 5


Automation Object Model
(Contd.,)

October 15, 2008 6


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
October 15, 2008 7
QTP Utility Functions – File
Operations
 File Creation

3. CreateFile "C:","mytextfile.txt","hi how are you?"


4. Public Function CreateFile(filpath,filname,filcontent)
5. xml_file = filpath & "\" & filname
6. Dim fileobject, tf
7. Set fileobject = CreateObject("Scripting.FileSystemObject")
8. Set tf = fileobject.CreateTextFile(xml_file, True)
9. tf.Write (filcontent)
10. tf.Close
11. End Function

October 15, 2008 8


QTP Utility Functions – File
Operations
• dim oFSO
• ' creating the file system object
• set oFSO = CreateObject ("Scripting.FileSystemObject")

• 'Option Explicit
• ' *********************************************************************************************
• ' Create a new txt file

• ' Parameters:
• ' FilePath - location of the file and its name
• ' *********************************************************************************************
• Function CreateFile (FilePath)
• ' varibale that will hold the new file object
• dim NewFile
• ' create the new text ile
• set NewFile = oFSO.CreateTextFile(FilePath, True)
• set CreateFile = NewFile
• End Function

• ' *********************************************************************************************
• ' Check if a specific file exist

• ' Parameters:
• ' FilePath - location of the file and its name
• ' *********************************************************************************************

October 15, 2008 9


QTP Utility Functions – 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
October 15,line
2008 from file 10
QTP Utility Functions – File
Operations
• 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
• ' *********************************************************************************************

October 15, 2008 11


QTP Utility Functions – File
Operations
• ' *********************************************************************************************
• 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)
October 15, 2008 12
QTP Utility Functions – File
Operations
• ' ************** 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"

October 15, 2008 13


QTP Utility Functions – 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
October 15, 2008 14
QTP Utility Functions – 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

October 15, 2008 15


QTP Utility Functions - 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
• '********************************************************************************
October 15, 2008 16
QTP Utility Functions - XML
• '****************************************************************************
****
• ' 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
• '****************************************************************************
****
• ' End15,of2008
October Function UpdateXMLElementData 17
QTP Utility Functions - XML
• ' 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
• '********************************************************************************
October 15, 2008 18
QTP Utility Functions - XML
• ' Function GetAttributeValue
• Public Function GetAttributeValue(xmlElement, xmlAttribute)

• Dim sIdValue
• Set nodeBook = xmlDoc.selectSingleNode(xmlElement)
• sIdValue = nodeBook.getAttribute(xmlAttribute)
• GetAttributeValue = sIdValue

• End Function
• '********************************************************************************
• ' End of Function GetAttributeValue
• '********************************************************************************

• '********************************************************************************
• ' Function ReplaceAttributeValue
• '********************************************************************************
• Public Function ReplaceAttributeValue (xmlElement, xmlAttribute, NewXMLValue)

• Set nodeBook = xmlDoc.selectSingleNode(xmlElement)


• nodeBook.setAttribute xmlAttribute, NewXMLValue

• End Function
• '********************************************************************************
• ' End of Function ReplaceAttributeValue

October 15, 2008 19


QTP Utility Functions - XML
• ' Function SaveXMLFile
• Public Function SaveXMLFile (SavePath)

• xmlDoc.save(SavePath)

• End FUnction
• '********************************************************************************
• ' End of Function SaveXMLFile
• '********************************************************************************

• '********************************************************************************
• ' Function XMLError
• '********************************************************************************
• Public Function XMLError()

• Dim myErr
• Set myErr = xmlDoc.parseError
• MsgBox("XML Error : " & myErr.reason)

• End Function
• '********************************************************************************
• ' End of Function XMLError
October 15, 2008 20
QTP Utility Functions - XML
• '********************************************************************
************
• ' 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
• '********************************************************************
************
October 15, 2008 21
QTP Utility Functions – MSDN
Integration

• extern.Declare
micLong,"GetForegroundWindow","user32.dll","GetForegroundW
indow"

• hwnd = extern.GetForegroundWindow()

• If hwnd = 0 Then
• Msgbox "Window Not Found"
• ExitRun
• Else
• Msgbox "Window Found with Handle ”&hwnd

October 15, 2008 22


QTP Utility Functions – 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

October 15, 2008 23


QTP Utility Functions – 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

October 15, 2008 24


QTP Utility Functions – Keystroke
Functions

• 'An example that presses a key using


DeviceReplay.
• Set obj =
CreateObject("Mercury.DeviceReplay")

• Window("Notepad").Activate

• obj.PressKey 63

October 15, 2008 25


QTP Utility Functions – keyboard
Values

October 15, 2008 26


QTP Utility Functions – 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
October 15, 2008 27
QTP Utility Functions – Mouse Click
Events

• 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

October 15, 2008 28


QTP Utility Functions – HTML Functions

• Syntax:
• Browser(“Browser”).Page(“Page").Object.documen
tElement.innerHTML

• Example:
• htmlSrc = Browser("Welcome to HP-
GDIC").Page("Welcome: Mercury
Tours").Object.documentElement.innerHTML

• Msgbox htmlSrc

October 15, 2008 29


QTP Utility Functions – System
Operations

• Running and Closing Applications


Programmatically
• Syntax:
• SystemUtil.Run “file, [params], [dir] “

• Example:
• SystemUtil.Run “notepad.exe myfile.txt “

October 15, 2008 30


QTP Utility Functions – 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.Clear
cb.SetText "TEST"
MsgBox cb.GetText
October 15, 2008 31
October 15, 2008 32

You might also like