320 likes | 532 Views
QTP Training (Advanced). Prepared By : Manjeet Kumar Created On : 27-09-2010. 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. Summary of Day 3.
E N D
QTP Training (Advanced) Prepared By : Manjeet Kumar Created On : 27-09-2010
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
Summary of Day 3 • Object Identification • Synchronization Points • Exception Handling • User Defined Objects & Virtual Objects • Actions • Parameterization
Continuation of Day – 3 Demo • Demo on • Exception handling using function calls • Descriptive Programming • Check points • AOM (Automation 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
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 • ' *********************************************************************************************
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 • ' *********************************************************************************************
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 • ' *********************************************************************************************
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
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"
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
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
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 • '********************************************************************************
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 • '********************************************************************************
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 • '********************************************************************************
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
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
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 • '********************************************************************************
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
QTP Utility Functions – TimedMsg-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
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
QTP Utility Functions – Keystroke Functions • 'An example that presses a key using DeviceReplay. • Set obj = CreateObject("Mercury.DeviceReplay") • Window("Notepad").Activate • obj.PressKey 63
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
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
QTP Utility Functions – 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
QTP Utility Functions – System Operations • Running and Closing Applications Programmatically • Syntax: • SystemUtil.Run “file, [params], [dir] “ • Example: • SystemUtil.Run “notepad.exe myfile.txt “
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