Library Files库文件

库文件是包含VBScript代码的纯文本文件。用于声明函数,变量,类等。库文件可以以任何扩展名类型存储,最常见的是VBSTXT。库文件有助于将代码组织成可区分的功能模块。库文件使得不同的QTP脚本共享代码。下面将介绍加载库文件的2中方法。

Associating a Library globally with a Test将库文件和测试脚本全局关联

使用该方法,同一个库文件的实例可以被一个测试脚本里的所有Action共享和访问。操作方法:菜单FileàSettings…àResources(标签),添加可文件,如下图所示:

NOTE1: 库文件是从下往上加载的。如果2个不同的库文件中同时含有一个定义完全一样的函数,那么将使用列表中靠上的那个。

NOTE2: 如果库文件B依赖于库文件A中的定义,那么库文件A要位于列表的底部。

NOTE3: QTP添加库文件时使用的是完整路径。不过建议使用相对路径,如”..\test.vbs”。

Dynamically Loading libraries locally at run-time在运行时动态加载局部库文件

QTP提供ExecuteFile函数在运行时动态加载库文件。使用该方法加载的库文件,其内容之对于使用ExecuteFile函数的Action是可见的。下面是一些了例子:

<代码部分暂略>

NOTE: 如果ActionAActionB都通过ExecuteFile函数加载了test.vbs,且ActionA调用ActionBActionAActionB将使用各自的库文件副本和库文件中所有变量和函数的独立的实例。

Dynamically Loading libraries globally at run-time在运行时动态加载全局库文件

如上所述,直接在一个Action中使用ExecuteFile函数来加载库文件,该库文件只对于该Action是可见的。但是每一个QTP脚本可以有一个对于所有Action都可用的全局库文件集。这样,不是通过直接在每个Action中使用ExecuteFile函数来加载库文件,而是在全局库文件中使用一个函数来实现,那么该库文件就可以被所有Action可用了。

'C:\Liboader.vbs

Public Function ExecuteFileGlobal(ByVal fileName)

   ExecuteFile fileName

End Function

将上述代码保存成VBS文件并和测试脚本关联,然后再任何Action中使用ExecuteFileGlobal函数来加载文件。通过ExecuteFileGlobal函数加载的文件,该测试脚本中的所有Action都是可用的。

'在全局范围内加载文件

ExecuteFileGlobal "C:\Test.vbs"

如果一个Action要循环执行多次,调用ExecuteFileGlobal函数将重复加载库文件多次,这样每次重新加载库文件时,将破坏该库文件中全局变量的当前状态。

该问题可以通过下面的代码来解决。在ExecuteFileGlobal中加一个重载标记,当重载标记为false时,库文件不会被重复加载。创建一个所有被加载文件的全局数据字典文件路径用于判断该文件是否已被加载。

' C:\LoadLibrary.vbs

Dim loadedFiles

Set loadedFiles=CreateObject("Scripting.Dictionary")

loadedFiles.CompareMode=vbTestCompare '执行文本比较,  字母不区分大小写. vbBinaryCompare是执行二进制比较字母区分大小写的.

'使用ExecuteFileGlobal在运行时加载文件

输入参数  strFile:要加载的文件的完全文件名(即绝对路径)

reload用于标记当文件已被加载过时,是否要重复加载

Public function ExecuteFileGlobal (ByVal strFile, ByVal reLoad )' ByVal是传值, By Rel是传地址缺省为By Rel

如果reLoadfalse, 就检查文件是否已被加载过

If  reload=false and loadedFiles.exists(strFile) Then

如果已被加载过,就退出加载函数

ExecuteFileGlobal=false

Exit function

End If

‘加载文件

executefile  strFile

loadedFiles(strFile)=true  ‘将文件加到数据字典中,标记该文件已被加载过

ExecuteFileGlobal=true

End function

为了更好的理解上述代码,我们来举个例子。首先创建一个将在运行时加载的库文件。

D:\testA.vbs

dim x

x=2

下面的代码演示了ExecuteFileGlobal的使用方法

‘在全局范围内加载testA.vbs

