数据驱动在QTP中的运用(二) - 自定义EXCEL文件
2007-09-14 23:24
337 查看
本节共有两个实例,第一个是把测试用例的数据写到EXCEL文件中,第二个是通过读取EXCEL文件中的数据,并把执行结果写入到EXCEL文件中。
1.对EXCEL文件进行写操作
Option Explicit
Dim fso, ddFilePath, i
Dim ExcelBook, ExcelSheet
ddFilePath = Environment.Value("TestDir") & "/ddFile.xls"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(ddFilePath) Then
fso.DeleteFile(ddFilePath)
End If
wait 3
Set ExcelBook = CreateObject("Excel.Application")
Set ExcelSheet = CreateObject("Excel.Sheet")
ExcelSheet.Application.visible = False
ExcelSheet.ActiveSheet.Cells(1,1).Value = "Agent Name"
ExcelSheet.ActiveSheet.Cells(1,2).Value = "Password"
ExcelSheet.ActiveSheet.Cells(1,3).Value = "Expire Value"
ExcelSheet.ActiveSheet.Cells(1,4).Value = "Fact Value"
ExcelSheet.ActiveSheet.Cells(1,5).Value = "Execute Result"
ExcelSheet.ActiveSheet.Cells(2,1).Value = "ad"
ExcelSheet.ActiveSheet.Cells(2,2).Value = "Mercury"
ExcelSheet.ActiveSheet.Cells(2,3).Value = "Agent name must be at least 4 characters long."
ExcelSheet.ActiveSheet.Cells(3,1).Value = "Admin"
ExcelSheet.ActiveSheet.Cells(3,2).Value = "Merc"
ExcelSheet.ActiveSheet.Cells(3,3).Value = "Incorrect password. Please try again"
ExcelSheet.ActiveSheet.Cells(4,1).Value = "Admin"
ExcelSheet.ActiveSheet.Cells(4,2).Value = "Mercury"
ExcelSheet.ActiveSheet.Cells(4,3).Value = "Flight Reservation"
ExcelSheet.SaveAs ddFilePath
ExcelBook.Quit
Set ExcelBook = Nothing
2.对EXCEL文件进行读写操作
Option Explicit
Dim fso, filePath, i
Dim ExcelBook, ExcelSheet, myExcelBook, myExcelSheet
filePath = Environment.Value("TestDir") & "/ddFile.xls"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ExcelBook = CreateObject("Excel.Application")
Set ExcelSheet = CreateObject("Excel.Sheet")
Set myExcelBook = ExcelBook.WorkBooks.Open(filePath)
Set myExcelSheet = myExcelBook.WorkSheets("Sheet1")
For i = 2 To 4
SystemUtil.CloseProcessByName "Flight4a.exe"
SystemUtil.Run Environment.Value("ProductDir") & "/samples/flight/app/flight4a.exe"
Dialog("Login").WinEdit("Agent Name:").Set myExcelSheet.Cells(i,1)
Dialog("Login").WinEdit("Password:").Set myExcelSheet.Cells(i,2)
Dialog("Login").WinButton("OK").Click
If Dialog("Login").Dialog("Flight Reservations").Exist Then
myExcelSheet.Cells(i,4).Value = Dialog("Login").Dialog("Flight Reservations").Static("errInfo").GetROProperty("text")
If Dialog("Login").Dialog("Flight Reservations").Static("errInfo").GetROProperty("text") = myExcelSheet.Cells(i,3) Then
myExcelSheet.Cells(i,5).Font.Color = vbBlue
myExcelSheet.Cells(i,5).Value = "测试成功"
Else
myExcelSheet.Cells(i,5).Font.Color = vbRed
myExcelSheet.Cells(i,5).Value = "测试失败"
End If
Dialog("Login").Dialog("Flight Reservations").WinButton("确定").Click
Dialog("Login").WinButton("Cancel").Click
Elseif Window("Flight Reservation").Exist Then
myExcelSheet.Cells(i,4).Value = Window("Flight Reservation").GetROProperty("text")
myExcelSheet.Cells(i,5).Font.Color = vbBlue
myExcelSheet.Cells(i,5).Value = "测试成功"
Window("Flight Reservation").Close
Else
logFile.WriteLine "没有窗口弹出,测试失败!"
ExitAction
End If
Next
myExcelBook.Save
ExcelBook.Quit
Set ExcelBook = Nothing
1.对EXCEL文件进行写操作
Option Explicit
Dim fso, ddFilePath, i
Dim ExcelBook, ExcelSheet
ddFilePath = Environment.Value("TestDir") & "/ddFile.xls"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(ddFilePath) Then
fso.DeleteFile(ddFilePath)
End If
wait 3
Set ExcelBook = CreateObject("Excel.Application")
Set ExcelSheet = CreateObject("Excel.Sheet")
ExcelSheet.Application.visible = False
ExcelSheet.ActiveSheet.Cells(1,1).Value = "Agent Name"
ExcelSheet.ActiveSheet.Cells(1,2).Value = "Password"
ExcelSheet.ActiveSheet.Cells(1,3).Value = "Expire Value"
ExcelSheet.ActiveSheet.Cells(1,4).Value = "Fact Value"
ExcelSheet.ActiveSheet.Cells(1,5).Value = "Execute Result"
ExcelSheet.ActiveSheet.Cells(2,1).Value = "ad"
ExcelSheet.ActiveSheet.Cells(2,2).Value = "Mercury"
ExcelSheet.ActiveSheet.Cells(2,3).Value = "Agent name must be at least 4 characters long."
ExcelSheet.ActiveSheet.Cells(3,1).Value = "Admin"
ExcelSheet.ActiveSheet.Cells(3,2).Value = "Merc"
ExcelSheet.ActiveSheet.Cells(3,3).Value = "Incorrect password. Please try again"
ExcelSheet.ActiveSheet.Cells(4,1).Value = "Admin"
ExcelSheet.ActiveSheet.Cells(4,2).Value = "Mercury"
ExcelSheet.ActiveSheet.Cells(4,3).Value = "Flight Reservation"
ExcelSheet.SaveAs ddFilePath
ExcelBook.Quit
Set ExcelBook = Nothing
2.对EXCEL文件进行读写操作
Option Explicit
Dim fso, filePath, i
Dim ExcelBook, ExcelSheet, myExcelBook, myExcelSheet
filePath = Environment.Value("TestDir") & "/ddFile.xls"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ExcelBook = CreateObject("Excel.Application")
Set ExcelSheet = CreateObject("Excel.Sheet")
Set myExcelBook = ExcelBook.WorkBooks.Open(filePath)
Set myExcelSheet = myExcelBook.WorkSheets("Sheet1")
For i = 2 To 4
SystemUtil.CloseProcessByName "Flight4a.exe"
SystemUtil.Run Environment.Value("ProductDir") & "/samples/flight/app/flight4a.exe"
Dialog("Login").WinEdit("Agent Name:").Set myExcelSheet.Cells(i,1)
Dialog("Login").WinEdit("Password:").Set myExcelSheet.Cells(i,2)
Dialog("Login").WinButton("OK").Click
If Dialog("Login").Dialog("Flight Reservations").Exist Then
myExcelSheet.Cells(i,4).Value = Dialog("Login").Dialog("Flight Reservations").Static("errInfo").GetROProperty("text")
If Dialog("Login").Dialog("Flight Reservations").Static("errInfo").GetROProperty("text") = myExcelSheet.Cells(i,3) Then
myExcelSheet.Cells(i,5).Font.Color = vbBlue
myExcelSheet.Cells(i,5).Value = "测试成功"
Else
myExcelSheet.Cells(i,5).Font.Color = vbRed
myExcelSheet.Cells(i,5).Value = "测试失败"
End If
Dialog("Login").Dialog("Flight Reservations").WinButton("确定").Click
Dialog("Login").WinButton("Cancel").Click
Elseif Window("Flight Reservation").Exist Then
myExcelSheet.Cells(i,4).Value = Window("Flight Reservation").GetROProperty("text")
myExcelSheet.Cells(i,5).Font.Color = vbBlue
myExcelSheet.Cells(i,5).Value = "测试成功"
Window("Flight Reservation").Close
Else
logFile.WriteLine "没有窗口弹出,测试失败!"
ExitAction
End If
Next
myExcelBook.Save
ExcelBook.Quit
Set ExcelBook = Nothing
相关文章推荐
- 数据驱动在QTP中的运用(一) - 自定义TXT文件
- 如何处理ODBC中EXCEL驱动读取EXCEL文件中字段长度大于255字符时出现的"数据截断"问题.
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- 如何处理Oledb中EXCEL驱动读取EXCEL文件中字段长度大于255字符时出现的"数据截断"问题.
- QTP的那些事--在QC中importsheet方式导入excel数据驱动
- 数据驱动在QTP的运用
- 数据驱动在QTP中的运用(三) - 使用ADODB导入EXCEL数据文件
- 数据驱动测试三:使用TestNG、Apache POI和Excel文件进行数据驱动
- 处理EXCEL驱动读取EXCEL文件中字段长度大于255字符时出现的数据截断
- QTP数据驱动之读取Excel数据
- 数据驱动在QTP中的运用(四) - 使用SQL数据库
- Selenium(Python) ddt读取Excel文件数据驱动
- Excel从另一个Excel文件中用户自定义区域(UsedRange)拷贝数据和行高、列宽
- 数据驱动在QTP中的运用
- 数据驱动在QTP中的运用
- Excel从另一个Excel文件中用户自定义区域(UsedRange)拷贝数据和行高、列宽
- Appium自动化-----数据驱动读取外部Excel文件
- Codeigniter+PHPExcel中导出数据到Excel文件
- 将Control List中的数据导入类Excel的表格中,(使用了.csv文件)
- 读取Excel数据和写入txt文件以及读取配置文件工具类