Advanced QTP © Satyam Computer Services Limited 1 Objectives S.No. Topics Total hours Working with QTP object model reference Web objects Windows objects Utility objects Working with dynamic objects File System Handling Enhancing Your test using VB Scripts Creating User defined function/procedure Working with Associated library files Registering User-Defined Functions as Test Object Methods Registering a Function 1 Registering User-Defined Functions as Test Object Methods 6 hours Enhancing Your Tests using the Windows API Working with Email application (outlook) How to access the data from excel 2 XML file handling © Satyam Computer Services Limited 2 hours 2 Object Model Reference • An object model is a structural representation of software objects (classes) that comprise the implementation of a system or application • An object model defines a set of classes and interfaces, together with their properties, methods and events, and their relationships © Satyam Computer Services Limited 3 What is Quick test automation object model? • It is a model to represent a real time objects which are there in the application. • We can use the objects, its methods and properties to customize our testing needs © Satyam Computer Services Limited 4 Example • In Esupport, to access the user name box the hierarchy is Browser(“satyam virtual universe”).page(“satyam virtual unirverse”).webedit(txtusername) © Satyam Computer Services Limited 5 Web Objects • Browser- The Web browser. The name of the Browser test object is the same as the first recorded Page object • Page - An HTML page • Webedit - An edit box, usually contained inside a form • Webbutton - An HTML button. • Webcheckbox - A check box with an ON and OFF state. • WebTable - A table containing a variable number of rows and columns © Satyam Computer Services Limited 6 Some examples on web objects • Login to the esupport and viewing the leave balance • Login to the esupport and opening the power on to view the current status © Satyam Computer Services Limited 7 Windows objects • Dialog – A Windows dialog box • Window – A standard window. • WinEdit – A Windows edit box. • WinCheckBox - A Windows check box • WinEdit - A Windows edit box • WinButton - A Windows button. © Satyam Computer Services Limited 8 Utility objects • QuickTest reserved objects can be used for testing and result reporting preferences during a run session. © Satyam Computer Services Limited 9 Utility objects • • • • • • • • • • Crypt Object DataTable Object Description Object DotNetFactory Object DTParameter Object DTSheet Object Environment Object Extern Object LocalParameter Object MercuryTimers Object (Collection) • MercuryTimer Object © Satyam Computer Services Limited • • • • • • • • • • • • Parameter Object PathFinder Object Properties Object (Collection) QCUtil Object RandomNumber Object Recovery Object Reporter Object Repository Object Services Object Setting Object TSLTest Object XMLUtil Object 10 Description object “Description” - Creates a new, empty description object in which you can add collection of properties and values in order to specify the description object in place of a test object name in a step. © Satyam Computer Services Limited 11 Example set EditDesc = Description.Create() EditDesc("Name").Value = "userName" EditDesc("Index").Value = "0" Browser("Welcome: Mercury").Page("Welcome: Mercury").WebEdit(EditDesc).Set "MyName” © Satyam Computer Services Limited 12 Crypt Object • Encrypts a string Syntax Crypt.Encrypt(value) Example x = "mercury" msgbox Crypt.Encrypt(x) © Satyam Computer Services Limited 13 Dynamic objects • Programmatic descriptions enable us to create temporary versions of test objects to represent objects from our application. • We can perform operations on those objects without referring to the object repository. • For example, suppose an edit box was added to a form on your Web site. You could use a programmatic description to add a statement in a user-defined function that enters a value in the new edit box, so that QuickTest can identify the object even though you never recorded on the object or added it to the object repository © Satyam Computer Services Limited 14 Syntax • TestObject("PropertyName1:=PropertyValue1", "..." , "PropertyNameX:=PropertyValueX") • TestObject—the test object class. • PropertyName:=PropertyValue—the test object property and its value. Each property:=value pair should be separated by commas and quotation marks © Satyam Computer Services Limited 15 Example • Browser("Mercury Tours").Page("Mercury Tours").WebEdit("Name:=Author", "Index:=3").Set "Mark Twain” • Window("Text:=Myfile.txt - Notepad").Move 50, 50 Window("Text:=Myfile.txt -Notepad").WinEdit("AttachedText:=Find what:").Set "hello" Window("Text:=Myfile.txt - Notepad").WinButton("Caption:=Find next").Click © Satyam Computer Services Limited 16 File system objects The FileSystemObject (FSO) object model allows us to use the familiar object.method syntax with a rich set of properties, methods, and events to process folders and files. © Satyam Computer Services Limited 17 Creating file system object Dim fso Set fso = CreateObject("Scripting.FileSystemObject") © Satyam Computer Services Limited 18 File operations Dim fso, f1 Set fso = CreateObject("Scripting.FileSystemObject") Set f1 = fso.CreateTextFile("c:\testfile.txt", True) Set f1 = fso.opentextfile("C:\test",2) ‘1 – Reading , 2 – Writing, 9 - Appending f1.writeline "hai you are good" © Satyam Computer Services Limited 19 Copying file Dim fso Set fso = createobject("Scripting.FileSystemObject") fso.copyfile "C:\test","C:\test1“ ‘copyfile source,destination,true or false ‘true – if file exists, overwrites ‘false – if file exists, does not copy © Satyam Computer Services Limited 20 Methods • • • • • • • • • • • • • • Add Method (Dictionary) Add Method (Folders) BuildPath Method Close Method Copy Method CopyFile Method CopyFolder Method CreateFolder Method CreateTextFile Method Delete Method DeleteFile Method DeleteFolder Method DrivesExists Method Exists Method © Satyam Computer Services Limited • FileExists Method • FolderExists Method • GetAbsolutePathName Method • GetBaseName Method • GetDrive Method • GetDriveName Method • GetExtensionName Method • GetFile Method • GetFileName Method • GetFileVersion Method • GetFolder Method • GetParentFolderName Method • GetSpecialFolder Method 21 Methods • • • • • • • GetTempName Method Items Method Keys Method Move Method MoveFile Method MoveFolder Method OpenAsTextStream Method • OpenTextFile Method • Read Method • ReadAll Method © Satyam Computer Services Limited • • • • • • • • ReadLine Method Remove Method RemoveAll Method Skip Method SkipLine Write Method WriteBlankLines Method WriteLine Method 22 Demo on file system • Counting no of lines in a file • File existence check • Comparing text files © Satyam Computer Services Limited 23 RegisterUserFunc utility • Enables us to add new methods to test objects or change the behavior of an existing test object method during a run session • When we use this statement, QuickTest uses our user-defined function as a method of a specified test object class for the remainder of a run session, or until we unregister the method. © Satyam Computer Services Limited 24 Syntax • RegisterUserFunc TOClass, MethodName, FunctionName, SetAsDefault Argument Type Description TOClass String The test object class for which you want to register the method. MethodName String The method you want to register. FunctionName String The name of your user-defined function. The function can be located in your action or in any library file associated with your test or component. SetAsDefault Boolean Indicates whether the registered function is used as the default method for the test object. © Satyam Computer Services Limited 25 Example • The following example uses the RegisterUserFunc method to create a new Copy method for the WinEdit test object that copies all the text in an edit box to the Windows Clipboard. • Sub Copy (edit) Edit.Click 3, 3 Edit.SetSelection 0, Len(Edit.GetROProperty("text")) Edit.Type micCtrlDwn + "c" + micCtrlUp End Sub RegisterUserFunc "WinEdit", "Copy", "Copy" ' Now you can call the new method Dialog("Login").WinEdit("Agent Name:").Copy © Satyam Computer Services Limited 26 Working with API functions • Extern object – It enables us to declare calls to external procedures from an external dynamic-link library (DLL). • Declare Method - Declares references to external procedures in a dynamic-link library (DLL). • Once we use the Declare method for a method, we can use the Extern object to call the declared method © Satyam Computer Services Limited 27 Syntax • Extern.Declare(RetType, MethodName, LibName, Alias [, ArgType(s)]) Argument Type Description RetType String Data type of the value returned by the method. MethodName String Any valid procedure name. LibName String Name of the DLL or code resource that contains the declared procedure. Alias String Name of the procedure in the DLL or code resource. String A list of data types representing the data types of the arguments that are passed to the procedure when it is called. ArgType(s) © Satyam Computer Services Limited 28 Example 'Declare FindWindow method Extern.Declare micHwnd, "FindWindow", "user32.dll", "FindWindowA", micString, micString 'Declare SetWindowText method Extern.Declare micLong, "SetWindowText", "user32.dll", "SetWindowTextA", micHwnd, micString 'Get HWND of the Notepad window hwnd = Extern.FindWindow("Notepad", vbNullString) if hwnd = 0 then MsgBox "Notepad window not found" end if 'Change the title of the notepad window res = Extern.SetWindowText(hwnd, "kuku") © Satyam Computer Services Limited 29 Working with outlook express • QuickTest Professional does not have special support for the Outlook application, so there is no built-in method that will locate an e-mail and allow you to view the attachments. • However, Microsoft does have an Object Model for Outlook that can be used to automate many of the features. • You can refer to "Microsoft Outlook 2000 Object Model" for a complete listing of Outlook methods and properties that can be used within a QuickTest Professional (QTP) script. © Satyam Computer Services Limited 30 How to open outlook thru QTP? Set myOlApp = CreateObject("Outlook.Application") ‘to create an object Set myNameSpace = myOlApp.GetNameSpace("MAPI") ‘to create an name space Set myFolder= myNameSpace.GetDefaultFolder(6) ‘to open inbox (inbox is the sixth folder) myFolder.Display © Satyam Computer Services Limited 31 Outlook methods • Items.count - To display the total mails in the Inbox Set myOlApp = CreateObject("Outlook.Application") Set myNameSpace = myOlApp.GetNameSpace("MAPI") Set myFolder= _ myNameSpace.GetDefaultFolder(6) myFolder.Display msgbox myfolder.items.count © Satyam Computer Services Limited 32 Sending Mails Set myOlApp = CreateObject("Outlook.Application") ‘ to create object for outlook Set mail = myOlApp.CreateItem(0) ‘to click on the new message mail.to = "chandrasekar_venkata“ ‘to edit the mail id mail.body = "Hai user, Welcome“ ‘to edit the subject mail.display ‘to display the outlook mail.send ‘to send the mail © Satyam Computer Services Limited 33 Displaying how many unread mails are there Set myOlApp = CreateObject("Outlook.Application") Set myNameSpace = myOlApp.GetNameSpace("MAPI") Set myFolder= myNameSpace.GetDefaultFolder(6) myFolder.Display msgbox myFolder.UnReadItemCount ‘for displaying how many items are unread till now © Satyam Computer Services Limited 34 Working with Excel • • • • • Opening workbook Opening worksheets Reading data from Excel sheet Writing data to Excel sheet Saving data to Excel sheet © Satyam Computer Services Limited 35 Opening workbook Set xl = CreateObject("Excel.Application") ‘to create object for the excel object xl.workbooks.open "c:\template.xls“ ‘to open file © Satyam Computer Services Limited 36 Opening worksheets Set xl = CreateObject("Excel.Application") xl.workbooks.open "c:\template.xls“ xl.sheets("sheet1").activate © Satyam Computer Services Limited 37 Reading data from Excel Set xl = CreateObject("Excel.Application") xl.workbooks.open "c:\template.xls“ xl.sheets("sheet1").activate msgbox xl.sheets("sheet1").cells(2,2) xl.quit © Satyam Computer Services Limited 38 Writing data to Excel Set xl = CreateObject("Excel.Application") xl.workbooks.open "c:\template.xls“ xl.sheets("sheet1").activate xl.sheets("sheet1").cells(2,2) = "mercury123" © Satyam Computer Services Limited 39 Saving file Set xl = CreateObject("Excel.Application") xl.workbooks.open "c:\template.xls“ xl.sheets("sheet1").activate xl.sheets("sheet1").cells(2,2) = "mercury123“ xl.Save xl.quit © Satyam Computer Services Limited 40 Working with XML object QuickTest provides several scripting methods that we can use with XML data. We can use these scripting methods to retrieve data and return new XML objects from existing XML data. © Satyam Computer Services Limited 41 XMLUtil The object used to access and return XML objects. Syntax Set Object = XMLUtil.CreateXML() © Satyam Computer Services Limited 42 Loading XML file Set XMLObj = XMLUtil.CreateXML() XMLObj.LoadFile("C:\XML\BookStore.xml") © Satyam Computer Services Limited 43 Example Code for replacing the value in XML file using XML Object: Set doc = XMLUtil.CreateXML() doc.LoadFile "C:\Documents and Settings\ss21189\Desktop\mails.xml" Set root = doc.GetRootElement() Set children = root.ChildElements() if children.Count() <> 0 then Set firstChild = children.Item(1) firstChildName = firstChild.ElementName() Set sameNameChildren = children.AllItemsByName(firstChildName) msg = "My first child name is " + firstChildName + " and I have " + FormatNumber(sameNameChildren.Count(),0) + " of them." End If msgbox msg set firstChild=root.ChildElements().Item(2) © Satyam Computer Services Limited 44 Example Set child2 = firstChild.ChildElements().Item(2) Msgbox child2.Value() child2.SetValue("replacement") firstChild.AddChildElementByName "Value", "replacement" Dim fso, MyFile Set fso = CreateObject("Scripting.FileSystemObject") Set MyFile = fso.OpenTextFile("C:\Documents and Settings\ss21189\Desktop\mails.xml") MsgBox MyFile.ReadAll MyFile.Close © Satyam Computer Services Limited 45 Summary • • • • • • • • • Object Model Reference Utility objects Dynamic objects File system Registering user defined functions Working with Win32 API functions Working with outlook object Working with Excel application Working with XML application © Satyam Computer Services Limited 46