Qtp Training Deepti 4 Of 4493
-
Upload
azhar-satti -
Category
Business
-
view
2.891 -
download
0
description
Transcript of Qtp Training Deepti 4 Of 4493
© 2005 Hewlett-Packard Development Company, L.P.The information contained herein is subject to change without notice
QTP Training (Advanced)
Shanmugadas.C.SCreated On : 04-10-2005
April 10, 2023 2
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
April 10, 2023 3
Summary of Day 3• Object Identification• Synchronization Points• Exception Handling• User Defined Objects & Virtual Objects• Actions• Parameterization
April 10, 2023 4
Continuation of Day – 3 Demo• Demo on−Exception handling using function calls−Descriptive Programming−Check points−AOM (Automation Object Model)
April 10, 2023 5
Automation Object Model (Contd.,)
April 10, 2023 6
Automation Object Model (Contd.,)
April 10, 2023 7
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
April 10, 2023 8
QTP Utility Functions – File Operations
File Creation
1. CreateFile "C:","mytextfile.txt","hi how are you?"
2. Public Function CreateFile(filpath,filname,filcontent)
3. xml_file = filpath & "\" & filname
4. Dim fileobject, tf
5. Set fileobject = CreateObject("Scripting.FileSystemObject")
6. Set tf = fileobject.CreateTextFile(xml_file, True)
7. tf.Write (filcontent)
8. tf.Close
9. End Function
April 10, 2023 9
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• ' *********************************************************************************************
April 10, 2023 10
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 line from file• • ' Parameters:• ' FileRef - reference to the file• ' *********************************************************************************************
April 10, 2023 11
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• ' *********************************************************************************************
April 10, 2023 12
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)• End Sub
April 10, 2023 13
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"
April 10, 2023 14
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
April 10, 2023 15
QTP Utility Functions – Email Operations
• SendMail "[email protected]","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
April 10, 2023 16
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• '********************************************************************************• ' End of Function UpdateXMLAttribute• '********************************************************************************
April 10, 2023 17
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• '********************************************************************************• ' End of Function UpdateXMLElementData• '********************************************************************************
April 10, 2023 18
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• '********************************************************************************
April 10, 2023 19
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
April 10, 2023 20
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
April 10, 2023 21
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• '********************************************************************
************
April 10, 2023 22
QTP Utility Functions – 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
April 10, 2023 23
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
April 10, 2023 24
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
April 10, 2023 25
QTP Utility Functions – Keystroke Functions
• 'An example that presses a key using DeviceReplay.
• Set obj = CreateObject("Mercury.DeviceReplay")
• Window("Notepad").Activate
• obj.PressKey 63
April 10, 2023 26
QTP Utility Functions – keyboard Values
April 10, 2023 27
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
April 10, 2023 28
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
• End Sub
April 10, 2023 29
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
April 10, 2023 30
QTP Utility Functions – System Operations
• Running and Closing Applications Programmatically
• Syntax:• SystemUtil.Run “file, [params], [dir] “
• Example:• SystemUtil.Run “notepad.exe myfile.txt “
April 10, 2023 31
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.Clearcb.SetText "TEST"MsgBox cb.GetText
April 10, 2023 32