我常用的VBS方法(QTP)
这些是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)的更多相关文章
- vbs和qtp一些脚本
********************************************************************************************** 用vbs调 ...
- Node.js process 模块常用属性和方法
Node.js是常用的Javascript运行环境,本文和大家发分享的主要是Node.js中process 模块的常用属性和方法,希望通过本文的分享,对大家学习Node.js http://www.m ...
- Java常用的输入输出方法
对于经常上机刷题的来说,首先得解决输入输出方法,Java的输入输出流在Java学习过程的后面部分才会接触,但是我们可以掌握一些简单的,常用的输入输出方法 首先输出 大家最熟悉的莫过于输出方法,直接用S ...
- JavaScript常用对象的方法和属性
---恢复内容开始--- 本文将简单介绍JavaScript中一些常用对象的属性和方法,以及几个有用的系统函数. 一.串方法 JavaScript有强大的串处理功能,有了这些串方法,才能编写出丰富多彩 ...
- Document-对象属性和常用的对象方法
Document-对象属性和常用的对象方法 对象属性 document.title //设置文档标题等价于HTML的title标签 document ...
- 干货:结合Scikit-learn介绍几种常用的特征选择方法
原文 http://dataunion.org/14072.html 主题 特征选择 scikit-learn 作者: Edwin Jarvis 特征选择(排序)对于数据科学家.机器学习从业者来说非 ...
- ios基础篇(四)——UILabel的常用属性及方法
UILabel的常用属性及方法:1.text //设置和读取文本内容,默认为nil label.text = @”文本信息”; //设置内容 NSLog(@”%@”, label.text); //读 ...
- (转)在网页中JS函数自动执行常用三种方法
原文:http://blog.sina.com.cn/s/blog_6f6b4c3c0100nxx8.html 在网页中JS函数自动执行常用三种方法 在网页中JS函数自动执行常用三种方法 在HTML中 ...
- 结合Scikit-learn介绍几种常用的特征选择方法
特征选择(排序)对于数据科学家.机器学习从业者来说非常重要.好的特征选择能够提升模型的性能,更能帮助我们理解数据的特点.底层结构,这对进一步改善模型.算法都有着重要作用. 特征选择主要有两个功能: 减 ...
随机推荐
- html5 语音搜索
开始以为是接口什么的,原来这就是语言搜索. 只需要在input加上x-webkit-speech <input type="text" class="text&qu ...
- ThreadLocal实现session中用户信息 的线程间共享(精)
ThreadLocal并不难理解,我总结的最简单的理解就是: ThreadLocal像其它变量一样(局部.全局.静态)也是一种变量类型,只是他是线程变量,更直白的说他是一种变量作用域,即他的作用域是当 ...
- git提交代码步骤
01:首先git status一下查看当前目录下修改的文件,当然编译生成的文件也在其中,我们只看自己修改的: 02:git add ****** //(文件名) 将上述查找到自己修改的文件添加到git ...
- 51nod 1050 循环数组最大子段和 (dp)
http://www.51nod.com/onlineJudge/questionCode.html#problemId=1050¬iceId=13385 参考:http://blog. ...
- 第八篇 EBS实现企业日常业务运管模型的解决方案设计思路
常业务运管模型企业有大有小,各行各业,千差万别,但,其日常业务运管也有相通之处,以典型的制造企业为例,其日常业务运管模型如下图所示: (1)企业日常业务运管模型在市场经济条件下,一个生产型 ...
- config windows virtual machine on mac
1.download virtualbox and related extension pack from http://www.oracle.com/technetwork/server-stor ...
- SQL多表联合分页.....
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go /* 支持多表查询分页存储过程(事理改进)2012.3 --多表联查1 declare @Count int ...
- CodeForces Round #298 Div.2
A. Exam 果然,并没有3分钟秒掉水题的能力,=_=|| n <= 4的时候特判.n >= 5的时候将奇数和偶数分开输出即可保证相邻的两数不处在相邻的位置. #include < ...
- UVa 1586 Molar mass
题意:给出物质的分子式,计算它的相对原子质量 因为原子的个数是在2到99之间的,所以找出一个是字母之后,再判断一下它的后两位就可以找出这种原子的个数了 #include<iostream> ...
- POJ 3308 Paratroopers (对数转换+最小点权覆盖)
题意 敌人侵略r*c的地图.为了消灭敌人,可以在某一行或者某一列安置超级大炮.每一个大炮可以瞬间消灭这一行(或者列)的敌人.安装消灭第i行的大炮消费是ri.安装消灭第j行的大炮消费是ci现在有n个敌人 ...