认真的生活,快乐的工作~ 做自己喜欢做的事,更要喜欢自己做的事~ 学习、工作、生活的路途中,大家一路相随~

QTP学习 转

上一篇 / 下一篇  2011-01-21 16:33:00 / 个人分类:测试相关

----关于打开excel并保存数据

涉及到数据操作方面的学习,调试了一个脚本,关于打开已存在的一个excel文件,并在第二行第二列输入test

1  Set excelObj=Createobject("Excel.application")
2  xlsUrl="d:\test2.xls"
3  excelObj.workbooks.open(xlsUrl)
4  excelObj.cells(2,2).value="test"
5 excelObj.ActiveWorkbook.save
6 excelObj.quit

7 Set excelobj=nothing

----有关数据库的操作

实现了把数据库里的数据导入excel表的功能,本代码不需要设置数据源和安装sqlserver2005,不过要先在d盘建立一个test.xls文件,大家共同学习:)

Dim cnn,rst,strCnn,i
i=1
Set excelobj=createobject("excel.application")'设置excel对象
xlsurl="d:\test.xls"
excelobj.workbooks.open(xlsurl)'打开excel对象
Set sheetNew=excelobj.sheets.item(1)'设置excel文件中的第一页为操作对象
Set cnn=createobject("adodb.connection")'连接数据库对象
Set rs=createobject("adodb.recordset") '数据库的记录数对象
strcnn="driver=sql server;server=192.168.2.6;uid=sa;pwd=111111;app=microsoft office 2003;wsid=WWW-8667BF09E6F;database=qtp" 'server为数据库服务器的ip地址,wsid为本机器名,database为数据库名
cnn.open strcnn '打开数据库连接
If cnn.state=0 Then '连接是否建立,失败弹出提示框
  msgbox "failed"
  else
  msgbox "pass"
End If

rs.open "select * from test",cnn '把sql语句放入数据库里执行
rs.movefirst     '指向第一条记录
Do while not rs.eof '如果下一条记录不存在则跳出循环
 a=rs("a").value     '字段a的值赋给变量a
 b=rs("b").value
 sheetNew.cells(i,1).value=a '把a的值赋给 excel表的第一个框
  sheetNew.cells(i,2).value=b
  i=i+1
  rs.movenext '移动到下一条
Loop
excelobj.activeworkbook.save '保存excel文件
excelobj.quit       '关闭对象
Set excelobj=nothing '释放对象
cnn.close '关闭连接
Set cnn=nothing'释放连接

----用vbs写的屏蔽网站并取消屏蔽的程序

写了个屏蔽网站的程序,也许对家长有帮助,代码如下,跟大家共同交流。。

屏蔽网站:

On Error Resume Next '容错
Dim url '屏蔽的地址
Set wc=CreateObject("wscript.shell")
Set fso=CreateObject("scripting.filesystemobject") '创建文本对象
Set p=fso.OpenTextFile("C:\WINDOWS\system32\drivers\etc\hosts",8,true)'打开文本
If Err.Description="没有权限" Then '查看是否有权限操作
MsgBox "你没有操作系统的权限,请使用管理员帐号登陆!",,"系统提示"'弹出提示
Else
If Err.Number<>0 Then'查看其它问题
MsgBox Err.Description&",请与作者联系(博客:http://www.51testing.com/index.php?uid-99915",,"系统提示"
else
url=Trim(InputBox("请输入你要屏蔽的域名,请以www开头","屏蔽地址"))
if url="" then
MsgBox "操作取消,没有网站被屏蔽",,"系统提示"
else
op.WriteLine("127.0.0.1"&chr(32)&chr(32)&chr(32)&chr(32)&chr(32)&chr(32)&chr(32)&url)'输入文本
MsgBox "屏蔽成功,请重新打开浏览器访问网站:)",,"系统提示"
End if
End if
End if
op.Close
Set fso=nothing

取消屏蔽:

On Error Resume next
Dim url,readtxt
Set wc=CreateObject("wscript.shell")
Set fso=CreateObject("scripting.filesystemobject")'创建文本操作对象
Set p=fso.OpenTextFile("C:\WINDOWS\system32\drivers\etc\hosts",1,true)'打开文本
readtxt=op.ReadAll'得到文本内容
op.close'关闭文本流
url=Trim(InputBox("请输入你要取消屏蔽的域名,以www开头","取消屏蔽地址"))
if url="" then '没有输入时,退出
MsgBox "操作取消,没有网站被取消屏蔽",,"系统提示"
else
readtxt=Replace(readtxt,"127.0.0.1"&chr(32)&chr(32)&chr(32)&chr(32)&chr(32)&chr(32)&chr(32)&url&Chr(13) & Chr(10),"")'替换所有输入的地址
fso.DeleteFile("C:\WINDOWS\system32\drivers\etc\hosts")'删除host文件
If Err.Description="没有权限" Then '判断是否有权限
MsgBox "你没有操作系统的权限,请使用管理员帐号登陆!",,"系统提示"
Else
If Err.Number<>0 Then
MsgBox Err.Description&",请与作者联系(博客:http://www.51testing.com/index.php?uid-99915",,"系统提示"
else
Set newfile=fso.OpenTextFile("C:\WINDOWS\system32\drivers\etc\hosts",8,true)'打开文件,如果不存在,新建
newfile.Write(readtxt)'重新写入内容
MsgBox "取消屏蔽成功,请重新打开浏览器访问网站:)",,"系统提示"'弹出提示
newfile.Close '关闭文件流
Set fso=nothing
End if
End if
End if

