这些是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. Struts2笔记——自定义拦截器

    需要两个步骤,自定义类实现拦截器接口和在struts.xml注册拦截器 =============================== 1.自定义类实现com.opensymphony.xwork2. ...

  2. 如何在Java客户端调用RESTful服务

    在这个例子中,我们将看到如何使用java.net包实用工具,创建一个访问REST服务RESTful的客户端.当然这不是创建一个RESTful客户端最简单的方法,因为你必须自己读取服务器端的响应,以及J ...

  3. 282. Expression Add Operators

    题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...

  4. POJ1019——Number Sequence(大数处理)

    Number Sequence DescriptionA single positive integer i is given. Write a program to find the digit l ...

  5. Android setStartOffset方法:设置启动时间

    [功能说明]该方法用于设置一个动画执行的启动时间,单位为毫秒.系统默认当执行start方法后立刻执行动画,当使用该方法设置后,将延迟一定的时间再启动动画. [基本语法]public void setS ...

  6. Eclipse中使用正则表达式搜索替换

    Eclipse中使用正则表达式搜索替换 分类:software | 标签: 正则表达  替换  eclipse  2011-11-29 11:28 阅读(1930)评论(0)编辑删除 最近在eclip ...

  7. 使用struts taglib导致java.lang.NullPointerException: Module 'null' not found.

    比如说,只要jsp的代码里有<html:....>或者<bean:...>之类的struts标签就会在访问该jsp页面的时候报这个错 最后参考这篇文章,发现原来是因为web.x ...

  8. URL重写的优缺点分析

    如何增强您网站地址的可读性.如何让搜索引擎快速收录到您的站点,这就需要优化您的Url,即Url的重写技术,大家熟悉的可能有很多服务器都提供Url重写技术,以前我们用的最多的就是Apache,Jboss ...

  9. c 建立工程 常见错误及心得总结

    1. 调用函数里面的 调用子函数,要是出现已定义错误,则要看看,是不是主函数的 头文件写成.c啦,因为在.h里面也定义可一次,要是写.c出现两次 错误提示:UrlCheck.obj : error L ...

  10. oracle数据库导入导出命令!

    Oracle数据导入导出imp/exp 功能:Oracle数据导入导出imp/exp就相当与oracle数据还原与备份. 大多情况都可以用Oracle数据导入导出完成数据的备份和还原(不会造成数据的丢 ...