-
QTP编程小结
2008-10-04 14:44:19
例1:获取单元格中的值
thisText = Browser(…).Page(…).Frame.(…).WebTable("sample").GetCellData(2,1)
例2:获取图片的名称
ObjectName = Browser(…).Page(…).Image("Find").GetProperty("Name")
例3:检查某个对象是否存在,如果存在弹出对话框说明对象存在。
If Browser("Browser").Page("Page").Applet("login.html").JavaEdit("username").Exist Then
MsgBox("The object exists.")
End if
例4:描述性编程(descrīptive programming)
1、descrīptive programming概述
通常情况下,当在录制一个操作时,QTP会将被操作对象加入到对象库里(Object Repository)。一旦对象存在于对象库里,我们就可以在专家视图里通过添加相关的对象方法来对该对象进行操作。我们可以通过引用层次型对象库里的对象描述(Object Descrīption)来添加相应的方法。
因为QTP对象库中的每个对象都具有唯一名称,所以在引用时对象名是必须需要指定的。然后在测试运行期间,QTP在对象库中根据这个对象的名称和父对象来查找对象,并使用为这个测试对象存储的测试对象描述,在网站或应用程序中标识该对象。
例如我们用QTP录制Yahoo Mail登录情况时我们需要输入用户名,于是在录制时我们就会录下一个WebEdit对象,它的缺省逻辑名为“login”,该编辑字段位于名为“Yahoo! Mail - The best” 的页面上,并且该页面在浏览器中使用名称Yahoo!进行录制。如图所示,即为录制时的对象库的内容:
那么如果我们想要应用该对象,就可以在专家视图输入以下信息:
Browser("Yahoo!").Page("Yahoo! Mail - The best").WebEdit("login").Set “xxx”
或者我们也可以调用一些方法,获取改对象在运行时的对象名,如:
Browser("Yahoo!").Page("Yahoo! Mail - The best").WebEdit("login").GetROProperty(“name”)
然而,我们可以发觉到,上面的例子在处理对象时,对象已经存在于对象库里,因此我们可以应用这个对象的逻辑名。实际使用中,情况往往并非如此简单,我们经常会遇到很多在页面上动态产生的对象,换而言之,对象库里没有这些对象,我们也无从引用。因此我们必须采用其他的技术来完成这类操作,这也就是我们需要讲解的Descrīptive Programming。
为了满足上面提到的动态对象的处理问题,QTP允许用户通过将对象属性编码到测试脚本里来动态识别对象,这就是我们通常意义下称为的Descrīptive Programming。通过这种方式,我们可以指示QTP不通过引用对象库和对象名来对实际对象进行操作。具体操作中,我们只需要为QTP提供对象的一组属性和值,这样QTP就可以来识别相应的对象并对其进行操作。这相当于,告诉QTP要识别对象的一些关键特征,根据这些特征QTP就可以一一匹配然后识别出来这个对象。
而且,更为重要的是,通过这种Descrīptive Programming的方式,还可以让QTP识别具有某些相同属性的对象。我们先来举个例子来看一下:我们假设当前的Windows系统中打开了若干的Yahoo主页面(多于一个),现在我们要关闭所有的正在浏览Yahoo主页面的浏览器。
对于上面那个例子来说,我们先看一个简单一点的情况,假设只有且仅有一个Yahoo主页面:那么我们可以用下面的方法来
Window("Text:=Yahoo! - Microsoft Internet Explorer").Close
我们可以看到语句里我们要查找的对象是Window窗口标题为“Yahoo! - Microsoft Internet Explorer”,然后把它关闭,具体的语法说明我们稍后为解释。但是上面的语句仅仅适合前面提到的条件“只有且仅有一个Yahoo主页面”,如果有多个同样的窗口就会出错,原因是通过语句可以匹配到多个对象,而QTP不知道应该对哪个对象进行关闭动作。我们需要进一步的缩小匹配范围:
Dim i
i = 0
while (Window("Text:="Yahoo!" - Microsoft Internet Explorer", "index:="&i).exist)
Window("Text:=Yahoo! - Microsoft Internet Explorer", "index:="&i).close
i = i +1
wend
这里我们可以看到,对于具有相同属性的对象,我们可以通过index参数来对其进行区别,第一个对象为index=0,第二个为index=1等等,依次类推。当然我们还可以通过CreationTime和Location参数来定位对象,这里就不详细叙述了。
通过上面的例子,我们对Descrīptive Programming有一个基本了解了,下面我们详细讲解一下Descrīptive Programming:在具体实现中,我们有两种类型的Descrīptive Programming方法。可以列出直接在测试语句中描述对象的属性和值的集合;或者向Descrīption 对象中添加属性和值的集合,然后在语句中输入Descrīption 对象的名称。下面我们分别举例介绍。
2、直接在语句中输入编程描述
通过多个指定描述对象的property:=value对,可以直接在语句中描述对象,这是最直接有效的方法。
常规语法为:
TestObject("PropertyName1:=PropertyValue1", "..." , "PropertyNameX:="PropertyValueX""}
TestObject - 测试对象的类。
PropertyName:=PropertyValue - 测试对象的属性及其值。各个property:="value" 对之间应用逗号和引号分开。
例如:以下语句指定Mercury Tours 页面中名为author且索引值为3 的WebEdit 测试对象。当测试运行时,QTP 将查找具有匹配属性值的WebEdit 对象,并输入文本jojo。
Browser("Mercury Tours").Page("Mercury Tours").WebEdit("Name:="Author"", "Index:="3"").Set "Mark Twain"
我们也可以从从描述中的特定位置(从Page 对象描述开始)开始使用Descrīptive Programming。
Browser("Mercury Tours").Page("Title:="Mercury" Tours").WebEdit("Name:="Author"", "Index:="3"").Set "jojo"
此外,如果我们希望在在一个测试或组件中多次使用相同的Descrīptive Programming,则可以将创建的对象赋值给变量,这样使用会方便很多。
例如:我们需要完成下面一系列操作
Window("Text:=HyperSna").WinButton("Caption:=日期").Click
Window("Text:=HyperSna").WinButton("Caption:=时间").Click
Window("Text:=HyperSna").WinButton("Caption:=确定").Click
那么,为了方便其见,我们可以将Window("Text:=HyperSna")赋值到一个变量,然后再使用,参见下面的代码:
Set WinHyper = Window("Text:="HyperSna"")
WinHyper.WinButton("Caption:=日期").Click
WinHyper.WinButton("Caption:=时间").Click
WinHyper.WinButton("Caption:=确定").Click
如果使用了VBscrīpt里面的With语句,还可以简化为以下代码:
With Window("Text:="HyperSna"")
.WinButton("Caption:=日期").Click
.WinButton("Caption:=时间").Click
.WinButton("Caption:=确定").Click
End With
下面我们来看一个更为详细的例子,在QTP产品缺省安装里面自带了一个网上订机票的示例称为Mercury Tour,我们看一下在订票过程中何时需要用Descrīptive Programming。
首先登入系统后,如果需要订票,就要先搜索航班,此时系统要求输入订票乘客的数量,假设我们在第一次录制脚本时选择了1个Passenger,并成功完成订票。然后,我们需要参数化乘客数量来测试订票系统,我们会发现回放会失败。原因在于,不同的乘客的数量导致在订票时需要输入每个乘客的姓名,而录制时,只输入了一个乘客的姓名。而乘客姓名的输入框是随着乘客数量的变化而动态生成的,我们不可能从对象库里得到没有录制的对象,因此必须使用Descrīptive Programming。
在录制单个乘客时,我们得到的录制语句是:
Browser("Welcome: Mercury Tours").Page("Book a Flight: Mercury").WebEdit("passFirst0").Set "Michael"
Browser("Welcome: Mercury Tours").Page("Book a Flight: Mercury").WebEdit("passLast0").Set "Wang"
显然WebEdit("passFirst0")和WebEdit("passLast0")是录制时产生的对象并存放到对象库里的。通过对象库,我们可以看到对象的属性如下
系统对于发生多个FirstName时,命名规则是passFirst0,passFirst1…依次类推。因此只要通过简单的Descrīptive Programming就可以完成动态FirstName与LastName的识别工作。这里我们假设参数化的乘客数已经赋值给intPassNum,下面是脚本中的关键语句:
counter = 0
For i = 0 to (intPassNum)
Browser("Find a Flight:").Page("Book a Flight:").WebEdit("name:="passFirst""&i).Set "Michael"
Browser("Find a Flight:").Page("Book a Flight:").WebEdit("name:="passLast""&i).Set "Wang"
counter = counter + 1
Next
3、使用descrīption对象
使用Descrīption 对象可以返回包含一组Property 对象的Properties 集合对象。Property 对象由属性名和值组成。然后,可以在语句中指定用返回的Properties 集合代替对象名。(每个property 对象都包含一个属性名和值)。
要创建Properties 集合,可以使用以下语法输入Descrīption.Create 语句:
Set MyDescrīption = Descrīption.Create()
创建Properties 对象(例如,以上示例中的MyDescrīption)后,就可以输入语句,以便在运行会话期间在Properties 对象中添加、编辑、删除或检索属性和值。这样,就可以在运行会话期间,使用动态方法确定哪个属性以及多少个属性应包含在对象描述中。
在Properties 集合中填充一组Property 对象(属性和值)后,可以在测试语句中指定用Properties 对象代替对象名。
例如,假设我们需要完成以下一个操作:
Window("Error").WinButton("text:=OK", "index:="1"").Click
我们可以通过Descrīption对象来实现同样的功能,参加下面的代码:
Set MyDescrīption = Descrīption.Create()
MyDescrīption("text").Value = "OK"
MyDescrīption("index").Value = 1
Window("Error").WinButton(MyDescrīption).Click
Set MyDescrīption = Nothing
【小结】以上是对QTP中有关处理动态对象中的Descrīptive Programming的简单介绍,希望对大家能够有所帮助,就总体而言,如果能够熟练掌握Descrīptive Programming,那么有很多实际中的问题就可以迎刃而解。当然Descrīptive Programming只是QTP中的一个功能,QTP在实际功能测试中还有很多强大的功能,作为QTP学习的一个系列有机会我会一一介绍。使用QTP9.0的一点心得——对象仓库
2008-6-18
QTP9.0的大部分功能与QTP8.2没什么区别,使用后最大的感受是对象仓库的功能增强了,不光有对象仓库窗口,还增加了对象仓库管理窗口、连接对象仓库窗口。
我们知道对象仓库模式可分为每个对象库和共享对象库两类,在QTP8.2中,新建的测试默认情况下使用每个对象库,要使用共享对象库的话,应按如下方法进行设置:
Ø 选择“测试”——>“设置”——>“资源”选项卡。
Ø 在“对象库类型”区域,选择“共享”。
Ø 请指定要用作对象库的共享对象库文件。要指定文件,请输入对象库文件名,或单击浏览按钮并从“打开”对话框中选择资源文件。要新建共享对象库文件,请在“共享”框中输入新文件名。
Ø 对象库文件的默认文件扩展名是 .tsr,但是文件可以使用任何扩展名。在“打开”对话框中浏览现有对象库文件时,在“文件类型”框中选择“所有文件”。
Ø 注:请在新测试开始录制前进行设置,已有测试的对象库模式不能进行修改。
在QTP9.0中,对象仓库的使用相对灵活,每个action录制产生的对象都是存放在本地对象仓库中的,在对象仓库窗口(Resources...-> Object Repository...)中进行导出(File->Export Local Objects...)操作,可生成共享对象库,默认文件扩展名是 .tsr。
共享对象库的使用:打开连接对象仓库窗口(Resources...->Associate Repository...),选择共享对象库文件,然后进行连接。
将某一action本地对象仓库的对象加入到共享对象库中的方法:
方法一:
Ø 先将该action与共享对象库连接,然后进行录制
Ø 保存后关闭该测试文件
Ø 打开对象仓库管理窗口(Resources...-> Object Repository Manager...)
Ø 打开共享对象库文件(File->Open->*.tsr)
Ø 从本地对象库进行更新 (Tools->Updated from Local Repository...),选择刚才的测试文件及要更新的action,更新(Update All->Save)
方法二:
Ø 将该action本地对象仓库的对象也导出成一个共享对象库文件
Ø 打开对象仓库管理窗口(Resources...-> Object Repository Manager...)
Ø 将两个共享对象库文件进行整和(Tools-> Object Repository Merge Tool…),保存为另一个共享对象库文件。
在QTP9.0中,还可以在对象仓库中更新对象的属性,如果用来识别对象的属性在添加到对象仓库以后被改变了,QTP在回放脚本的过程中就无法识别这一对象,会导致出错。使用“更新应用中对象”这一方法,可以重新定义这些属性,使对象能被识别,所以非常有用。具体操作如下:
Ø 打开对象仓库管理窗口(Resources...-> Object Repository Manager...)
Ø 选择要更新的对象
Ø 选择“更新应用中对象”(Object > Update from Application)或工具栏上的
Ø 在应用中找到该对象并点击它,选择与对象仓库中相同类的对象
在QTP中,对象识别是其核心的技术,正确识别到对象是保证自动化测试脚本成功运行的关键,QTP9.0中强大的对象仓库处理技术将给对象的识别、管理和使用带来很大的便利。软件测试中的相关概念定义
2008-3-17
软件测试的相关概念定义1.什么是软件测试1.1软件测试定义软件测试是为了发现错误而执行程序的过程。1.2软件测试两个阶段软件测试在软件生存期中横跨两个阶段:通常在编写出每一个模块之后就对它做必要的测试(称为单元测试)。编写与单元测试属于软件生存期的同一阶段。在结束这个阶段之后,对软件系统还要进行各种综合测试,这是软件生存期的另一个独立的阶段,即测试阶段。2.软件测试的目的和原则基于不同的立场,存在着两种不同的测试目的。从用户的角度出发,普遍希望通过软件测试暴露软件中隐藏的错误和缺陷,以考虑是否可以接受该产品。而从软件开发者的角度出发,则希望测试成为表明软件产品中不存在错误的过程,验证该软件已正确地实现了用户的要求,确立人们对软件质量的信心。有鉴于此,Grenford J.Myers就软件测试目的提出以下观点:①测试是程序的执行过程,目的在于发现错误;②一个好的测试用例在于能发现至今未发现的错误;③一个成功的测试是发现了至今未发现的错误的测试。3.软件测试的对象软件测试并不等于程序测试。软件测试应贯穿于软件定义与开发的整个期间。因此,需求分析、概要设计、详细设计以及程序编码等各阶段所得到的文档,包括需求规格说明、概要设计规格说明、详细设计规格说明以及源程序,都应该成为软件测试的对象。4.黑盒测试与白盒测试4.1黑盒测试与白盒测试概念任何工程产品都可以使用以下的两种方法之一进行测试:①已知产品的功能设计规格,可以进行测试证明每个实现了的功能是否符合要求。②已知产品的内部工作过程,可以通过测试证明每种内部操作是否符合设计规格要求,所有内部成分是否已经过检查。前者就是黑盒测试,后者就是白盒测试。4.2黑盒测试测试人员完全不考虑程序内部的逻辑结构和内部特性,只依据程序的需求规格说明书,检查程序的功能是否符合它的功能说明。因此黑盒测试又叫做功能测试或数据驱动测试。黑盒测试主要是为了发现以下几类错误:①是否有不正确或遗漏的功能?②在接口上,输入能否正确地接受?能否输出正确的结果?③是否有数据结构错误或外部信息(例如数据文件)访问错误?④性能上是否能够满足要求?⑤是否有初始化或终止性错误?所以,用黑盒测试发现程序中的错误,必须在所有可能的输入条件和输出条件中确定测试数据,来检查程序是否都能产生正确的输出。4.3白盒测试软件的白盒测试是对软件的过程性细节做细致的检查。它允许测试人员利用程序内部的逻辑结构及相关信息,设计或选择测试用例,对程序所有逻辑路径进行测试。通过在不同点检查程序的状态,确定实际的状态是否与预期的状态一致。因此白盒测试又称为结构测试或逻辑驱动测试。软件人员使用白盒测试方法,主要想对程序模块进行如下的检查:①对程序模块的所有独立的执行路径至少测试一次;②对所有的逻辑判定,取“真”与取“假”的两种情况都能至少测试一次;③在循环的边界和运行界限内执行循环体;④测试内部数据结构的有效性,等等。5.α测试和β测试α测试是由一个用户在开发环境下进行的测试,也可以是开发机构内部的用户在模拟实际操作环境下进行的测试。软件在一个自然设置状态下使用。开发者坐在用户旁边,随时记下错误情况和使用中的问题。这是在受控制的环境下进行的测试。α测试的目的是评价软件产品的FLURPS(功能、局域化、可使用性、可靠性、性能和支持)。尤其注重产品的界面和特色。α测试人员是除开发人员之外首先见到产品的人,他们提出的功能和修改意见是特别有价值的。α测试可以从软件产品编码结束之时开始,或在模块(子系统)测试完成之后开始,也可以在确认测试过程中产品达到一定的稳定和可靠程度之后再开始。有关的手册(草稿)等应事先准备好。β测试是由软件的多个用户在一个或多个用户的实际使用环境下进行的测试。这些用户是与公司签订了支持产品预发行合同的外部客户,他们要求使用该产品,并愿意返回有关错误信息给开发者。与α测试不同的是,开发者通常不在测试现场。因而,β测试是在与开发者无法控制的环境下进行的软件现场应用。在β测试中,由用户记下遇到的问题,包括真实的以及主观认定的,定期向开发者报告。β测试主要衡量产品的FLURPS。着重于产品的支持性,包括文档、客户培训和支持产品生产能力。只有当α测试达到一定的可靠程度时,才能开始β测试。由于它处于整个测试的最后阶段,不能指望这时发现主要问题。同时,产品的所有手册文本也应该在此阶段完全定稿。6、为了隔离被测试模块,需要用一些安全模块来代替被测试模块所调用的模块,叫它们“桩”。
7、为了能把测试数据输入到被测试模块中,需要一个驱动模块将数据输入到被测模块中,并获得被测模块的反馈,最后通过判断得到测试结果。完成这样功能的模块通常也叫做“驱动”模块。Linux 基本命令
2007-11-05
Linux基本命令:
1、设置用户John的空间有效期
命令:# chage -M 30 John
2、用于获得临时root用户权限
命令:# su -
3、显示Linux系统的当前日期和时间
命令:# date
4、显示当前在系统中登录的所有用户名
命令:# who
5、显示当前登录用户(自己)的名称
命令:# who am i
6、清除终端屏幕
命令:# clear (同tput clear)
7、显示当前目录的完整路径名
命令:# pwd
8、改变目录
命令:# cd
9、显示文件的名称和目录的子目录
命令:# ls
Ex:# ls -l 显示文件和目录的详细列表
10、显示当前登录会话的所有活动进程
命令:# ps
11、帮助命令
命令:# man
12、结束Linux会话
命令:# exit / logout
13、关闭Linux操作系统
命令:# shutdown
14、查看系统上已经创建的文件系统的磁盘空间利用率
命令:# df
15、挂接一个文件系统
命令:# mount
16、定义启动时可挂接的文件系统和设置
命令:# /etc/fstab
17、卸载已经挂载的文件系统
命令:# umount
18、创建目录
命令:# mkdir
19、删除目录
命令:# rmdir
20、创建和编辑文本文件
命令:# vi
>按下i键可在当前光标位置插入文本
>键入:wq可保存所有更改并退出
>键入:q!可不保存更改而退出
21、用于将源文件中的内容复制到目录文件
命令:# cp
22、用于将文件或目录从一个位置移动到另一位置,或改变文件或目录的名称
命令:# mv
23、用于在屏幕上显示文件的内容
命令:# cat
24、允许用户在查看文件内容时向上翻页
命令:# more或less
注:
>more 和 less 命令可以和其他命令结合使用,通过管道符"|"分页显示内容
>键入q可退出
命令:ls -l | more
--显示当前目录中所有文件的所有属性,more命令分页显示ls -l命令的输出结果
25、rpm -qa | more
--查看用户计算机上安装的所有软件
>rpm是指计算机运行RPM程序的命令
>-q选项:执行查询操作
>a选项:查询选项的修饰符,用于告诉RPM用户想列出的所有包
>命令 | more 用于分页显示
rpm -ivh ***-***.i386.rpm
--用于安装软件包
>i选项:用于执行安装操作
>v选项:用于在安装失败时显示详细信息
>h选项:使用竖线显示安装进度
rpm -e ***-***.i386.rpm
--用于删除和卸载软件包
rpm -Uvh ***-***.i386.rpm
--用于更新当前安装的软件包
-- -U是upgrade的缩写
26、用于压缩和解压文件
命令:# tar
27、用于备份程序
命令:# cpio
28、用于在屏幕上显示消息
命令:# echo
29、用于查看IP地址
命令:# ifconfig
30、用于切换到Shell
命令:# !/bin/bash
31、查看IP
cat /etc/sysconfig/network-scrīpts/ifctg-etho
32、查看DNS
cat /etc/resolv.conf
33、执行/etc/profile文件
# . /etc/profile
34、验证JDK的安装
# java -versionQTP精华问题
2007-10-17
QTP问与答
1.Q:runaction 后面能不能接变量(动态调用action,所以从数据库取数据做action名字了,但是调用总是找不到)?
A:脚本中原有RunAction "testbase [case1]", oneIteration
把引号中的内容放到Global表中的第22行,然后将代码修改为:
datatable.getsheet("Global")
datatable.setcurrentrow(22)
strLogin=DataTable("ActionName","Global")
RunAction strLogin, oneIteration
help中也有相关帮助
如:
Syntax
RunAction ActionName, [IterationMode , IterationRange , Parameters]
ActionName : String : The name of the action
2.Q:QTP8.2 中调用VB函数的问题(用VBscrīpt写了一些测试脚本需要的几个通用函数,有没有办法可以用类似include的方式进行调用,而不需要每次都把这些函数Copy到新的脚本中)?
A:程序开头加上ExecuteFile "..\..\..\project\DeVariable.vbs"
3.Q:QTP 如何做回归测试(300多个TestCase,TD是否可以管理) ?
A:TD可以实现,可以生成测试集,一个测试集可以包含若干个测试脚本
QTP8.2本身提供一个工具Test Batch Runner但是运行完没有报告。
MI有另一个工具叫MTM(multitestmanager)
4.Q:qtp 自动节图功能
A: 具体可参考此帖:[url]http://www.51testing.com/cgi-bin[/url] ... d=17663&fpage=1
5.Q: 在QTP中如何设置使用别的浏览器(XP系统,用IE访问程序时,每次总提示屏蔽安装ActiveX插件,需要手动安装.但把这个过程录制到QTP后,回放一次是成功的。当我给某个输入框参数化了好多数据后,回放过程中,某些就会失败.
可能有两个方面可以解决这问题1、每个动作设置延迟时间 2、设置为用别的浏览器。)
(失败的提示信息是 object not visible)
A:1.延迟可用WAIT X(X单位是秒)
2.可以安装插件添加新的浏览器
SystemUtil.Run "file” "params" "dir" "op'' "mode"
QTP运行可执行文件的方法及其参数
ps: 建议是用IE浏览器,或者IE内核浏览器做测试
6.Q:checkpoint 检查网页,是否能实现只要网页出现乱码就返回错误报告?
A:Text not displayed能解决问题
关于 Text Checkpoint 的总结。
1)Text Checkpoint 的检查部分分为三个部分。Checked Text 、Text Before 、Text After。在默认的情况下,Checked Text执行的是精确检查,其余两个部分执行的是模糊检查。Text Before(After)检查的内容可以比实际的内容少。但是不能有和是实际内容不相符的地方,否则就失败。
2) Exact match选项。如果选择了这个选项,三部分完全都进行精确检查。个人觉得和只检查checked Text部分没有区别。
3) Text not displayed。这个选项本质上就是一个结果取反的过程。就是把检查的结果给反过来,把pass变成Fail,Fail变成pass。我觉得这样就很容易理解。
7.Q:WSH 的应用方法
A:WSH 实际上是一个脚本语言的运行环境,它之所以具备强大的功能,是在于其充分挖掘了脚本语言的潜力。因此,如果抛开脚本语言而空谈 WSH ,那实际上就没有了意义。而如果再展开来讲述脚本语言,显然就离开了今天的主题。
在这种情况下,只好采取一种折衷的方法:给大家推荐几个脚本文件利用 WSH 执行任务的实例,希望大家能通过这些例子对 WSH 的使用有一个初步的认识。
脚本文件的编写十分方便,你可以选用任意一个文字编辑软件进行编写,编写完成后,只需将它保存为 WSH 所支持的文件名就可以了(如.js 文件和.vbs 文件)。最常用的就是记事本编辑器,下面的实例都是以它作为工具编写的。
打开记事本编辑器,在上面编写如下内容:
Wscrīpt.Echo("走近 WSH")
将它保存为以 .vbs 或 .js 为后缀名(千万不要写成了 .txt)的文件并退出记事本。双击执行这个文件。
这一次,我们要利用 WSH 完成一次创建十个文件夹的工作。代码如下:
dim objdir
set ōbjdir=wscrīpt.createobject("scrīpting.filesystemobject")
for k=1 to 10
anewfolder="c:\chapter" & k
objdir.createfolder(anewfolder)
next
同样,将它存为 .vbs 文件并退出。运行后,我们会发现,C 盘根目录下一次性多出了十个新文件夹。
最后,再举一个在服务器上的运用。下面的代码将帮助你重新启动指定的 IIS 服务: ' define a constant for stopped services
Const ADS_SERVICE_STOPPED = 1
' get an ADSI object for a computer
Set ōbjComputer = GetObject("WinNT://MYCOMPUTER,computer")
' get an object for a service
Set ōbjService = objComputer.GetObject("Service","MYSERVICE")
' check to see if the service is stopped
If (objService.Status = ADS_SERVICE_STOPPED) Then
' if the service is stopped, then start it
objService.Start
End If
将它以 startsvc.vbs 为名保存在 C: 盘根目录。并通过如下命令执行:Cscrīpt C:\STARTSVC.VBS。运行后,经你指定的 IIS 服务项将被重新开启。
其实,在 Windows 的 samples 目录下,有个 WSH 文件夹,那里面有不少很具代表性的 .vbs 和. js 脚本文件。
此外,利用 WSH 还可以自己编写脚本文件来提高网络管理方面的效率。
8.Q; 从 EXCEL 中导出数据进行测试
datatable.AddSheet("51sheet")
datatable.ImportSheet "f:\test.xls","testsheet","51sheet"
Dim i,RowCount ' 定义两个变量
i=0
RowCount=datatable.GetSheet("51sheet").GetRowCount ' 设置 RowCount 等于 51sheet 中的行数。
msgbox RowCount
Do while i<rowcount
i=i+1 ' 第一次进入循环,执行这句后, i=1
'datatable.getsheet("51sheet").setcurrentrow(i) 这句话被我注释掉了,正确的写法应该是下面这样,分开写。
datatable.getsheet("51sheet")
datatable.setcurrentrow(i)
' 执行过上面两句后, CurrentRow 是第一行。
tempData=DataTable.GetSheet("51sheet").GetParameter("Name").Value
' 现在,我们调用 msgbox 看看下面这种调用方法得到的是什么值?没错,是第一行的值,下一次循环呢?
' 得到的是第二行的值么?
msgbox "GetParameter-Name:"&tempData ' 这里弹出我们要看的值。
' 下面我们用另外一种方法来得到。
msgbox "GetParameter-i:"&DataTable.GetSheet("51sheet").GetParameter(1).Value ' 这里我用 GetParameter(1) 去得到 sheet 中第一列的值。
loop
9.Q: 关于 dtGlobalsheet 与 dtGlobalsheet
1) dtGlobalsheet 只有一个,它的 index 值比较特殊,它有两个 index 值,一个是 1 还有一个是内置的默认的 1000 。
你可以用 1 或者 1000 去引用它都是正确的。当然了,如果你用 dtGlobalsheet 来引用它也是正确的。这个 sheet 的 Name 叫做 "Global" 。注意: SheetName 是区分大小写的。
2) dtLocalsheet 可以用 index:1001 来引用,当然,也可以用 2 来引用。至于其它自定义的 sheet 嘛,你就只能用 index:3 来引用了。
它没有内置的默认的类似前两个那样的 index 值。
3) datatable 这个对象只有一个。就是所有 sheet 的集合。或许你把它理解为 excel 文件比较好。
dtsheet 呢?就是其中的每个 sheet 。所不同的就是 MI 为他们做了一些默认值。在我们的应用中,可能会有多个 action, 如: actiion1,actiion2,actiion3
这些 action 也分别对应有各自默认的 LoaclSheet. 即: actiion1 , actiion2 , actiion3 。
如果每个 action 中,我们都只用到一个 sheet ,那就好办了,在每个 action 中都可以用 localsheet 来引用,但是如果我们有两个及两个以上的 sheet. 那么就比较容易乱套了。
10.Q: 移动当前位置的行
For i=1 to 3
datatable.getsheet("Global")
datatable.setcurrentrow(i)
DataTable("C","Global") = DataTable("nodename","Global") ‘把表Global中的nodename字段中的内容取出来。
Next
11.Q: 如何能记录到页面的校验码?(Output value能不能得到web页面的校验码,一般的校验码是由图片随即生成的 ,用QTP怎么录脚本在登陆前得到校验码并输入到校验码一栏)
A:1)一个很简单的方法:测试时叫开发屏蔽掉检验码的功能后,再录制脚本。检验码的功能可以手工很简单测试出来。
2)想得到图片的校验码,唯一的方法就是跟程序员拿程序,然后自己在脚本里面写FUCNTION!
其它就要手动输入了!
12.Q:QTP 正则表达式的帮助
A:见附件word文档
附件: QTP regular expression usage.doc (2005-9-8 18:32, 26.5 K)
13. 自动测试实施计划
1)分析实施自动化测试可能存在的风险:就是决定是否实施, 用成本 时间 效果 。。
2)制定实施的时机:也就是在什么阶段
3)研究所要测试的功能 性能
4)分析在测试中可能遇到的问题 和困难
5)预估所需要的人时和相应的硬件
7)确定负责人员和相关测试人员
6)制定详细的测试计划 方案
7)最后是执行计划
14.Q: 一个Action里如何调用在另一个Action中定义的函数、过程(或变量、常量)
例如:
-----------------------------------
'Action_A
Public strURL
...... ......
Function QueryList()
...... ......
...... ......
End Function
------------------------------------
'Action_B
'如何调用 QueryList函数和strURL
A:1)可以把这些变量和Function放在vbs文件作为resource文件共享,在每个Action中添加该文件.
如果function中出现控件调用,那么必须确保该控件在相应的Action的object repository中是存在的.
2)定义一个可被调用的ACTION里面唯一的放一个FUNCTIONwing1799 2007-10-3 15:17
15.Q: 如何管理QTP的源代码?(QTP生成的源代码比较多,而且和环境控件都有关,假如需要多人同时开发, 请问如何管理源代码?)
A: 一个是代码你可以通过vss,cvs等来进行管理
一个是通过td或者qc的基于用例的代码管理
其实第一种方法是基于版本控制来进行的,第二种方法是基于用例管理进行的
角度不同,管理方法也不同,不过团队协作需要的大家分工明确,进度控制。代码管理可以借鉴开发的方法。
16:Q: 脚本不能回放,IE中的AtiveX设置有问题??
A: TOOL---OPTIONS----Ative screen
然后点开advanced..,把LOAD ACTIVEX CONTROLS打勾
TOOL---OPTIONS----Ative screen
然后点开advanced..,run scrīpts-->disabled!
17 .Q: 如何参数化link
Browser("Browser").Page("Page").Sync
Browser("Browser").Navigate "http://www.51testing.com/cgi-bin/index.php"
Browser("Browser").Page("51Testing 软件测试论坛---软件测试,软件质量工程师").Sync
Set tags=Browser("Browser").Page("51Testing软件测试论坛---软件测试,软件质量工程师").Object.links
Dim i,j, arr()
i=0
For Each element in tags
If Ucase(element.tagname)="A" and left(element.InnerText,1)="[" Then
ReDim Preserve arr(i+1)
arr(i)=element.InnerText
i=i+1
end if
Next
For j=0 to i
Browser("Browser").Page("51Testing软件测试论坛---软件测试,软件质量工程师").Link("[ 版主讨论区 ]").SetTOProperty "Text",arr(j)
Browser("Browser").Page("51Testing软件测试论坛---软件测试,软件质量工程师").Link("[ 版主讨论区 ]").Click
Browser("Browser").Back
Next
这段代码先是打开一个空的页面,然后输入url.
到达论坛首页。
然后得到所有版面的名称,也就是link的名称。
存到数组里面。
然后使用SetTOProperty更换录制时候录下的link的属性。
这时候再click
18. Q:QTP 在Debug狀態,在Export View 區域不能寫入任何東西
A: 如果你的目的是在debug过程中修改已执行过的命令,可以在Debug view的Command中执行命令,如重新执行已经执行过的命令,修改变量的值等等。
如:Window("Flight Reservation").WinEdit("Name:").Set "51testing"
已经执行,如果现在想修改“51testing”为“testing”,可以在command中执行
Window("Flight Reservation").WinEdit("Name:").Set "testing"
19 .Q: 动态变化值如何获取
A:VAL=Browser("欢迎使用我的工作台").Page("欢迎使用我的工作台").Frame("managePlace_7").WebRadioGroup("userAccountId_0").GetROProperty("Value")
Browser("欢迎使用我的工作台").Page("欢迎使用我的工作台").Frame("managePlace_7").WebRadioGroup("userAccountId_0").select val
20 .Q: 如何一一获得Table中 某栏 link 的 text?
A:通过上面link 的学习. 我终于融会贯通,完成了我的问题: 与大家共享:
//////////////////////////////////////////////////////////////////////////////////////
Browser("Login").Page("Page").Frame("contents").ViewLink("treeview").Image("Tplus").Click
Browser("Login").Page("Page").Frame("contents").ViewLink("treeview").Image("Tplus_2").Click
Browser("Login").Page("Page").Frame("contents").ViewLink("treeview").Link("开课设置").Click
Browser("Login").Page("Page").Frame("main").WebList("drpStatus").Select "任意"
Browser("Login").Page("Page").Frame("main").WebButton("查找").Click
Browser("Login").Page("Page").Sync
Dim finded,findCode,Nowout
'define a constrat for find
findCode = 110901
finded = false
Function MaxPage(pageString)
'msgbox pageString
Dim ilen,i,j
ilen = len(pageString)
i=ilen
While i>0
j = mid(pageString,i,1)
'msgbox j
If instr("123456789",j)>0 Then
MaxPage = j
'msgbox MaxPage
Exit function
End If
i=i-1
Wend
End Function
Dim trowcount,maxp
trowcount = Browser("Login").Page("Page").Frame("main_8").WebTable("开课代码").RowCount
msgbox "Rowcount: "&trowcount
Nowout = Browser("Login").Page("Page").Frame("main_8").WebTable("开课代码").GetCellData(trowcount,1)
Nowout = trim(Nowout)
maxp = MaxPage(Nowout)
msgbox "max page: "& maxp
Dim nowPage,checkid
For nowPage = 1 to maxp
If finded Then
Exit for
End If
' link to the 当前所需page
If nowpage>1 Then
Browser("Login").Page("Page").Frame("main_8").Link("[2]").SetTOProperty "Text","["&nowpage&"]"
Browser("Login").Page("Page").Frame("main_8").Link("[2]").Click
Browser("Login").Page("Page").Sync
end if
' Get the rowcount of table in now page
trowcount = Browser("Login").Page("Page").Frame("main_8").WebTable("开课代码").RowCount
msgbox "Rowcount: "&trowcount
'link every record in the table of the page
for i = 2 to trowcount - 2
Nowout = Browser("Login").Page("Page").Frame("main_8").WebTable("开课代码").GetCellData(i,2)
'msgbox i&": "&Nowout
checkid = "dgCourse:_ctl" &(i+1)& ":_ctl0"
Browser("Login").Page("Page").Frame("main_8").WebCheckBox("dgCourse:_ctl3:_ctl0").SetTOProperty "name",checkid
Browser("Login").Page("Page").Frame("main_8").WebCheckBox("dgCourse:_ctl3:_ctl0").Set "ON"
Browser("Login").Page("Page").Frame("main_8").Link("0901").SetTOProperty "Text",Nowout
Browser("Login").Page("Page").Frame("main_8").Link("0901").Click
Browser("开课设置详细信息").Page("开课设置详细信息").Sync
'wait(1)
msgbox "begun"
msgbox findCode
msgbox Nowout
msgbox "finished"
If trim(findCode) = trim(Nowout) Then
finded = true
msgbox "find is ok!"
wait(2)
Exit for
End If
Browser("开课设置详细信息").Close
Browser("Login").Page("Page").Sync
next
Next
21 .Q:网页下拉框的选择
A:For i =1 to 10
Randomize
IndexNum=Int((10 - 5 + 1) * Rnd + 5)
Browser("Mercury Tours").Page("Find Flights_2").WebList("arrive").Select "#"&IndexNum
wait(3)
Next
Sub ChildObjects_Example()
'The following example uses the ChildObjects method to find all the
'list objects on a Web page, and then to select an item in each list.
Set ōDesc = Descrīption.Create()
oDesc("micclass").Value = "WebList"
Set Lists = Browser("Mercury Interactive").Page("Mercury Interactive").ChildObjects(oDesc)
NumberOfLists = Lists.Count()
For i = 0 To NumberOfLists - 1
Lists(i).Select i + 1
Next
End Sub
22 .Q: 将测试数据单独拿出来
A: 取得一个
Browser("Browser").Page("").WebList("fid").GetItem (1)
取得全部
Browser("Browser").Page("").WebList("fid").GetROProperty("all items")
以下可以在自带的例子中实现
Window("Flight Reservation").WinComboBox("Fly From:").Select "Frankfurt"
a=window("Flight Reservation").wincombobox("Fly From:").GetItem(1)
reporter.ReportEvent 2,"下拉列表的值",a
23 .Q:和TD连接
A:在QTP中不是有个Quality Center Connection,选择服务器连接,服务器处输入类似http://computer_name/tdbin,其中computer_name为服务器的名字,连接后在测试结果中添加defect就可以与TD相连了。
24 :Q:处理Windows弹出窗口
A:IF Not Window("Flight Reservation").Exist(1) Then
'Calling open flight
If not Dialog("Login").Exist(1) Then
Browser("管理系统").Dialog("Microsoft Internet Explorer").WinButton("确定").Click
End If
25 .Q:查询结果的比较
这是查询一个字段的,对查询结果多页的情况也涉及了,其实应该把所有查询字段联合起来的,也就是改改sql语句和判断条件。
[i]Set Conn = CreateObject("ADODB.Connection")
Set Rs = CreateObject("ADODB.Recordset")
Conn.Open "Descrīption=kml-it;DRIVER=SQL Server;SERVER=KML-IT;UID=sa;PWD=password;APP=QuickTest Professional;WSID=KML-MICHELLE;DATABASE=kml_db"
sql="select distinct grn_no from grn_dtl where grn_no like '%"&grnNo&"%' order by grn_no DESC"
Rs.open sql,Conn,1,3
Dim i,j,cell
i=1
j=2 'j=2 的原因是因为页面上table是的数据是隔一行一条,不知 道怎么回事,开发人员弄的怪把
Do while not rs.eof
If i=13 Then '13是每页显示出的最大行数,是个常数
i=1
j=2
Browser("::").Page("::").Frame("mainFrm_5").Link("下一页").Click
end if
cell=Browser("::").Page("::").Frame("mainFrm_5").WebTable("收货单号").GetCellData(j,2)
If cell<>rs("grn_no") Then
Reporter.ReportEvent 1, "查询功能"&cell, "查询结果错误."
Exit do
else
Reporter.ReportEvent 0, "查询功能"&cell, "查询结果正确."
End If
rs.movenext
j=j+2
i=i+1
Loop
rs.close
conn.close
set conn=nothing[/i]QTP中的描述性编程
2007-10-12
使用编程性描述(Using Programmatic Descrīptions )
在录制脚本时,QTP会将被测对象添加到对象仓库中。只要对象存在于仓库中,我们就可以在专家视图中使用该对象进行手动添加脚本。在脚本中,我们一般都使用对象的名称(该对象名称不区分大小写)作为对象描述。
例如:
在下面的语句中“username”是一个编辑框的名称。这个编辑框位于某页面(Page)之上(该页面的名称为“Mercury Tours”),并且该页面属于名为“Mercury Tours”的浏览器(browser)。
Browser("Mercury Tours").Page("Mercury Tours").WebEdit("username")
QTP代码
2008-09-25 14:20:55
QTP 基础代码收集
2008-06-01 18:26:13 / 个人分类:QuickTestProfessional
1。 将bug添加到QC
Dim TDConnection
Set TDConnection = CreateObject("TDApiOle.TDConnection")
TDConnection.InitConnection "http://yovav/tdbin" ' URL for the DB
TDConnection.ConnectProject "TD76","bella","pino" ' Valid login information
If TDConnection.Connected Then
MsgBox("Connected to " + chr (13) + "Server " + TDConnection.ServerName _
+ chr (13) +"Project " + TDConnection.ProjectName )
Else
MsgBox("Not Connected")
End If
'Get the IBugFactory
Set BugFactory = TDConnection.BugFactory
'Add a new empty bug
Set Bug = BugFactory.AddItem (Nothing)
'fill the bug with relevant parameters
Bug.Status = "New"
Bug.Summary = "Connecting to TD"
Bug.Priority = "4-Very High" ' depends on the DB
Bug.AssignedTo = "admin" ' user that must exist in the DB's users list
Bug.DetectedBy = "admin" ' user that must exist in the DB's users list
'Post the bug to DB ( commit )
Bug.Post2。 文件操作函数集:
' Creates a specified file and returns a TextStream object that can be used to read from or write to the file.
' Example of usage
' Set f = CreateFile("d:\temp\beenhere.txt", True)
' f.WriteLine Now
' f.Close
Function CreateFile(sFilename, bOverwrite)
Set fso = CreateObject("scrīpting.FileSystemObject")
Set CreateFile = fso.CreateTextFile(sFilename, bOverwrite)
End Function
' Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file.
' iomode: 1 - ForReading, 2 - ForWriting, 8 - ForAppending
' Example of usage
' Set f = OpenFile("d:\temp\beenhere.txt", 2, True)
' f.WriteLine Now
' f.CloseFunction OpenFile(sFilename, iomode, create)
Set fso = CreateObject("scrīpting.FileSystemObject")
Set ōpenFile = fso.OpenTextFile(sFilename, iomode, create)
End Function
' Appends a line to a file
' Example of usage
' AppendToFile "d:\temp\beenhere.txt", Now
Function AppendToFile(sFilename, sLine)
Const ForAppending = 8
If sFilename = "" Then
sFilename = Environment("SystemTempDir") & "\QTDebug.txt"
End If
Set f = OpenFile(sFilename, ForAppending, True)
f.WriteLine sLine
f.Close
End Function
' Writes a line to a file.
' Destroys the current content of the file!
' Example of usage
' WriteToFile "d:\temp\beenhere.txt", Now
Function WriteToFile(sFilename, sLine)
Const ForWriting = 2
If sFilename = "" Then
sFilename = Environment("SystemTempDir") & "\QTDebug.txt"
End If
Set f = OpenFile(sFilename, ForWriting, True)
f.WriteLine sLine
f.Close
End Function3。 使用qtp发mail
' Example 1
Function SendMail(SendTo, Subject, Body, Attachment)
Set ōl=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 ōl = Nothing
End Function
' Example 2
Function SendMail(SendFrom, SendTo, Subject, Body)
Set ōbjMail=CreateObject("CDONTS.Newmail")
ObjMail.From = SendFrom
ObjMail.To = SendTo
ObjMail.Subject = Subject
ObjMail.Body = Body
ObjMail.Send
Set ōbjMail = Nothing
End Function
4。Excel操作函数集合:
Dim ExcellApp 'As Excel.Application
Dim excelSheet1 'As Excel.worksheet
Dim excelSheet2 'As Excel.worksheet
Set ExcelApp = CreateExcel()
'Create a workbook with two worksheets
ret = RenameWorksheet(ExcelApp, "Book1", "Sheet1", "Example1 Sheet Name")
ret = RenameWorksheet(ExcelApp, "Book1", "Sheet2", "Example2 Sheet Name")
ret = RemoveWorksheet(ExcelApp, "Book1", "Sheet3")
'SaveAs the work book
ret = SaveWorkbook(ExcelApp, "Book1", "D:\Example1.xls")
'Fill worksheets
Set excelSheet1 = GetSheet(ExcelApp, "Example1 Sheet Name")
Set excelSheet2 = GetSheet(ExcelApp, "Example2 Sheet Name")
For column = 1 to 10
For row = 1 to 10
SetCellValue excelSheet1, row, column, row + column
SetCellValue excelSheet2, row, column, row + column
Next
Next
'Compare the two worksheets
ret = CompareSheets(excelSheet1, excelSheet2, 1, 10, 1, 10, False)
If ret Then
MsgBox "The two worksheets are identical"
End If
'Change the values in one sheet
SetCellValue excelSheet1, 1, 1, "Yellow"
SetCellValue excelSheet2, 2, 2, "Hello"
'Compare the worksheets again
ret = CompareSheets(excelSheet1, excelSheet2, 1, 10, 1, 10, True)
If Not ret Then
MsgBox "The two worksheets are not identical"
End If
'save the workbook by index identifier
SaveWorkbook ExcelApp, 1, ""
'Close the Excel application
CloseExcel ExcelApp
' ****************************************** Function Library ***********************************************************Dim ExcelApp 'As Excel.Application
Dim excelSheet 'As Excel.worksheet
Dim excelBook 'As Excel.workbook
Dim fso 'As scrīpting.FileSystemObject
' This function will return a new Excel Object with a default new Workbook
Function CreateExcel() 'As Excel.Application
Dim excelSheet 'As Excel.worksheet
Set ExcelApp = CreateObject("Excel.Application") 'Create a new excel Object
ExcelApp.Workbooks.Add
ExcelApp.Visible = True
Set CreateExcel = ExcelApp
End Function
'This function will close the given Excel Object
'excelApp - an Excel application object to be closed
Sub CloseExcel(ExcelApp)
Set excelSheet = ExcelApp.ActiveSheet
Set excelBook = ExcelApp.ActiveWorkbook
Set fso = CreateObject("scrīpting.FileSystemObject")
On Error Resume Next
fso.CreateFolder "C:\Temp"
fso.DeleteFile "C:\Temp\ExcelExamples.xls"
excelBook.SaveAs "C:\Temp\ExcelExamples.xls"
ExcelApp.Quit
Set ExcelApp = Nothing
Set fso = Nothing
Err = 0
On Error GoTo 0
End Sub
'The SaveWorkbook method will save a workbook according to the workbookIdentifier
'The method will overwrite the previously saved file under the given path
'excelApp - a reference to the Excel Application
'workbookIdentifier - The name or number of the requested workbook
'path - the location to which the workbook should be saved
'Return "OK" on success and "Bad Workbook Identifier" on failure
Function SaveWorkbook(ExcelApp, workbookIdentifier, path) 'As String
Dim workbook 'As Excel.workbook
On Error Resume Next
Set workbook = ExcelApp.Workbooks(workbookIdentifier)
On Error GoTo 0
If Not workbook Is Nothing Then
If path = "" Or path = workbook.FullName Or path = workbook.Name Then
workbook.Save
Else
Set fso = CreateObject("scrīpting.FileSystemObject")
'if the path has no file extension then add the 'xls' extension
If InStr(path, ".") = 0 Then
path = path & ".xls"
End If
On Error Resume Next
fso.DeleteFile path
Set fso = Nothing
Err = 0
On Error GoTo 0
workbook.SaveAs path
End If
SaveWorkbook = "OK"
Else
SaveWorkbook = "Bad Workbook Identifier"
End If
End Function
'The SetCellValue method sets the given 'value' in the cell which is identified by
'its row column and parent Excel sheet
'excelSheet - the excel sheet that is the parent of the requested cell
'row - the cell's row in the excelSheet
'column - the cell's column in the excelSheet
'value - the value to be set in the cell
Sub SetCellValue(excelSheet, row, column, value)
On Error Resume Next
excelSheet.Cells(row, column) = value
On Error GoTo 0
End Sub
'The GetCellValue returns the cell's value according to its row column and sheet
'excelSheet - the Excel Sheet in which the cell exists
'row - the cell's row
'column - the cell's column
'return 0 if the cell could not be found
Function GetCellValue(excelSheet, row, column)
value = 0
Err = 0
On Error Resume Next
tempValue = excelSheet.Cells(row, column)
If Err = 0 Then
value = tempValue
Err = 0
End If
On Error GoTo 0
GetCellValue = value
End Function
'The GetSheet method returns an Excel Sheet according to the sheetIdentifier
'ExcelApp - the Excel application which is the parent of the requested sheet
'sheetIdentifier - the name or the number of the requested Excel sheet
'return Nothing on failure
Function GetSheet(ExcelApp, sheetIdentifier) 'As Excel.worksheet
On Error Resume Next
Set GetSheet = ExcelApp.Worksheets.Item(sheetIdentifier)
On Error GoTo 0
End Function
'The InsertNewWorksheet method inserts an new worksheet into the active workbook or
'the workbook identified by the workbookIdentifier, the new worksheet will get a default
'name if the sheetName parameter is empty, otherwise the sheet will have the sheetName
'as a name.
'Return - the new sheet as an Object
'ExcelApp - the excel application object into which the new worksheet should be added
'workbookIdentifier - an optional identifier of the worksheet into which the new worksheet should be added
'sheetName - the optional name of the new worksheet.
Function InsertNewWorksheet(ExcelApp, workbookIdentifier, sheetName) 'As Excel.worksheet
Dim workbook 'As Excel.workbook
Dim worksheet 'As Excel.worksheet
'In case that the workbookIdentifier is empty we will work on the active workbook
If workbookIdentifier = "" Then
Set workbook = ExcelApp.ActiveWorkbook
Else
On Error Resume Next
Err = 0
Set workbook = ExcelApp.Workbooks(workbookIdentifier)
If Err <> 0 Then
Set InsertNewWorksheet = Nothing
Err = 0
Exit Function
End If
On Error GoTo 0
End If
sheetCount = workbook.Sheets.Count
workbook.Sheets.Add , sheetCount
Set worksheet = workbook.Sheets(sheetCount + 1)
'In case that the sheetName is not empty set the new sheet's name to sheetName
If sheetName <> "" Then
worksheet.Name = sheetName
End If
Set InsertNewWorksheet = worksheet
End Function
'The RenameWorksheet method renames a worksheet's name
'ExcelApp - the excel application which is the worksheet's parent
'workbookIdentifier - the worksheet's parent workbook identifier
'worksheetIdentifier - the worksheet's identifier
'sheetName - the new name for the worksheet
Function RenameWorksheet(ExcelApp, workbookIdentifier, worksheetIdentifier, sheetName) 'As String
Dim workbook 'As Excel.workbook
Dim worksheet 'As Excel.worksheet
On Error Resume Next
Err = 0
Set workbook = ExcelApp.Workbooks(workbookIdentifier)
If Err <> 0 Then
RenameWorksheet = "Bad Workbook Identifier"
Err = 0
Exit Function
End If
Set worksheet = workbook.Sheets(worksheetIdentifier)
If Err <> 0 Then
RenameWorksheet = "Bad Worksheet Identifier"
Err = 0
Exit Function
End If
worksheet.Name = sheetName
RenameWorksheet = "OK"
End Function
'The RemoveWorksheet method removes a worksheet from a workbook
'ExcelApp - the excel application which is the worksheet's parent
'workbookIdentifier - the worksheet's parent workbook identifier
'worksheetIdentifier - the worksheet's identifier
Function RemoveWorksheet(ExcelApp, workbookIdentifier, worksheetIdentifier) 'As String
Dim workbook 'As Excel.workbook
Dim worksheet 'As Excel.worksheet
On Error Resume Next
Err = 0
Set workbook = ExcelApp.Workbooks(workbookIdentifier)
If Err <> 0 Then
RemoveWorksheet = "Bad Workbook Identifier"
Exit Function
End If
Set worksheet = workbook.Sheets(worksheetIdentifier)
If Err <> 0 Then
RemoveWorksheet = "Bad Worksheet Identifier"
Exit Function
End If
worksheet.Delete
RemoveWorksheet = "OK"
End Function
'The CreateNewWorkbook method creates a new workbook in the excel application
'ExcelApp - the Excel application to which an new Excel workbook will be added
Function CreateNewWorkbook(ExcelApp)
Set NewWorkbook = ExcelApp.Workbooks.Add()
Set CreateNewWorkbook = NewWorkbook
End Function
'The OpenWorkbook method opens a previously saved Excel workbook and adds it to the Application
'excelApp - the Excel Application the workbook will be added to
'path - the path of the workbook that will be opened
'return Nothing on failure
Function OpenWorkbook(ExcelApp, path)
On Error Resume Next
Set NewWorkbook = ExcelApp.Workbooks.Open(path)
Set ōpenWorkbook = NewWorkbook
On Error GoTo 0
End Function
'The ActivateWorkbook method sets one of the workbooks in the application as Active workbook
'ExcelApp - the workbook's parent excel Application
'workbookIdentifier - the name or the number of the workbook
Sub ActivateWorkbook(ExcelApp, workbookIdentifier)
On Error Resume Next
ExcelApp.Workbooks(workbookIdentifier).Activate
On Error GoTo 0
End Sub
'The CloseWorkbook method closes an open workbook
'ExcelApp - the parent Excel application of the workbook
'workbookIdentifier - the name or the number of the workbook
Sub CloseWorkbook(ExcelApp, workbookIdentifier)
On Error Resume Next
ExcelApp.Workbooks(workbookIdentifier).Close
On Error GoTo 0
End Sub
'The CompareSheets method compares between two sheets.
'if there is a difference between the two sheets then the value in the second sheet
'will be changed to red and contain the string:
'"Compare conflict - Value was 'Value2', Expected value is 'value2'"
'sheet1, sheet2 - the excel sheets to be compared
'startColumn - the column to start comparing in the two sheets
'numberOfColumns - the number of columns to be compared
'startRow - the row to start comparing in the two sheets
'numberOfRows - the number of rows to be compared
Function CompareSheets(sheet1, sheet2, startColumn, numberOfColumns, startRow, numberOfRows, trimed) 'As Boolean
Dim returnVal 'As Boolean
returnVal = True
'In case that one of the sheets doesn't exists, don't continue the process
If sheet1 Is Nothing Or sheet2 Is Nothing Then
CompareSheets = False
Exit Function
End If
'loop through the table and fill values into the two worksheets
For r = startRow to (startRow + (numberOfRows - 1))
For c = startColumn to (startColumn + (numberOfColumns - 1))
Value1 = sheet1.Cells(r, c)
Value2 = sheet2.Cells(r, c)
'if 'trimed' equels True then used would like to ignore blank spaces
If trimed Then
Value1 = Trim(Value1)
Value2 = Trim(Value2)
End If
'in case that the values of a cell are not equel in the two worksheets
'create an indicator that the values are not equel and set return value
'to False
If Value1 <> Value2 Then
Dim cell 'As Excel.Range
sheet2.Cells(r, c) = "Compare conflict - Value was '" & Value2 & "', Expected value is '" & Value1 & "'."
Set cell = sheet2.Cells(r, c)
cell.Font.Color = vbRed
returnVal = False
End If
Next
Next
CompareSheets = returnVal
End Function5。WebTable功能函数集合:
' ************************************************** Function Library ********************************
' Registering both functions
RegisterUserFunc "WebTable", "ObjectsByMicClass", "ObjectsByMicClass"
RegisterUserFunc "WebTable", "ItemByKeyColumn", "ItemByKeyColumn"
' Function: ObjectsByMicClass
' Descrīption: Returns a collection of objects all the objects in a
' WebTable that have the specified MicClass
' Return Value: A Collection of Objects
' Arguments:
' Obj - Test Object (WebTable)
' micClass - The micClass of the objects to retrieve
'-----------------------------------------------------------------------------------------------------------
Function ObjectsByMicClass(Obj, micClass)
Set Table = Obj
' Create a collection object to hold the items
Set ōbjCollection = CreateObject("scrīpting.Dictionary")
' Go over all the cells in the table, and look for objects with the specified micClass
For row=1 to Table.RowCount
ColumnCount=Table.ColumnCount(row)
For col=1 to ColumnCount
For ItemIndex=0 to Table.ChildItemCount(row, col, micClass)-1
Set childItem=Nothing
Set childItem = Table.ChildItem(row, col, micClass, ItemIndex)
If Not childItem is Nothing Then
' If the cell contains a micClass object, add it to the collection
ItemKey = objCollection.Count + 1
objCollection.Add ItemKey, childItem
End if
Next
Next
Next
Set ōbjectsbyMicClass = objCollection
End Function
' Function: ItemByKeyColumn
' Descrīption: Returns an item from a column, based on the value of a
' key column
' Return Value: Object
' Arguments:
' Obj - Test Object (WebTable)
' KeyColumnIndex - Index of the KeyColumn
' KeyColumnValue - Value to search for in the key column
' KeyItemIndex - Index of the value in the key column (if there is
' more than one). If 0, the first item will be used.
' TargetColumnIndex - Column from which to retrieve the target item
' micClass - The micClass of the target item
' TargetItemIndex - Index of the target item to retrieve (if there is
' more than one). If 0, the first item will be used.
' ------------------------------------------------------------------------------------------------------------------------------------
Function ItemByKeyColumn(Obj, KeyColumnIndex, KeyColumnValue, KeyItemIndex, TargetColumnIndex, micClass, TargetItemIndex)
Table = Obj
rowCount = Table.RowCount
' if TargetItemIndex was not specified, use 1 as deafult
If TargetItemIndex < 1 Then
TargetItemIndex = 1
End If
' if KeyColumnIndex was not specified, use 1 as default
If KeyItemIndex < 1 Then
KeyItemIndex = 1
End If
' look for KeyColumnValue in the key column to determine which
' row to retrieve the targe item from
Row = 0
foundIndex = 0
While Row <= RowCount And foundIndex < KeyItemIndex
Row = Row + 1
CellData = Table.GetCellData(Row, KeyColumnIndex)
If CellData = KeyColumnValue Then
foundIndex = foundIndex + 1
End If
Wend
If foundIndex < KeyItemIndex Then
Exit Function
End If
' Now that we know the row, retrieve the item (according to its micClass)
' from the target column.
ChildItemsCount = Table.ChildItemCount(Row, TargetColumnIndex, micClass)
If ChildItemsCount > =1 And ChildItemsCount >= TargetItemIndex Then
Set GetItemByKeyColumn = Table.ChildItem(Row, TargetColumnIndex, micClass, TargetItemIndex-1)
End If
End Function
' ************************************ Examples that use these functions *******************************************************
' Using the ItemByKeyColumn Function
Set ōbj = Browser("Table with objects").Page("Itenerary: Mercury Tours").WebTable("Acapulco to Zurich").ItemByKeyColumn(1,"FLIGHT",2,3,"WebElement",1)
msgbox obj.GetROProperty("innerhtml")
' Using the ObjectsByMicClass function
Set collection = Browser("Browser").Page("Page").WebTable("Table").ObjectsByMicClass("WebCheckBox")
For i=1 to collection.count
If collection(i).GetROProperty("checked") Then
collection(i).Set "OFF"
Else
collection(i).Set "ON"
End If
Next
清空Cookie - 联系我们 - 51Testing软件测试网 - 交流论坛 - 空间列表 - 站点存档 - 升级自己的空间
Powered by 51Testing
© 2003-2021
沪ICP备05003035号