这些是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字符串格式化String.format()的使用

    JAVA字符串格式化-String.format()的使用常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprin ...

  2. CentOS 7快速搭建Nodejs开发环境

    Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascript的速度非常快,性能非常好.学习Nodejs首先需要会安装环境.这里我介绍如 ...

  3. iOS:CALayer的隐式动画的详解

    CALayer的隐式动画属性: •每一个UIView内部都默认关联着一个CALayer,称这个Layer为Root Layer.所有的非Root Layer都存在着隐式动画,隐式动画的默认时长为1/4 ...

  4. Linux命令行通配符

    如果我们想对一类文件批量操作,例如批量查看硬盘文件属性,那么正常命令是如下所示: [root@localhost Desktop]# ls /dev/sda1 [root@localhost Desk ...

  5. JVM的stack和heap,JVM内存模型,垃圾回收策略,分代收集,增量收集

    (转自:http://my.oschina.net/u/436879/blog/85478) 在JVM中,内存分为两个部分,Stack(栈)和Heap(堆),这里,我们从JVM的内存管理原理的角度来认 ...

  6. Back to Back Order Process

    Steps involved involved in back to back order process in oracle apps 1. Enter Sales Order 2. Book Sa ...

  7. android 内存溢出问题分析

      最近的项目中,内存一直再增长,但是不知道是什么问题,导致内存溢出,在网上看到了这么一篇关于内存分析与管理的文章,解决了部分问题,感觉这篇文 章还不错,就转帖到我的blog上了,希望对大家有所帮助. ...

  8. UVa401 回文词

    Palindromes A regular palindrome is a string of numbers or letters that is the same forward as backw ...

  9. A1486. 树(王康宁)

    题目:http://www.tsinsen.com/A1486 题解: 其实看到和路径有关的就应该想到点分治. 我们找出重心之后遍历每一棵子树得到它的 { x=经过特殊点的个数,y=到rt的异或和} ...

  10. 每个极客都应该知道的Linux技巧

    每个极客都应该知道的Linux技巧 2014/03/07 | 分类: IT技术 | 0 条评论 | 标签: LINUX 分享到:18 本文由 伯乐在线 - 欣仔 翻译自 TuxRadar Linux. ...