---
title: "QTP录制Delphi客户端"
author: "uin952dg"
date: "2012-01-20T01:35:03Z"
tags: []
categories: ["非技术"]
slug: "qtp-delphi"
draft: false
lastmod: "2012-01-20T01:35:03Z"
id: 1361448
url: "https://uin952dg.iteye.com/blog/1361448"
---

QTP录制Delphi客户端 
2010年08月19日
　　虽然Delphi现在已经是强弩之末了，但是很多一些大型应用，如银行，电信，保险业，由于其性能和安全性的考虑，并未对软件做界面的升级，所以目前绝大部分关键型应用，仍然有很多都是基于Delphi设计的客户端。由于我之前正是参与一个前台基于Delphi开发的银行客户端平台，在采用QTP对某些核心功能进行自动化时，遇到很多问题。 
　　现在QTP已经是到10版本了，我目前还在用的是9.2，比较成熟的版本。但是在用它录制Delphi程序时，发现很多控件并不能识别。因为QTP它默认可以识别Windows标准控件，但是银行很多的一些功能或者按钮控件都是封装windows标准控件，导致QTP并不能识别，加之QTP识别Windows控件是采用Windows_id，而Delphi并没有，所以基本上录制出来的脚本，并不能正确执行。解决这个问题，QTP本身有好几种解决方案： 
　　1、安装QTP Delphi控件，但是QTP9.2官网并没有给出这个add-in，有一个比较牵强的办法，就是安装8.2版本的Delphi控件，9.2版本是可以正确识别的。安装后便可以识别Delphi窗口按钮等控件。有些人可能会遇到录制界面死掉的问题，这个目前还解决不了，可以采用方法2 
　　2、QTP为了保证能够录制大部分C/S程序，采用了一种对象映射机制来保证它可以识别非标准控件。它采取的办法，就是用待测软件的对象映射到它可以识别的标准控件。 
　　基于软件的操作方法如下： 
　　1.) 选择"Tools">>"Object Identification"； 
　　2.) 在"Environment"框中选择"Standard Windows"，则"User-Defined…"按钮将变为可用； 
　　3.) 点击"User-Defined…"按钮，将显示"Object Mapping"对话框； 
　　4. ) 单击指向手，然后单击要将其类作为用户定义的类添加的对象。用户定义的对象的名称将显示在"Class Name"框中。（其实这个"Class Name"框也可以自己填，如果自己清楚该填什么的话） 
　　提示：按住Ctrl键，可以更改窗口焦点或执行右键单击或鼠标悬停（以显示上下文菜单）等操作。注意：按Ctrl键时，您不能从Windows任务栏中选择应用程序，因此，必须确保要访问的窗口没有最小化。 
　　5.) 在"Map to"框中，选择要将用户定义的对象类映射到的标准对象类，然后单击"Add"。类名和映射将添加到对象映射列表中。 
　　6.) 如果要将其他对象映射到标准类，请对每个对象重复步骤4-5。 
　　7.)  单击"OK"。"Object Mapping"对话框关闭，您的对象作为用户定义的测试对象添加到标准Windows测试对象类列表中 
　　此外，你也可以采用编辑VBS可执行脚本方式，来将你的空间做映射： 
　　这里只举一个例子，比如添加一个自定义的toolbar映射到Windows标准toolbar控件，代码如下 
　　Dim App 'As Application     //定义启动应用程序对象 
　　Set App = GetObject("","QuickTest.Application")  //启动QTP应用 
　　'Configuration of user-defined objects 
　　'Object identification configuration for user-defined object "ttoolbar" 
　　App.Options.ObjectIdentification("WinToolbar").Cre ateUserDefinedObject("ttoolbar") 
　　App.Options.ObjectIdentification("ttoolbar").Ordin alIdentifier = "location" 
　　App.Options.ObjectIdentification("ttoolbar").Manda toryProperties.RemoveAll() 
　　App.Options.ObjectIdentification("ttoolbar").Manda toryProperties.Add("nativeclass") 
　　App.Options.ObjectIdentification("ttoolbar").Manda toryProperties.Add("text") 
　　App.Options.ObjectIdentification("ttoolbar").Assis tiveProperties.RemoveAll() 
　　App.Options.ObjectIdentification("ttoolbar").Assis tiveProperties.Add("window id") 
　　App.Options.ObjectIdentification("ttoolbar").Enabl eSmartIdentification = False 
　　App.Options.ObjectIdentification("ttoolbar").BaseF ilterProperties.RemoveAll() 
　　App.Options.ObjectIdentification("ttoolbar").Optio nalFilterProperties.RemoveAll() 
　　基本格式就如上面，如果你要新增某些其他识别该空间的变量，就是用App.Options.ObjectIdentification("ttoolbar").Manda toryProperties.Add("nativeclass") 
　　这样，将你的程序编写好后，便可以在QTP中运行，结果就是，你的新增对象会被导入到【Standard Windows】中。当然，你也可以在Tools->Object Identification,导出你通过软件新增的映射对象，格式为vbs，这样你就可以再不同机器上是用你的对象组了，这非常有利i项目团队去共享。 
　　3、另外，时常关注QTP新版本的信息，其实QTP10 版本，已经支持Delphi了，ReadMe更新如下： 
　　New Supported Operating Systems and Environments QuickTest Professional 10.00 now offers support for the operating systems, browsers, and development environments listed below. For a complete list of all supported operating systems, browsers, and development environments, see the HP QuickTest Professional 10.00 Product Availability Matrix. lMicrosoft Windows 2008 Server 32-bit Edition lMicrosoft Windows 2008 Server 64-bit Edition lMicrosoft Windows Vista, Service Pack 1, 32-bit Edition lMicrosoft Windows Vista, Service Pack 1, 64-bit Edition lMicrosoft Windows XP Professional 32-bit Edition-Service Pack 3（9.5下也可以用，不过我发现先开Foxmail，再开QTP会报错，但先开QTP，再开Foxmail没有问题） lCitrix Presentation Server 4.5 lMicrosoft Internet Explorer 8, Beta 2 lMozilla Firefox 3.0.x（终于支持了，9.5可是不支持的） lDelphi: IDE, versions 6, 7, and 2007 (for controls based on the Win32 VCL library) lSAP: CRM 2007 (For controls that support test mode enhancements. Requires SAP notes: 1147166, 1066565, and 1002944. Later SAP notes related to test mode enhancements are not supported.) lJava: IBM 32-bit JDK 1.5.x, SWT toolkit version 3.4 lJava Extensibility:Eclipse IDE 3.4 l.NET: .NET Framework 3.5-Service Pack 1 --------EOF---------