`
tengzejun508
  • 浏览: 26226 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

QTP对EXCEL操作函数

    博客分类:
  • QTP
阅读更多
' *********************************************************************************************
'''''''''参数: ReportExcelFile 报告输出的路径,每次QTP运行后的测试结果路径
''''''调用方法:
''                             ReportExcelFileSavePath
' *********************************************************************************************
Public ReportExcelFileSavePath
ReportExcelFileSavePath = Environment ("TestDir")& "\" & "测试结果-" & Date & "-"& Hour(Now) & Minute(Now)& Second(Now) & ".xlsx"
' *********************************************************************************************

' ******************************************************************************检查点结果输出***************************************************************************************************
'''以下这个段落是对检查点进行判断,正确输出Pass,错误输出红色的Fail,
'''调用方法:
                       ''CheckPointResultOutputToExcel 检查点名称,CheckPointResult
''注意:1. 在前面的CheckPoint前面插入:CheckPointResult=
''2.检查点的最后Check CheckPoint("打开首页") 中改为:Check (CheckPoint("打开首页")) 
' *************************************************************************' *************************************************************************
Function CheckPointResultOutputToExcel (CheckPointName,CheckPointResult)
'On Error Resume Next
Set ExcelSheet1 = ExcelApp.Worksheets.Item("测试结果")
Row= ExcelSheet1.Cells(10,5)   ''使用Excel文档数据来记录行数
Column=2
RunTestCaseAmount=ExcelSheet1.Cells(7,3) 
RunTestCaseAmount=RunTestCaseAmount+1
ExcelSheet1.Cells(7,3) = RunTestCaseAmount
    If CheckPointResult = "True" Then
        ExcelSheet1.Cells(Row, Column) =  RunTestCaseAmount
''''设置用例编号居中
ExcelSheet1.Range("B"&Row&":B"&Row).HorizontalAlignment = 3 ''4’右边对齐  3 '居中  2 '左边对齐
        column=column+1
        ExcelSheet1.Cells( Row, Column) =  CheckPointName
        column=column+1
        ExcelSheet1.Cells( Row, Column) = "Pass"
''''设置测试结果居中
ExcelSheet1.Range("D"&Row&":D"&Row).HorizontalAlignment = 3 ''4’右边对齐  3 '居中  2 '左边对齐
        ''设置Pass单元格颜色为绿色
        ExcelSheet1.Cells(Row, Column).Font.Color = vbGreen
PassAmountOfTestCase = ExcelSheet1.Cells( 8, 3)
        ExcelSheet1.Cells( 8, 3) = PassAmountOfTestCase+1    
    Elseif CheckPointResult = "False" then
        ExcelSheet1.Cells(Row, Column) =  RunTestCaseAmount
''''设置用例编号居中
ExcelSheet1.Range("B"&Row&":B"&Row).HorizontalAlignment = 3 ''4’右边对齐  3 '居中  2 '左边对齐
        column=column+1
        ExcelSheet1.Cells( Row, Column) =  CheckPointName
        column=column+1
        ExcelSheet1.Cells( Row, Column) = "Fail"
''''设置测试结果居中
ExcelSheet1.Range("D"&Row&":D"&Row).HorizontalAlignment = 3 ''4’右边对齐  3 '居中  2 '左边对齐
        ''设置Fail单元格颜色为红色
        ExcelSheet1.Cells(Row, Column).Font.Color = vbRed
FailAmountOfTestCase = ExcelSheet1.Cells( 9, 3)
        ExcelSheet1.Cells( 9, 3) = FailAmountOfTestCase+1
Else
        ExcelSheet1.Cells(Row, Column) =  RunTestCaseAmount
''''设置用例编号居中
ExcelSheet1.Range("B"&Row&":B"&Row).HorizontalAlignment = 3 ''4’右边对齐  3 '居中  2 '左边对齐
        column=column+1
        ExcelSheet1.Cells( Row, Column) =  CheckPointName
        column=column+1
        ExcelSheet1.Cells( Row, Column) = CheckPointResult
''''设置测试结果居中
ExcelSheet1.Range("D"&Row&":D"&Row).HorizontalAlignment = 3 ''4’右边对齐  3 '居中  2 '左边对齐
''        ''设置忽略单元格颜色为绿色
''        ExcelSheet1.Cells(Row, Column).Font.Color = vbGreen
NoActionAmountOfTestCase = ExcelSheet1.Cells( 10, 3)
        ExcelSheet1.Cells( 10, 3) = NoActionAmountOfTestCase+1
    End If

   '设置测试结果的边界
   ExcelSheet1.Range("B"&Row&":F"&Row).Borders(1).LineStyle = 1
   ExcelSheet1.Range("B"&Row&":F"&Row).Borders(2).LineStyle = 1
   ExcelSheet1.Range("B"&Row&":F"&Row).Borders(3).LineStyle = 1
   ExcelSheet1.Range("B"&Row&":F"&Row).Borders(4).LineStyle = 1

ExcelApp.ActiveWorkbook.Save
CheckPointResult="False"  ''防止没有进行CheckPointResult值的获取
'''让行数增加1,以备下一条测试用例获取当前行使用
Row=Row+1
    ExcelSheet1.Cells(10,5) = Row
  '''填入结束时间用来计算脚本执行时间,插入后会自动进行计算执行时间
ExcelSheet1.Cells(4,3) = Time
End Function
'''''****************************************************************************************************************************************************************
'''''****************************************建立一个测试结果Excel并打印出格式**********************************************************************
'''本函数用于为测试结果建立一个Excel并将该Excel设置相关格式
'''调用方法:(在创建QTP脚本时可以不需要直接调用本函数,可以调用 StartQTP()函数)
                       ''CreatAExcelAndPrintTheForm()
' *************************************************************************' ***************************************************************************************
Function CreatAExcelAndPrintTheForm()
On Error Resume Next

''''创建一个Excel文件
    Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = True
ExcelApp.Workbooks.Add
''''保存测试结果文件
    ExcelApp.ActiveWorkbook.SaveAs ReportExcelFileSavePath
''''选定一个Excel表
    Set ExcelSheet1 = ExcelApp.Sheets.Item(1)
    ExcelApp.Sheets.Item(1).Select
''''更改当前sheet的名字
    ExcelSheet1.Name = "测试结果"

''''设置列宽
ExcelSheet1.Columns("A:A").ColumnWidth = 4
ExcelSheet1.Columns("B:B").ColumnWidth = 11
ExcelSheet1.Columns("C:C").ColumnWidth = 30
    ExcelSheet1.Columns("D:D").ColumnWidth = 14
    ExcelSheet1.Columns("E:E").ColumnWidth = 30
    ExcelSheet1.Columns("F:F").ColumnWidth = 40

'''设置单元格为自动换行
ExcelApp.Range("A:G").WrapText = TRUE

   '设置显示区域的字体类型和大小
   ExcelSheet1.Range("A:F").Font.Name = "Arial"
   ExcelSheet1.Range("A:F").Font.Size = 10

   '为测试标题设置格式
   ExcelSheet1.Range("B1:E1").Interior.ColorIndex = 53  ''单元格底色
   ExcelSheet1.Range("B1:E1").Font.ColorIndex = 19
   ExcelSheet1.Range("B1:E1").Font.Bold = True
    ExcelSheet1.Range("B1:E1").Merge  ''''合并单元格

   '设置测试参数cell的边界
   ExcelSheet1.Range("B2:B10").HorizontalAlignment = 4 ''4’右边对齐  3 '居中  2 '左边对齐
   ExcelSheet1.Range("C2:C10").HorizontalAlignment = 2 ''4’右边对齐  3 '居中  2 '左边对齐
   ExcelSheet1.Range("D2:D10").HorizontalAlignment = 4 ''4’右边对齐  3 '居中  2 '左边对齐
   ExcelSheet1.Range("E2:E10").HorizontalAlignment = 2 ''4’右边对齐  3 '居中  2 '左边对齐
   ExcelSheet1.Range("B2:E10").Borders(1).LineStyle = 1
   ExcelSheet1.Range("B2:E10").Borders(2).LineStyle = 1
   ExcelSheet1.Range("B2:E10").Borders(3).LineStyle = 1
   ExcelSheet1.Range("B2:E10").Borders(4).LineStyle = 1

   '设置测试参数Cell的样式
   ExcelSheet1.Range("B2:E10").Interior.ColorIndex = 40     ''单元格底色
   ExcelSheet1.Range("B2:E10").Font.ColorIndex = 7           '‘字体颜色
   ExcelSheet1.Range("B2:B10").Font.ColorIndex = 12           '‘字体颜色
   ExcelSheet1.Range("D2:D10").Font.ColorIndex = 12           '‘字体颜色
   ExcelSheet1.Range("B2:E10").Font.Bold = True        ''加粗

   '为Result Summery设置格式
   ExcelSheet1.Range("B11:F11").Interior.ColorIndex = 53
   ExcelSheet1.Range("B11:F11").Font.ColorIndex = 19
   ExcelSheet1.Range("B11:F11").Font.Bold = True
   ExcelSheet1.Range("B11:G11").HorizontalAlignment = 3 ''4’右边对齐  3 '居中  2 '左边对齐

   '设置Result Summery的边界
   ExcelSheet1.Range("B11:F11").Borders(1).LineStyle = 1
   ExcelSheet1.Range("B11:F11").Borders(2).LineStyle = 1
   ExcelSheet1.Range("B11:F11").Borders(3).LineStyle = 1
   ExcelSheet1.Range("B11:F11").Borders(4).LineStyle = 1

'''对单元格进行冻结
   ExcelSheet1.Range("B12").Select
   ExcelApp.ActiveWindow.FreezePanes = True

''''给单元格赋值
    ExcelSheet1.Range("B1").Value = "可得网自动化测试结果"
    ExcelSheet1.Cells(2,2) = "测试日期:"
ExcelSheet1.Cells(2,3) =  Date
    ExcelSheet1.Cells(3,2) = "开始时间:"
ExcelSheet1.Cells(3,3) = Time
    ExcelSheet1.Cells(4,2) = "结束时间:"
    ExcelSheet1.Cells(5,2) = "执行时长:"
ExcelSheet1.Range("C5").NumberFormat = "[h]:mm:ss;@"
    ExcelSheet1.Cells(6,2) = "测试机器:"
ExcelSheet1.Range("C6").Value =GetIP()
    ExcelSheet1.Cells(7,2) = "运行用例数:"
    ExcelSheet1.Cells(7,3) = 0
    ExcelSheet1.Cells(8,2) = "通过用例数:"
    ExcelSheet1.Cells(8,3) = 0
    ExcelSheet1.Cells(9,2) = "失败用例数:"
    ExcelSheet1.Cells(9,3) = 0
    ExcelSheet1.Cells(10,2) = "忽略用例数:"
    ExcelSheet1.Cells(10,3) = 0
    ExcelSheet1.Cells(2,4) = "测试环境:"
    ExcelSheet1.Cells(3,4) = "SQL 地址:"
    ExcelSheet1.Cells(4,4) = "SQL用户名:"
    ExcelSheet1.Cells(5,4) = "SQL 密码:"
    ExcelSheet1.Cells(6,4) = "数据库名:"
    ExcelSheet1.Cells(10,4) = "当前行:"
    ExcelSheet1.Cells(10,5) = 12
    ExcelSheet1.Cells(2,5) = "Http://192.168.1.81:8085"
    ExcelSheet1.Cells(3,5) = "192.168.1.81"
    ExcelSheet1.Cells(4,5) = "test"
    ExcelSheet1.Cells(5,5) = "t#@!$%"
    ExcelSheet1.Cells(6,5) = "test.keede"
    ExcelSheet1.Cells(11,2) = "用例编号"
    ExcelSheet1.Cells(11,3) ="测试用例标题"
    ExcelSheet1.Cells(11,4) ="测试结果"
    ExcelSheet1.Cells(11,5) ="测试数据"
    ExcelSheet1.Cells(11,6) ="备注"

'''填入结束时间
ExcelSheet1.Cells(4,3) = Time
'''计算脚本运行时间
ExcelSheet1.Range("C5").Value = "=R[-1]C-R[-2]C"

msgbox("请确认测试结果文档中的“测试环境”,“SQL地址”,“SQL用户名”,“SQL密码”,“数据库名”  5个参数是否正确,确认无误后点击确定按钮开始运行测试脚本!如果错误,请直接在EXCEL中更改!")
End Function
'''''*******************************************************************************************************************************************************************************
''''******************************************************************设置测试结果测试数据函数*****************************************************
'''本函数用于对测试用例进行添加测试用例所用的测试数据,一个测试用例可以添加多个测试数据,即可以多次使用本函数
'''调用方法:
                       ''SetTestResultComments 备注标题,备注内容
' *************************************************************************' *************************************************************************
Function  SetTestData(DataTitle,DataValue)
   On Error Resume Next
    Set ExcelSheet1 = ExcelApp.Worksheets.Item("测试结果")
    Row= ExcelSheet1.Cells(10,5)
TestData = ExcelSheet1.Cells(Row,5)
''''防止一个测试用例中多次使用测试数据函数,先取出测试数据中的值,将需要添加的新的值添加在后面
    If  TestData = ""  then
TestData =DataTitle&": "&DataValue
    else
    ''没找到如何对Excel单元格进行换行,现在使用空格来生成换行
         TheValueOfBlankSpace = "                                                                                                             "
TestData = TestData&TheValueOfBlankSpace&DataTitle&":"&DataValue
End If
    ExcelSheet1.Cells(Row,5) = TestData
End Function
'''''*****************************************************************************
'''''******************************************************************设置测试结果备注函数*****************************************************
'''本函数用于对测试用例进行添加备注使用,一个测试用例可以添加多个备注,即可以多次使用本函数
'''调用方法:
                       ''SetTestResultComments 备注标题,备注内容
' *************************************************************************' *************************************************************************
Function  SetTestResultComments(CommentsTitle,CommentsValue)
   On Error Resume Next
    Set ExcelSheet1 = ExcelApp.Worksheets.Item("测试结果")
    Row= ExcelSheet1.Cells(10,5)
Comments = ExcelSheet1.Cells(Row,6)
''''防止一个测试用例中多次使用备注函数,先取出备注的值,将需要添加的新的值添加在后面
    If  Comments = ""  then
Comments=CommentsTitle&": "&CommentsValue
    else
    ''没找到如何对Excel单元格进行换行,现在使用空格来生成换行
         TheValueOfBlankSpace = "                                                                                                             "
Comments=Comments&TheValueOfBlankSpace&CommentsTitle&":"&CommentsValue
End If
    ExcelSheet1.Cells(Row,6) = Comments
End Function
'''''*****************************************************************************
'''''**********************************开始函数*******************************************
Function StartQTP()
    CreatAExcelAndPrintTheForm()
End Function

'''''****************************************************************************
'''''***********************************结束函数******************************************
Function EndQTP()
   On Error Resume Next
  Set ExcelSheet1 = ExcelApp.Worksheets.Item("测试结果")
'''将当前行和行数置空,该值只用于脚本运行过程
' ExcelSheet1.Range("D10").Value = ""
' ExcelSheet1.Range("E10").Value = ""

  '''填入结束时间用来计算脚本执行时间,插入后会自动进行计算执行时间
ExcelSheet1.Cells(4,3) = Time

  '保存已经存在的Excel文档
  ExcelApp.ActiveWorkbook.Save

   ''''释放参数
'    ExcelApp.Quit
    Set ExcelSheet1 = Nothing
    Set ExcelApp = Nothing

End Function
'''''*****************************************************************************
'''''******************************************************************打开已经存在的Excel*****************************************************************
Function OpenExcel(ExcelPath)
    Set ExcelApp = CreateObject("Excel.Application")
    ExcelApp.Visible = True
    Set objworkbook = ExcelApp.Workbooks.Open (ExcelPath)
' Set ExcelSheet1 = ExcelApp.Worksheets.Item("测试结果")
End Function
'''''*****************************************************************************'''''*****************************************************************************
分享到:
评论

相关推荐

    QTP Excel函数

    QTP Excel函数包含全部常用函数功能代码 QTP Excel函数 QTP Excel函数

    qtp excel函数

    qtp excel函数

    非常全的QTP学习资料

    QTP Excel函数.docX QTP:EXCEL报告输出格式,源码.docx qtp常用方法.txt QTP代码参考框架.txt QTP多种函数使用方法.doc QTP技术集锦.doc QTP识别和操作对象的原理.doc 1 自动化测试的实践-应用QTP测试框架.ppt 3 TD...

    QTP从excel中读取测试数据,写入测试结果源代码

    QTP从excel中读取测试数据,写入测试结果源代码 其中《calcexcel.vbs》为封装的函数,《calcexcl.txt》为QTP中代码

    QTP常用技巧应用与代码实践

    3、QTP读取Excel数据的方法 4、QTP连接数据库 5、QuickTest Plus小工具,大作用 6、基础知识 7、对象库和对象属性 8、文件、参数表和ACTION 9、数据库链接和检查 10、其它运行时问题 11、QTP中常用的VBS函数 12、...

    QTP描述性编程技术集锦

    53. 对文件系统操作 125 54. 对象同步 125 55. 正则表达式应用模板 125 56. Wscript.shell 的应用 126 57. Create log file 126 58. 对象同步 127 59. 异常控制 127 60. Check if the object.exist 128 61. QTP场景...

    QTP 学习资料打包

    QTP 学习资料打包 包括 oldsidney QTP学习笔记、 QTP技巧和实用函数、 VBS脚本语言、 VBS函数速查、 QTP EXCEL函数、 QTP学习笔记、 QTP学习历程、 QTP常用方法

    精通QTP——自动化测试技术领航

    3.4 Excel—数据驱动必备利器 3.5 WshShell对象常用方法介绍 3.6 FSO对象模型 3.7 重要的Test Design Studio 3.8 DotnetFactory对象 3.9 API的应用 4.1 QTP深入探索 4.2 QTP深层机密 4.3 总结 5.1 QTP设计模式 5.2 ...

    QTP技术集锦--总结

    53. 对文件系统操作 125 54. 对象同步 125 55. 正则表达式应用模板 125 56. Wscript.shell 的应用 126 57. Create log file 126 58. 对象同步 127 59. 异常控制 127 60. Check if the object.exist 128 61. QTP场景...

    用QTP实现自动对比网页上两表的数据

    通过QTP脚本中的函数读取表1中的数据到Run-Time Table的Global sheet中,读取表2中的数据到Run-Time Table的Action1 sheet中。通过程序控制对Global sheet 和Action1 sheet中的相关数据作统计,比较。对不一致的数据...

    公司内部QTP培训PPT

    7.QTP的参数化-DataTable/excel 8.QTP操作Action 9.QTP验证点与测试报告 10.VBS的介绍 11.常用WEB控件介绍 12.AOM 13.场景恢复 14.正则表达式的运用 15.Html中DOM的运用 16.VBS操作数据库 17.WshShell介绍 20.函数...

    QTP中对用户自定义环境变量的XML操作的几个函数

    那么在多个Test之间如何传递参数呢,一般常用的有excel,这个方法应该比较普遍了。这里介绍一下我自己设计的方法。就是用环境变量来传递参数,  大家可能都知道怎么在一个Test里的多个action之间传递参数,可以用...

    基于QTP的数据驱动测试开发

    具体方法是采用Excel作为测试数据源,制定Excel中测试数据的设计格式,建立专用的函数库来操作Excel数据,实现以数据为驱动的自动化测试。这一方法能有效地实现测试数据与测试脚本的分离,同时,测试数据的设计操作...

    2018 猎豹网校 教程大全 资源难找赶紧保存吧

    Excel 零基础教程(赠送图表操作技巧)[MP4] Excel 达人进阶 你早该这么玩 Excel[MP4] Excel 达人进阶 公式和函数使用技巧[MP4] DirectX 9.0 3D 游戏开发编程基础[MP4] Delphi基础课程[MP4] C语言高级教程 指针和...

Global site tag (gtag.js) - Google Analytics