鲁豫胖的时候照片:vbscript?脚本学习笔记不断更新<二>

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 12:20:08

vbscript 脚本学习笔记不断更新<二>

(2008-08-20 21:42:58)转载 标签:

qtp

vbscript

environment

环境变量

脚本

it

分类: QTP测试工具

13.导入外部xml文件的环境变量,并取其值实例

'导入外部文件
Environment.LoadFromFile("D:\test\test.xml")
'读取环境变量的myusername 值
myusername=Environment.Value("myusername")
'读取环境变量的 mypassword值
mypassword=Environment.Value("mypassword")
'读取环境变量的myvarian 值
myvariant=Environment.Value("myvariant")
Reporter.ReportEvent micdone,"读取test.xml文件","myusername="&myusername&",mypassword = "&mypassword&",myvariant = "&myvariant

 

外部test.xml文件内容如下:


 
  mypassword
  mercury
 

 
  myusername
  Cheers_Lee
 

 
  myvariant
  10
 


 

提示:附加一个小实例

'获取当前导入的环境变量文件名
filename=Environment.ExternalFileName
Reporter.ReportEvent micDone,"当前导入环境变量名",filename
'如果为空则导入
If (filename="") Then
 Environment.LoadFromFile("D:\test\test.xml")
End If
'导入后可以直接使用环境变量
msgbox Environment("myusername")


14、QTP中Print函数的一个实例

 

Dialog("Login").WinEdit("Agent Name:").Set "Cheers_Lee"
Dialog("Login").WinEdit("Password:").Set "mercury"
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").ActiveX("MaskEdBox").Type "082208"
Window("Flight Reservation").WinComboBox("Fly From:").Select "Frankfurt"
Window("Flight Reservation").WinComboBox("Fly To:").Select "London"

Window("Flight Reservation").WinButton("FLIGHT").Click

'定位到Flights Table 窗口的WinList控件
Set FlightsList=Window("Flight Reservation").Dialog("Flights Table").WinList("From")
'获取Winlist控件的所有项,并通过Print方法现实出来
For i=1 to FlightsList.GetItemsCount
 Print FlightsList.GetItem(i-1)
Next
wait 5

Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Name:").Set "liujianting"
Window("Flight Reservation").WinButton("Insert Order").Click
Window("Flight Reservation").Close

15、QTP中wait函数和GetROProperty方法的一个实例

Dialog("Login").WinEdit("Agent Name:").Set "Cheers_Lee"
Dialog("Login").WinEdit("Password:").Set "mercury"
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").ActiveX("MaskEdBox").Type "082208"
Window("Flight Reservation").WinComboBox("Fly From:").Select "Frankfurt"
Window("Flight Reservation").WinComboBox("Fly To:").Select "London"
Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Name:").Set "liujianting"
Window("Flight Reservation").WinButton("Insert Order").Click
wait 10
'获取自动生成的订票记录号
msgbox Window("Flight Reservation").WinEdit("Order No:").GetROProperty("text")
Window("Flight Reservation").Close
16.用Executefile执行VBScript脚本

脚本文件myfunction.vbs包括如下内容:

addtest 1,9

Function addtest(a,b)
   Dim c
   c=a+b
   msgbox c
   addtest=c
End Function
在QTP中编写如下脚本来执行myfunction.vbs,调用addtest函数

executefile "D:\test\myfunction.vbs"
x=addtest(8,9)
print "调用myfunction.vbs中的函数addtesth返回值为:" & x

17.在测试过程中设置测试选项DefaultTimeOut

注意:这个方法和在File---settings----run 设置的DefaultTimeOut是一样的。

 

'获取DefaultTimeOut
DefaultTimeOut=Setting("DefaultTimeOut")
If DefaultTimeOut<50000  Then
 msgbox (DefaultTimeOut)
 Milliseconds=50000
 Setting("DefaultTimeOut")=Milliseconds
End If

18.巧用Setting对象的Item属性

 

With Setting
'判断名为IterNUmber的Item是否存在,如果不存在,则添加一个,并且设置其值为1
If not.exists("IterNumber") Then
 .add "IterNumber",1
 '如果存在,则把其值累加1
else
.Item("IterNumber")=.Item("IterNumber")+1
End If
End with
msgbox Setting("IterNumber")

 -------------------------------------------

IterNumber=Environment.Value("IterNumber")
If IterNumber<20  Then
 Environment.Value("IterNumber")=20
End If
msgbox Environment.Value("IterNumber")


19.Desktop 对象的使用

(1)capture方法来截屏

Desktop.CaptureBitmap"D:\test\testcapturebitmap.bmp",true

(2)使用ChildObjects方法获取某个对象中包含的对象列表

Set objdesc=description.Create()
objdesc("text").value="无标题-记事本"

’通过Desktop对象的Childobjects方法获取当前桌面中所有名为“无标题-记事本”的窗口对象
Set objectcollection=desktop.childobjects(objdesc)
msgbox objectcollection.count

 

 20.Clipboard的使用方法实例

Set myclipboard=createobject("Mercury.Clipboard")
myclipboard.clear
myclipboard.settext "test"
msgbox myclipboard.Gettext

 21.在VBScript脚本中设置qtp选项

dim qtapp
dim qtoptions
Set qtapp=createobject("QuickTest.application")
qtapp.Launch
qtapp.visible=true
'获取Windows Applications Options 对象
set qtoptions=qtapp.options.windowsapps

qtoptions.attachedtextarea="bottomleft"

qtoptions.attachedtextradius=50

qtoptions.expandmenutoretrieveproperties=true

qtoptions.nonuniquelistitemrecordmode="byindex"

qtoptions.recordownerdrawnbuttonas="checkboxes"

set qtoptions=nothing

set qtapp=nothing

将脚本保存为:.vbs文件双击运行,执行操作。