QTP的那些事--在QC中importsheet方式导入excel数据驱动

简介:

原文地址:

http://www.joinwithjack.com/testautomation/importsheet-in-qtp-data-table-from-qc

该地址提供了一个解决的方法:

ImportSheet in QTP Data Table from QC

By Jiten on May 28, 2011 4 Comments and 2 Reactions

How to import and export sheets from Quality Center using DataTable of QTP ?

I came across this problem recently when we decided to store our excel files in test plan module of Quality Center(Version 10.0.).

Joining the automation team while the project is already ON keeps you limited in your approaches. If Automation is being carried out using QTP and Quality Center with a framework, which is not flexible It limits the approaches further.

In my project, There were loads of reusable Actions , in which data was being pulled from excel sheets and we were using "DataTable.ImportSheet" for the task. Excel files were on client filesystem BUT now we wanted to keep our data at a central location so that it is created, managed and maintained better. QC was on everybody's mind as It was already being used by other testing teams as central repository. Till Date QC does not have any mechanism which can be used to take care of test data in easier and better ways. Atleast to me it is not better. I am not a very big fan of QTP and QC.

We wanted to reflect minimum changes, for data needs, in our scripts. I wish QTP could support over-riding ImportSheet method of DataTable using RegisterUserFunc. Which could solve the problem without going inside scripts and replacing DataTable.ImportSheet with search and replace option. However, We do not have that option.

I am posting the approach taken by me (May not be the best one) so that it helps you if you are also stuck on the same problem.

Importing Data tables inside QTP is quite simple using the following line of code.

DataTable.ImportSheet "C:\Full\File\path\FileName.xls" _
,"SourceSheetName","DestinationSheetName"

'OR
'Add C:\Full\File\path to folder options in QTP Options.

DataTable.ImportSheet pathfinder.locate("FileName.xls") _
,"SourceSheetName","DestinationSheetName"

If you refer the documentation provided in QTP,you will find that the Ist parameter to ImportSheet method can be a filesystem path or Quality Center Path.

Following the docs In File-->Settings-->resources if you would choose a path for importing data table using the file explorer and point it to the file stored in attachments of a test stored in Quality Center the path would be something like

[Quality Center] \Subject\path\to\yourtest\FileName.xls

  1. What was digged to solve the problem ?

    1. Pathfinder.Locate("FileName.xls") by adding my QC Folder in options.

      I tried using this approach, however it did not work, and I kept getting error Path not found

    2. Can I play with Environment("TestDir") or Envrionment("SystemTempDir") ??

      If you explore the %Temp%TD_80 you would see the following path to the attachments of your test!!

      "C:\Users\[UserName]\AppData\Local\Temp\TD_80\ _6f4df540\Attach\TEST\[TestID]\TEST_[TestID]_[FileName].xls"

      How to ensure that Temp%\TD_80 is not cleared on every round of execution. And the solution should mandatorily download the excel file so that we can keep our standard approach of using DataTable.ImportSheet.

  2. Solution

We decided to go for OTA API and felt relieved that it could be accessed using set QCConn = QCUtil.QCConnection inside QTP's IDE.

Following is the algorithm, which we used.

  1. Get hold of a Test Stored in Quality Center and reach the attachment object associated with a test.
  2. Download the excel file(stored as attachment) to client's filesystem using the Attachment Object.
  3. Use DataTable.ImportSheet method to import data.
  4. replce DataTable.ImportSheet call with a custom function and Leave rest of the scripts as intact as it was.

I am posting the code below for your use and review.

Public Function DataTableImportSheetFromQC(QTPTest,sFileName,SheetSource,SheetDest)
        Dim attachFact,attachList,attachObj,bResult

        On Error Resume Next

        'Assingments
        bResult=False
        Set attachFact = QTPTest.Attachments
        Set attachList = attachFact.NewList("")

        For each attachObj in attachlist
        'Ensuring Excel File is stored as attachment to the test
        'Naming Convention used by QC is TEST_[TESTID]_[FileName]
                If Instr(attachObj.Name,sFileName)>0 And _
                        Instr(attachObj.Name,TestObject.ID)>0 Then
                        'Download the attachment to client  filesystem
                        'Import Sheet Method was not working to QCPaths,
                        'Or We can say that QCPath is the temp path of downloaded file
                        attachObj.Load True, "" 'the second param is not required in vbscript
                        'FilName Property of attachObj gives full file path
                        DataTable.ImportSheet  attachObj.FileName,SheetSource,SheetDest
                        bResult = True
                        Exit For ' Dont waste time in searching for others
                End If
        Next

        If Err.Number <> 0 Or Not  bResult Then
                bResult = False
                Err.Clear
                On Error goto 0
        End If

        DataTableImportSheetFromQC = bResult
        'release the objects
        Set attachList = Nothing
        Set attachFact = Nothing

End Function

After designing this function we replaced all lines, which used DataTable.ImportSheet statement with a call to this function. Hope it is of some help to you and please post replies if there is any other approach, which you have adopted

 

 

其中的第一个参数为如下说明:

Apologies for using the wrong term to define a variable. I have changed the Name of variable to QTPTest, which is stored in Test Plan Module of Quality Center. If you are working inside QTP then you can use QCUtil.QCConnection object to reach the QTPTest. If you have opened an already stored test in QC , then you can use QCUtil.CurrentTest to create QTPTest. If you are using any other COM enabled language such as vbscript outside QTP, please refer OTA help





本文转自hcy's workbench博客园博客,原文链接:http://www.cnblogs.com/alterhu/archive/2012/02/11/2346765.html ,如需转载请自行联系原作者。

目录
相关文章
|
JSON 中间件 API
Scrapy中的parse命令:灵活处理CSV数据的多功能工具
Scrapy是一个用Python编写的开源框架,它可以快速地从网站上抓取数据。Scrapy提供了许多强大的功能,其中之一就是parse命令,它可以让你灵活地处理CSV数据。CSV(逗号分隔值)是一种常用的数据格式,它用逗号来分隔不同的字段。在本文中,我们将介绍parse命令的基本用法,以及它的一些亮点和案例。
158 0
Scrapy中的parse命令:灵活处理CSV数据的多功能工具
|
测试技术
python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(优化版)
python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(优化版)
307 0
python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(优化版)
|
JSON 前端开发 数据格式
给我实现一个前端的 Excel 导入和导出功能(二)
给我实现一个前端的 Excel 导入和导出功能
251 0
|
JSON 前端开发 BI
给我实现一个前端的 Excel 导入和导出功能(一)
给我实现一个前端的 Excel 导入和导出功能
266 0
|
测试技术 Python
python接口自动化(二十六)--批量执行用例 discover(详解)
我们在写用例的时候,单个脚本的用例好执行,那么多个脚本的时候,如何批量执行呢?这时候就需要用到 unittest 里面的 discover 方法来加载用例了。加载用例后,用 unittest 里面的 TextTestRunner 这里类的 run 方法去一次执行多个脚 本的用例。那么前边介绍那么多都是半道开始,半道出家,这篇就带大家从头到尾,一步一步给小伙伴们详细介绍一下。
258 0
python接口自动化(二十六)--批量执行用例 discover(详解)
|
easyexcel Java
超方便.使用EasyExcel导入excel
超方便.使用EasyExcel导入excel