编译好的屏蔽网站并取消屏蔽的程序见附件。

----问题

问题:

一 应用程序和web程序录制时一起运行

今天准备使用应用程序,结果选定后web程序也打开了,最后发现需要在初始化时重新选择插件才能避免,在tool------aption中,选中display add-in Manager on startup,再重新打开qtp,不选web,选择另外两个,一切ok

二 class name在描述性编程中不能使用的问题

  在自带的应用程序中

Set descEditLogin=description.Create()
'descEditLogin("Class Name").value="Dialog"
descEditLogin("regexpwndtitle").value="login"
Dialog(descEditLogin).WinEdit("Attached text:=Agent Name:").Set "17testing"
Dialog(descEditLogin).WinEdit("Attached text:=Password:").SetSecure "4b18a447cec84f211463ff232a69197f1bc8f819"
Dialog(descEditLogin).Winbutton("class name:=winbutton","text:=cancel").Click

最后一行的winbutton如果加的两个属性,其中一个为class name时,运行始终不通过,同理,在上面的注释部分,dialog对象如果加上class name,也不能通过,百思不得其解,希望能遇到高人解答
三 导入外部xml文件

在d盘建立一个test.xml文件,代码如下:

<Environment>
 <Variable>
  <Name>aaa</Name>
  <Value>111</Value>
 </Variable>
</Environment>

然后在程序中导入和调用

environment.LoadFromFile  "d:\test.xml"
Dim ss
ss=environment.Value("aaa")
msgbox ss

---qtp自动记录日志的函数

想在qtp运行过程中输出一些自己检查的结果。注意,需要在qtp的环境变量中设置你把日志保存的目录,如LogDir=d:\log\

function  WriteLogMsg(str)
   Dim fso,MyFile,fileAddress
   Set fso=createobject("scripting.FileSystemObject")
   fileAddress=Environment("LogDir")+"QtpLog"+CStr(Date)+".txt"
   if(fso.FileExists(fileAddress)=false)   then
    if(fso.FolderExists(Environment("LogDir"))=false) then
  fso.createFolder(Environment("LogDir"))
 end if
     fso.createTextFile(fileAddress)
   end if
   Set MyFile=fso.OpenTextFile(fileAddress,8,True)
   MyFile.WriteLine(str)
   MyFile.Close
   Set MyFile=nothing
   Set fso=nothing
end function

----截图函数

function CutPic(name)
Set bjQTP=getobject("","quicktest.application")
  bjQTP.windowstate="Minimized"
 Set bjQTP=nothing
Desktop.CaptureBitmap  GetPath(name),true
end function

function GetPath(name)
dim dateFolder,fileAddress
 Set fso=createobject("scripting.FileSystemObject")
  dateFolder=CStr(split(Date,"-")(0) & split(Date,"-")(1)& split(Date,"-")(2))
if(fso.folderexists(Environment("LogDir")+dateFolder)) then
   fileAddress=Environment("LogDir") & dateFolder & "\" & name &CStr(split(time,":")(0) & split(time,":")(1)& split(time,":")(2)) &".bmp"
 else
  fileAddress="d:\default.bmp"
end if
GetPath=fileAddress
set fso=nothing
end function

----抛出异常处理函数

 '注:里面用到的writeLogMsg函数是我前面写到的一个函数,可以自动记录日志

For  i=1 to 2
Err.Clear
On Error Resume Next
   Select Case i
   case 1 Call 模块一()
   case 2  call 模块二()
   End Select
If Err.Number <> 0 Then
  WriteLogMsg("系统执行异常,请检查源代码" )

 WriteLogMsg( Err.Number ) '错误码
WriteLogMsg Err.Description  '错误描述
   WriteLogMsg("-------------------------------------------------------------------------" )
End if
Err.Clear
On Error goto 0
 Next

---- 压缩文件

'压缩文件
function PressFile()
on error resume next
set ws=createobject("wscript.shell")
set fso=createobject("scripting.FileSystemObject")
dim Pressdate
pressdate=split(date,"-")(0) & split(date,"-")(1) & split(date,"-")(2)
m1=" D:\log\" & pressdate &".rar"
m2=" D:\log\" & pressdate
m3="D:\log\" & pressdate &".rar"
m4="D:\log\" & pressdate
if (fso.FolderExists(m4)) then
mm="WinRAR a -r"&m1&m2
myre=ws.run(mm,1,True)
end if
set fso=nothing
set ws=nothing
PressFile=m3
end function
call PressFile()

-----通过时间循环来定时启动脚本

'通过时间循环来定时启动脚本
dim IsStart,timeStart
timeStart="17:07"
IsStart=true
while IsStart
  wscript.sleep 1000
  if cstr(Hour(now))=split(timeStart,":")(0) then
    if cstr(minute(now))=cstr(cint(split(timeStart,":")(1))) then
       set ws =createobject("wscript.shell")
       ws.run "D:\function\All.vbs"
       set ws=nothing
       IsStart=false
    end if
  end if
wend

---取当前时间

dim timeNow

timeNow=dotnetfactory.createinstance("System.DateTime").Now.tostring("yyMMddHHmmss")

msgbox timeNow



TAG: QTP qtp

 

评分:0

我来说两句

日历

« 2024-05-02  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 11766
  • 日志数: 15
  • 建立时间: 2010-12-15
  • 更新时间: 2011-02-28

RSS订阅

Open Toolbar