ExecuteFileGlobal "D:\testA.vbs", false

msgbox x

x=x+2

‘在全局范围内加载testA.vb,已加载时不重复加载

ExecuteFileGlobal "D:\testA.vbs", false

msgbox x

‘在全局范围内加载testA.vb

ExecuteFileGlobal "D:\testA.vbs", true

msgbox x

问题9-1: 如何在运行时定义全局变量

有时需要在运行时在2个或多个action 之间共享变量的值。同样使用加载全局库文件的方式,来创建全局变量:

<代码部分暂略>

Understanding Executing Scope理解执行范围

理解和区分测试脚本的全局和局部范围很重要。

l 全局范围是QTP加载所有所有源文件和场景恢复库文件的地方。它可以被脚本中所有的Action访问。

l 局部范围是action范围,在action里定义的任何代码,不能被该action以外的代码访问。

9-2描述了含有2actionQTP 的执行范围。这就是QTP运行的方式。

l 当脚本开始运行时,QTP创建了一个全局范围。

l QTP首先添加相关场景恢复的库文件。使场景恢复从上往下被关联。

l 当加载了所有场景恢复的文件后,QTP然后加载在TestàSettings…àResource(标签)中关联的文件,这些文件是从下往上关联的。

l 然后QTP按照在测试流中定义的顺序调用每个actionQTP为每个action创建一个局部范围,该范围是私有的,只有该action可见。该局部范围在action每次循环过程中创建和销毁。

l 在action1中定义的函数不能被其他action和全局范围访问。

NOTE: 如果全局范围内有多个库文件包含同名的函数,将调用最新被加载的那个库文件里的函数 。

Applicability of Option Explicit(看不懂)

Executing code in Local scope from within Global Scope

前面我们学习了如何在局部范围加载局部库文件(“在运行时动态加载局部库文件”), 及如何在局部范围内加载全局库文件(“在运行时动态加载全局库文件”)。

有时,由于脚本维护的需要,我们可能会在action的开始或末尾添加代码。这种维护可能需要应用在多个脚本上。QTP只允许一次打开一个单独的脚本,这样要编辑一次编辑多个脚本就很费时。为了避免这中情况,可以在action中调用下面2个特殊的函数。

《代码略》-代码看不懂

Title: BSD Hack Author: Ajay Kumar Tiwari Length: 410 pages Edition: 1 Language: English Publication Date: 2015-03-22 ISBN-10: B00V3DWD80 In the world of Unix operating systems, the various BSDs come with a long heritage of high-quality software and well-designed solutions, making them a favorite OS of a wide range of users. Among budget-minded users who adopted BSD early on to developers of some of today's largest Internet sites, the popularity of BSD systems continues to grow. If you use the BSD operating system, then you know that the secret of its success is not just in its price tag: practical, reliable, extraordinarily stable and flexible, BSD also offers plenty of fertile ground for creative, time-saving tweaks and tricks, and yes, even the chance to have some fun."Fun?" you ask. Perhaps "fun" wasn't covered in the manual that taught you to install BSD and administer it effectively. But BSD Hacks, the latest in O'Reilly's popular Hacks series, offers a unique set of practical tips, tricks, tools--and even fun--for administrators and power users of BSD systems.BSD Hacks takes a creative approach to saving time and getting more done, with fewer resources. You'll take advantage of the tools and concepts that make the world's top Unix users more productive. Rather than spending hours with a dry technical document learning what switches go with a command, you'll learn concrete, practical uses for that command.The book begins with hacks to customize the user environment. You'll learn how to be more productive in the command line, timesaving tips for setting user-defaults, how to automate long commands, and save long sessions for later review. Other hacks in the book are grouped in the following areas: Customizing the User Environment Dealing with Files and Filesystems The Boot and Login Environments Backing Up Networking Hacks Securing the System Going Beyond the Basics Keeping Up-to-Date Grokking BSD If you want more than your average BSD user--you want to explore and experiment, unearth shortcuts, create useful tools, and come up with fun things to try on your own--BSD Hacks is a must-have. This book will turn regular users into power users and system administrators into super system administrators.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值