这些是4年前在HP用QTP做自动化测试时候总结的一些,现在贴出来,说不准以后会不会用到

当初花了2天时间写的一个自动生成的Excel Report

Public Function Report (status, objtype, text)
Dim TestName Reporter.Filter = rtEnableAll
Reporter.ReportEvent status, objtype, text
Reporter.Filter = rfDisableAll Call WExcel(status,objtype,text) End Function Function CreateExcel(sFolderPath)
Dim cTestName_Sum,cStatus_Sum,cSum_Sum,cPass_Sum,cFail_Sum,cTime_Sum
Dim cTestName,cStep,cStatus,cDetail,cTime,cPicName
Dim oFile,oExcel,sExcelPath,sNewBook,sNewSheet cTestName_Sum =
cStatus_Sum =
cSum_Sum =
cPass_Sum =
cFail_Sum =
cTime_Sum = cTestName =
cStep =
cStatus =
cDetail =
cTime =
cPicName =
Set oFile = CreateObject("Scripting.FileSystemObject")
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = False If not oFile.FolderExists(sFolderPath) Then
oFile.CreateFolder(sFolderPath)
End If sExcelPath = sFolderPath&"/Result.xls" If not oFile.FileExists(sExcelPath) Then
Set sNewBook = oExcel.Workbooks.Add
With sNewBook.Worksheets() End With
With sNewBook.Worksheets()
.Activate
.Cells(,cTestName_Sum).value = "TestName"
.Cells(,cStatus_Sum).value = "Status"
.Cells(,cSum_Sum).value = "Sum Num"
.Cells(,cPass_Sum).value = "Passed Num"
.Cells(,cFail_Sum).value = "Failed Num"
.Cells(,cTime_Sum).value = "TestTime"
.Name = "Summary"
.Rows().Font.Bold = True
.Columns(cTestName_Sum).ColumnWidth=
.Columns(cStatus_Sum).ColumnWidth=
.Columns(cSum_Sum).ColumnWidth=
.Columns(cPass_Sum).ColumnWidth=
.Columns(cFail_Sum).ColumnWidth=
.Columns(cTime_Sum).ColumnWidth=
End With
With sNewBook.Worksheets()
.Activate
.Cells(,cTestName).value = "TestName"
.Cells(,cStep).value = "Step Object"
.Cells(,cStatus).value = "Status"
.Cells(,cDetail).value = "Result Detail"
.Cells(,cTime).value = "TestTime"
' .Cells(1,cPicName).value = "Capture Screen Name"
.Name = "Passed Step"
.Rows().Font.Bold = True
.Columns(cTestName).ColumnWidth=
.Columns(cStep).ColumnWidth=
.Columns(cStatus).ColumnWidth=
.Columns(cDetail).ColumnWidth=
.Columns(cTime).ColumnWidth=
.Columns(cPicName).ColumnWidth=
End With
With sNewBook.Worksheets()
.Activate
.Cells(,cTestName).value = "TestName"
.Cells(,cStep).value = "Step Object"
.Cells(,cStatus).value = "Status"
.Cells(,cDetail).value = "Result Detail"
.Cells(,cTime).value = "TestTime"
.Cells(,cPicName).value = "Capture Screen Name"
.Name = "Failed Step"
.Rows().Font.Bold = True
.Columns(cTestName).ColumnWidth=
.Columns(cStep).ColumnWidth=
.Columns(cStatus).ColumnWidth=
.Columns(cDetail).ColumnWidth=
.Columns(cTime).ColumnWidth=
.Columns(cPicName).ColumnWidth=
End With
sNewBook.SaveAs sExcelPath
oExcel.Application.quit
Set sNewBook = Nothing
' CreateExcel = sExcelPath
End If End Function Function WExcel(Status,sStep,sDetail)
Dim cTestName_Sum,cStatus_Sum,cSum_Sum,cPass_Sum,cFail_Sum,cTime_Sum
Dim cTestName,cStep,cStatus,cDetail,cTime,cPicName
Dim oFile,oExcel,sExcelPath,sNewBook,sNewSheet
Dim iLen,iLenPass,iLenFail,sTestName,sFolderPath
sTestName = Environment.Value("TestName")
sFolderPath = "C:/FP_Results" cTestName_Sum =
cStatus_Sum =
cSum_Sum =
cPass_Sum =
cFail_Sum =
cTime_Sum = cTestName =
cStep =
cStatus =
cDetail =
cTime =
cPicName = CreateExcel(sFolderPath)
' msgbox sExcelPath Set oFile = CreateObject("Scripting.FileSystemObject")
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = False
sExcelPath = sFolderPath&"/result.xls" Set sNewBook = oExcel.Workbooks.Open(sExcelPath)
Set sNewSheet = sNewBook.Worksheets()
Set sNewSheetPass = sNewBook.Worksheets()
Set sNewSheetFail = sNewBook.Worksheets()
iLen = sNewSheet.UsedRange.Rows.count
iLenPass = sNewSheetPass.UsedRange.Rows.count
iLenFail = sNewSheetFail.UsedRange.Rows.count If Status = Then
With sNewSheetPass
.Activate
.Cells(iLenPass+,cTestName).value = sTestName
.Cells(iLenPass+,cStep).value = sStep
.Cells(iLenPass+,cDetail).value = sDetail
.Cells(iLenPass+,cTime).value = now
.Cells(iLenPass+,cStatus).value = "Passed"
.Cells(iLenPass+,cStatus).Font.Color = vbGreen
.Cells(iLenPass+,cStatus).Font.Bold = True
End With
With sNewSheet
.Activate
If sNewSheet.Cells(iLen,cTestName_Sum).value = sTestName Then
.Cells(iLen,cSum_Sum).value = .Cells(iLen,cSum_Sum).value+
.Cells(iLen,cPass_Sum).value = .Cells(iLen,cPass_Sum).value+
Else
.Cells(iLen+,cTestName_Sum).value = sTestName
.Cells(iLen+,cSum_Sum).value =
.Cells(iLen+,cTime_Sum).value =now
.Cells(iLen+,cPass_Sum).value =
.Cells(iLen+,cFail_Sum).value =
.Cells(iLen+,cStatus_Sum).value = "Passed"
.Cells(iLen+,cStatus_Sum).Font.Color = vbGreen
.Cells(iLen+,cStatus_Sum).Font.Bold = True
End If
End With
Else
With sNewSheetFail
.Activate
.Cells(iLenFail+,cTestName).value = sTestName
.Cells(iLenFail+,cStep).value = sStep
.Cells(iLenFail+,cDetail).value = sDetail
.Cells(iLenFail+,cTime).value = now
.Cells(iLenFail+,cStatus).value = "Failed"
.Cells(iLenFail+,cStatus).Font.Color = vbRed
.Cells(iLenFail+,cStatus).Font.Bold = True
' oExcel.Application.Visible = False
.Cells(iLenFail+,cPicName).value = CapturePic(sFolderPath,sStep)
Call .Hyperlinks.Add(.Cells(iLenFail+,cPicName),sFolderPath&"/"&.Cells(iLenFail+,cPicName).value,"","Capture screen when failed")
End With
With sNewSheet
.Activate
If sNewSheet.Cells(iLen,cTestName_Sum).value = sTestName Then
.Cells(iLen,cSum_Sum).value = .Cells(iLen,cSum_Sum).value+
.Cells(iLen,cFail_Sum).value = .Cells(iLen,cFail_Sum).value+
.Cells(iLen,cStatus_Sum).value = "Failed"
.Cells(iLen,cStatus_Sum).Font.Color = vbRed
.Cells(iLen,cStatus_Sum).Font.Bold = True
Else
.Cells(iLen+,cTestName_Sum).value = sTestName
.Cells(iLen+,cSum_Sum).value =
.Cells(iLen+,cTime_Sum).value =now
.Cells(iLen+,cPass_Sum).value =
.Cells(iLen+,cFail_Sum).value =
.Cells(iLen+,cStatus_Sum).value = "Failed"
.Cells(iLen+,cStatus_Sum).Font.Color = vbRed
.Cells(iLen+,cStatus_Sum).Font.Bold = True
end if
End With
End If sNewBook.Save
oExcel.Application.Quit
Set sNewBook = Nothing
Set oExcel = Nothing End Function Public Function CapturePic(pathway,sStep)
Dim datestamp
Dim picName
Dim filename
Dim ofile,ran
datestamp = Hour(Now)&Minute(Now)&Second(Now)
Set ofile = CreateObject("Scripting.FileSystemObject")
Randomize
ran = Int(Rnd()*)
filename = Environment("TestName")&"_"&sStep&datestamp&ran
filename = Replace(filename,"|","")
filename = Replace(filename,">","")
filename = Replace(filename,"<","")
filename = Replace(filename,"?","")
filename = Replace(filename,"*","")
filename = Replace(filename,"\","")
filename = Replace(filename,"/","")
filename = Replace(filename,":","")
If ofile.FileExists(pathway+"/"+""&filename&".png") Then
filename=filename&""
End If
filename = filename&".png"
picName = filename
filename = pathway + "/" + ""&filename
Desktop.CaptureBitmap filename
CapturePic = picName
End Function

With Object

With Browser("DUI 02").Page("DUI 02").SlvWindow("Shell").SlvDialog("FileFlightFormView")

    iTimer=Timer
Do
Loop until .Exist or (Timer-iTimer)>
If .Exist Then
Reporter.ReportEvent micPass,"The system displays the fill form","dialog box displays successfully"
.SlvButton("Select").Click
end if

ArrayList Sort

Option Explicit
Dim mArray()
ReDim mArray() mArray()="0AABB"
mArray()="11abc"
mArray()="2ec11"
mArray()="aAACC"
mArray()="aAACC"
mArray()="aaaxx"
mArray()="AAAyb"
mArray()="AAdew"
mArray()="aaxew"
mArray()="ddddd"
mArray()="zzaAA1" ' Call function to check the order of the array
Call IsSorted(mArray) Function IsSorted(arraylist)
Dim leng,i 'get the length of the array
leng=Ubound(arraylist)+ 'check whether arraylist length is more than two
If leng < 2Then
msgbox("No enough data in this arraylist.")
End If
For i= to Ubound(arraylist)- 'The StrComp function compares two strings and returns a value that represents the result of the comparison.
'0 = vbBinaryCompare - Perform a binary comparison,1 = vbTextCompare - Perform a textual comparison If strcomp(arraylist(i),arraylist(i+),) = Then
' call Report (micFail, "Check the Sort of the array", "The array sort is not correct between "&arraylist(i)& " and "&arraylist(i+1)& " .")
msgbox "The array sort is not correct between "&arraylist(i)& " and "&arraylist(i+)& " ."
Exit function
End If
Next
end function

Send Key

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad"
WScript.Sleep 500
WshShell.AppActivate "Notepad"
Wshshell.SendKeys "%(123)"

Set shell=Createobject("WScript.Shell")
shell.SendKeys "{END}"

Run Action

RunAction "login [login_search]", oneIteration, , , url
RunAction "Search_Flight [login_search]", oneIteration, flight, "", ""

正则表达式

Dim itype
itype="^(3[0-1]|2[0-9]|1[0-9]|0[1-9])-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{2}/[0-6]\d:[0-6]\d$"
msgbox isPatternMatch(itype,"30-Jan-04/00:00") Public Function isPatternMatch(patternType,inputData)
Dim myReg
If trim(inputData) = "" Then
isPatternMatch = true
Exit function
End If
Set myReg= New RegExp
'Set pattern
myReg.Pattern =patternType
' Set case insensitivity
myReg.IgnoreCase = True
' Set global applicability
myReg.Global = True
' Execute search
isPatternMatch=myReg.test(inputData)
End Function

PageScrollDown

browser("title:="&PageTitleURL).Page("title:="&PageTitleURL).object.body.doScroll("scrollbarPageDown")

Import from Excel

'datatable.ImportSheet "C:\FP\datatable\Input _ Initial018.xls" ,1 ,"Global"
rowcount = DataTable.GetSheet("Global").GetRowCount
'msgbox "step1:the number need to check is: "&rowcount

Get Value from Table

'Get value
value=datatable.getsheet("sheet").getparameter("Para").valueByrow()
value=slvTable("table).getcelldata(1,"para")
'Get Rowcount
count=datatable.getsheet("sheet").getrowcount
count=slvTable("table").rowcount

Connect Oracle

Dim Cnn
Set Cnn = CreateObject("ADODB.Connection")
Cnn.ConnectionString ="Provider=OraOLEDB.Oracle.1;Password=skyobj;Persist Security Info=True;User ID=skyobj;Data Source=TAEDFLP.airservices.eds.com"
Cnn.Open
If (Cnn.State = )Then
MsgBox "failed"
' Call Report(micFail, "Database connect testing", "Failed!")
'Reporter.ReportEvent micFail, "Database connect testing", "连接数据库失败"
Else
MsgBox "success"
' Call Report(micPass, "Database connect testing", "Success!")
'Reporter.ReportEvent micPass, "Database connect testing", "连接数据库成功"
end if

CheckDate

Public Function currentdate()
a = day(date)
b = MonthName(month(date),true)
c = right(Year(date),)
if cint(a) < then a = ""&a
currentdate = a&"-"&b&"-"&c
End Function MsgBox currentdate()

Click Save button (FP,silverligh)

Set var_Object = Browser("FPC").Page("FPC").Object.body
var_Object.doScroll("pageDown")
x = Browser("FPC").Page("FPC").SlvWindow("Shell").SlvButton("btnSave").GetROProperty("x") +
y = Browser("FPC").Page("FPC").SlvWindow("Shell").SlvButton("btnSave").GetROProperty("y") +
Browser("FPC").Page("FPC").WinObject("MicrosoftSilverlight").Click x,y

我常用的VBS方法(QTP)的更多相关文章

  1. vbs和qtp一些脚本

    ********************************************************************************************** 用vbs调 ...

  2. Node.js process 模块常用属性和方法

    Node.js是常用的Javascript运行环境,本文和大家发分享的主要是Node.js中process 模块的常用属性和方法,希望通过本文的分享,对大家学习Node.js http://www.m ...

  3. Java常用的输入输出方法

    对于经常上机刷题的来说,首先得解决输入输出方法,Java的输入输出流在Java学习过程的后面部分才会接触,但是我们可以掌握一些简单的,常用的输入输出方法 首先输出 大家最熟悉的莫过于输出方法,直接用S ...

  4. JavaScript常用对象的方法和属性

    ---恢复内容开始--- 本文将简单介绍JavaScript中一些常用对象的属性和方法,以及几个有用的系统函数. 一.串方法 JavaScript有强大的串处理功能,有了这些串方法,才能编写出丰富多彩 ...

  5. Document-对象属性和常用的对象方法

    Document-对象属性和常用的对象方法 对象属性 document.title                           //设置文档标题等价于HTML的title标签 document ...

  6. 干货:结合Scikit-learn介绍几种常用的特征选择方法

    原文  http://dataunion.org/14072.html 主题 特征选择 scikit-learn 作者: Edwin Jarvis 特征选择(排序)对于数据科学家.机器学习从业者来说非 ...

  7. ios基础篇(四)——UILabel的常用属性及方法

    UILabel的常用属性及方法:1.text //设置和读取文本内容,默认为nil label.text = @”文本信息”; //设置内容 NSLog(@”%@”, label.text); //读 ...

  8. (转)在网页中JS函数自动执行常用三种方法

    原文:http://blog.sina.com.cn/s/blog_6f6b4c3c0100nxx8.html 在网页中JS函数自动执行常用三种方法 在网页中JS函数自动执行常用三种方法 在HTML中 ...

  9. 结合Scikit-learn介绍几种常用的特征选择方法

    特征选择(排序)对于数据科学家.机器学习从业者来说非常重要.好的特征选择能够提升模型的性能,更能帮助我们理解数据的特点.底层结构,这对进一步改善模型.算法都有着重要作用. 特征选择主要有两个功能: 减 ...

随机推荐

  1. 【解惑】让人头疼的"相等"关系

    Java中判断相等关系一般有两种手段:(1) “==”关系操作符  (2) equals()方法. 显然,基本数据类型变量之间只能用"==".而对象之间两种手段都是合法的.但是有很 ...

  2. PenetrationTest

    一.渗透测试是什么 渗透测试(PenetrationTest)是对安全情况最客观.最直接的评估方式,主要是模拟黑客的攻击方法对系统和网络进行非破坏性质的攻击性测试,在保证整个渗透测试过程都在可以控制和 ...

  3. Data Base MongoVue 破解治标不治本

    MongoVue  破解治标不治本 ---------解决燃眉之急 注册表中查找B1159E65-821C3-21C5-CE21-34A484D54444中的子项4FF78130 ,删除其下的三个子项 ...

  4. spring mvc 中文乱码 post与get的方法解决

    spring mvc表单提交中文参数乱码问题 今天测试spring mvc  ,中文乱码,在web.xml中加上 <filter> <filter-name>encodingF ...

  5. ACdream 1735 输油管道 (排序)

    http://acdream.info/problem?pid=1735 官方题解:http://acdream.info/topic?tid=4246 因为主干线是平行于x轴的直线,那么跟x坐标其实 ...

  6. .NET Framework 4.5 五个很棒的特性

    转自http://news.cnblogs.com/n/192958/ 英文原文:Five Great .NET Framework 4.5 Features 简介 自 .NET 4.5 发布已经过了 ...

  7. 函数lock_rec_add_to_queue

    在原来的type_mode基础上,加上LOCK_REC /*********************************************************************// ...

  8. PHP项目中composer和Git的组合使用

    highlight: 在国内由于众所周知的原因,composer的package可能无法访问,解决办法是使用中国的全镜像: composer config -g repositories.packag ...

  9. UVa 10161 Ant on a Chessboard

    一道数学水题,找找规律. 首先要判断给的数在第几层,比如说在第n层.然后判断(n * n - n + 1)(其坐标也就是(n,n)) 之间的关系. 还要注意n的奇偶.  Problem A.Ant o ...

  10. 51nod1158 全是1的最大子矩阵

    跟最大子矩阵差不多O(n3)扫一下.有更优写法?挖坑! #include<cstdio> #include<cstring> #include<cctype> #i ...