VB code example

上一篇 / 下一篇  2012-09-11 10:08:01 / 个人分类:QTP

1. 随机选择 航班起航地点

ItemCount = Window("Flight Reservation").Dialog("Flights Table").WinList("From").GetItemsCount

SelectItem = RandomNumber(0, ItemCount)

Window("Flight Reservation").Dialog("Flights Table").WinList("From").Select SelectItem

2. Highlight 功能 

Browser("百度一下,你就知道").Page("百度一下,你就知道").WebEdit("wd").Highlight

3.添加对象库

RepositoriesCollection.Add "D\QTP自动化测试技术领航\link.tsr"


example:

Public Function AddObjectRepository(objectrepositoryname) 

     Dim Pos

     '判断:如果存在".tsr"后缀名则直接使用文件路径,如果不存在则添加后缀名

     If instr(objectrepositoryname,".tsr") > 0 Then

     RepPath = objectrepositoryname

     else

     RepPath = objectrepositoryname & ".tsr"

     End If

 

     '初始化:如果存在残留的副对象库则删除

     RepositoriesCollection.RemoveAll()

     '添加副对象库

     RepositoriesCollection.Add(RepPath) 

 

     '添加后的验证:查找副对象库并将值返回给变量,如果存在返回1

     Pos = RepositoriesCollection.Find(RepPath) 

     MsgBox Pos

     '返回的值如果不等于1代表不存在则报错

     If Pos <> 1 Then

     MsgBox "找不到副对象库"

     End If

 

End Function

'调用该函数

Call AddObjectRepository("D\QTP自动化测试技术领航\link.tsr")


4. 描述性编程 Descriptive programming

一个首页就有接近?20?个相同类别的控件(Link),虽然不多,但是一个个添加也够烦锁的,既然它们是完全相同类型的控件,那么使用描述性编程是一个上佳之选,下面来看这段脚本,看看是如何实现的:

Set baidu = Browser("micClass:=Browser").Page("micClass:=Page")

Print Baidu.Link("name:=新闻").Exist

With baidu

        Print .Link("name:=贴吧").Exist 

        Print .Link("name:=知道").Exist 

        Print .Link("name:=MP3").Exist 

        Print .Link("name:=图片").Exist 

        Print .Link("name:=把百度设为主页").Exist 

        Print .Link("name:=搜索风云榜").Exist 

        Print .Link("name:=About Baidu").Exist 

End With

Set baidu = Nothing

5. 从特定的文件中查询并返回

Option Explicit 

On Error Resume Next 

Dim arrTxtArray() 

Dim myFile 

Dim SearchString 

Dim objTextFile 

Dim strNextLine

Dim intSize

Dim objFSO

Dim i

intSize = 0

myFile = "d:\search.txt"

SearchString = "error"

Const ForReading = 1

Set bjFSO = CreateObject("Scripting.FileSystemObject")

Set bjTextFile = objFSO.OpenTextFile(myFile, ForReading)

Do until objTextFile.AtEndOfStream

strNextLine = objTextFile.ReadLine

if InStr (strNextLine, SearchString)then

ReDim Preserve arrTxtArray(intSize)

arrTxtArray(intSize) = strNextLine

intSize = intSize + 1

End If

Loop

objTextFile.close

For i = LBound(arrTxtArray) To UBound(arrTxtArray)

WScript.Echo arrTxtArray(i)

Next

WScript.Echo("all done")

Note:

InStr([start,]string1,string2[,compare]) 返回起始位置

'ReDim Preserve actually performs two tasks. The first is to resize the array, and the second is to make sure you don’t lose any data when the array is resized.


TAG: QTP qtp

 

评分:0

我来说两句

日历

« 2024-04-21  
 123456
78910111213
14151617181920
21222324252627
282930    

我的存档

数据统计

  • 访问量: 12067
  • 日志数: 13
  • 建立时间: 2012-09-11
  • 更新时间: 2012-09-26

RSS订阅

Open Toolbar