QTP自动化测试权威指南 连载(五)

发表于:2013-6-19 14:46

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:赵旭斌 阙勇等译    来源:51Testing软件测试网

  微软Windows应用编程接口(API)为开发Windows应用程序提供了很多构建好的模块。它提供各种操作方法,比如获取鼠标坐标,窗口句柄,颜色等。QTP支持调用动态链接库内定义的这些方法,但是由于VBScript的限制,只有有限的一部分API可以在QTP中使用。

  本章使用的API的更多细节和信息可以从MSDN或者Visual Studio的API Viewer工具查询获得。

  Extern对象

  QTP提供一个Extern功能对象用来声明和调用API。

  语法

Extern.Declare (RetType, MethodName, LibName, Alias [, ArgType(s)])

  更多细节可参考QTP用户手册。

  VB API定义语法

Private Declare Function GetForegroundWindow Lib "user32.dll" () As Long

  对以上API,我们需要使用QTP的Declare方法确定使用的合适参数:

  RetType = micLong(函数返回类型)

  MethodName = "GetForegroudWindow" (我们可以使用任何其他名字,但是比较好的做法是使用实际API的名字)

  LibName = "user32.dll" (如果不是使用Windows系统自带的DLL,那么文件名必须使用绝对路径,例如"C:\MyApp\Lib\mylib.dll")

  Alias = "" 或者"GetForegroudWindow"(如果MethodName跟Alias相同,则Alias可以为空值。

  ArgType(s) =  本例中不需要传入参数)。

  QTP API定义

Extern.Declare micLong,"GetForegroundWindow","user32.dll","GetForegroundWindow"

  以下用一些用例来演示使用API解决通常遇到的问题。

  问题 17-1.如何判断当前桌面最上面的为浏览器窗口

'声明GetForeGroundWindow API
extern.Declare micLong,"GetForegroundWindow","user32.dll","GetForegroundWindow"

'获取最前面窗口的句柄
hwnd = extern.GetForegroundWindow()

'判断是否有包含此句柄的浏览器窗口
isBrowser = Browser("hwnd:=" &hwnd).Exist()
 
If isBrowser then
Msgbox "The top most window is a browser"
End if

  问题 17-2.如何获得Windows的环境变量 (注意不是QTP环境变量)

'变量声明
Dim s_EnvValue

'声明 API "GetEnvironmentVariable"
Extern.Declare micLong,"GetEnvironmentVariable","kernel32.dll","GetEnvironmentVariableA", _
micString,micString+micByRef,micLong

'获取全局变量 "TEMP" 值
Extern.GetEnvironmentVariable "TEMP",s_EnvValue,255 '会得到临时目录路径
MsgBoxs_EnvValue

  问题 17-3.如何使用Windwos API选中Listbox里的选项(item)

' 声明API
Extern.Declare micLong,"SendMessage","user32.dll","SendMessageA",micLong,micLong,micLong,micLong

' 设置Listbox选项item的消息
Const LB_SETSEL = &H185

'方法:根据index选中复选框
Function CheckListBox(hwnd, index)
extern.SendMessagehwnd, LB_SETSEL, True, index
end function

'方法:根据index取消选中复选框
Function UnCheckListBox(hwnd, index)
extern.SendMessagehwnd, LB_SETSEL, False, index
end function

31/3123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号