1.环境变量分为qtp 内置变量和用户 自定义变量
2.设置环境变量:file->settings->environment
3.使用环境变量代码
'qtp内置环境变量-------------------------
'获取内置环境变量TestDir、TestName、ActionName的值
testdir=environment.Value( "TestDir")
'print testdir
testname=environment.Value ("TestName")
actionname=environment.Value ("ActionName")
datearry=split(Date,"/",-1,1)
'msgbox Date
daten=datearry(0)&datearry(1)&datearry(2)
filename=testdir&"\"&testname&"_"&actionname&"_"&daten&".png"
'将截图以png或bmp格式保存到testdir路径中
desktop.CaptureBitmap filename,true
'将截图插入测试报告中
reporter.ReportEvent micPass ,"桌面截图","截图实例",filename
'qtp自定义变量,利用外部文件xml配置环境变量------------------------
Function createEnviXnml(xmlpath)
'创建fso对象
Set fso=createobject("Scripting.FileSystemObject")
'利用fso创建xml文件
Set oxml=fso.CreateTextFile(xmlpath)
'在xml文件中写入环境变量,包含变量名和变量值
oxml.Write "<Environment>"+vbCrLf+"<Variable>"+vbCrLf+"<Name>netname</Name>"+vbCrLf+ "<Value>baidu</Value>"+vbCrLf+"</Variable>"+vbCrLf+ "<Variable>"+vbCrLf+ "<Name>neturl</Name>"+vbCrLf+ "<Value>http://www.baidu.com</Value>"+vbCrLf+"</Variable>"+vbCrLf+"</Environment>"
Set oxml=nothing
Set fso=nothing
End Function
'注释:在调试上述函数中write里面的内容时,按照教材上的方式写的代码,总是报错“expected statement”,
'经排查发现是因为不能使用“_”进行换行连接代码语句
Dim environmentpath
environmentpath="d:\environ.xml"
'调用函数创建自定义环境变量的xml文件
Call createEnviXnml(environmentpath)
'动态加载自定义环境变量
environment.LoadFromFile environmentpath
'获取自定义环境变量neturl的值
url=environment.Value("neturl")
systemutil.Run url