DataTables

来源:互联网 发布:网络上1米是多少钱 编辑:程序博客网 时间:2024/05/29 03:20

Chapter 4 DataTables

QTP中,DataTable所扮演的角色主要为数据驱动。它和MS Excel的数据表非常相似。通过DataTable,可以控制每个Action运行的次数。DataTable分为全局表(Global)和局部表(Local)两种。全局表用来控制所有的Action,而每个Action则拥有其名下的局部表。有一点让人很容易产生混淆,不同的Action之间可以互相调用各自局部表的数据,只是调用方法需要用代码来实现。

4-1.DataTable

4-1中的DataTable范例添加了2个参数:Username和Password

DataTable是封装过后的Excel数据表,我们可以使用一些Excel所拥有的公式,同时 QTP提供了很多关于操作DataTable的函数。

'Gives the value of Parameter1 stored in the Global data table

DataTable("Patameter1",dtGlobalSheet)

'Gives the value of Parameter1 stored in the current's action local data table

DataTable("Patameter1",dtLocalSheet)

在同一张数据表中,不能存在名称相同的参数。但是在不同的数据表里(Global DataTableLocal DataTable),可以同时存在两个一样的参数。当程序执行的时候,每次只会读取DataTable中某一行的值。当我们保存QTP脚本时,DataTable中的内容会默认保存在相同路径下的Default.xls文件中。

4-2.Default.xls中的内容

打开Default.xls我们可以看到,每张表的第一行内容保存了QTP中填写的参数名。从第二行起才是DataTable中第一行的参数值。上图中显示,在Global表中有2行有效数据。QTP只会读取边框加黑的单元格内容,即使内容为空,QTP也认为它是有效的。

DataTable两种不同状态的比较

Design-Time  – 在脚本编写过程中Data Table面板中显示的是Design-time Data Table。当脚本被保存时,DataTable中的内容也同样被保存。

Run-Time – 在脚本运行过程中Data Table面板中显示的是Run-time Data Table。脚本执行时,QTP会相应的创建一个临的Data TableRun-time Data Table,首先会复制一份Design-time Data Table中的内容。在运行期间,QTP会在Data Table面板中显示Run-time数据,这样当参数值发生任意变化时,你都可以从Data Table面板观察得到。脚本运行中参数值的改变会记录到测试结果中。但脚本运行结束后,Run-time DataTable中改变的内容不会被保存,DataTable会恢复到原先的Design-time状态。

如果你很希望存储Run-time Data Table中的数据,可以在测试的结尾使用DataTable.Export语句,这样就可以将Run-time Data Table中的数据导出并保存为一个文件。然后你可以在Data Table面板,使用File>Import From File菜单,将文件中的数据导入到Design-time Data Table中;你也可以在测试脚本的最前面添加DataTable.Import语句,将以前测试导出的Run-time Data Table数据导入到当前测试的Run-time Data Table中。

全局表和局部表的使用区别

我们必须弄明白,什么时候该用全局表(Global),什么时候该用局部表(Local)。

以订票系统为例,列举出2种场景:

场景– 不同的用户,每次登陆系统,订一张票,登出系统。

场景– 单一用户,登陆系统,订3张不同的票,登出系统。

场景1下,我们用Global表作为数据驱动比较合适。以usernamepasswordtickets details作为3个参数,只需要单一的Action,多次运行,就可以实现目的。

场景2下,则采用Local表作为数据驱动比较合适。首先,我们需要将脚本拆分成3ActionLoginbookingLogout。其中LoginLogout只需要运行一次,我们用booking的局部表来控制它运行3次来实现我们的目的。

设置DataTable的循环次数

通过对全局表的设定,可以控制整个测试用例的循环次数。打开菜单File → Settings → RunTab)。下图显示的设置将影响到脚本运行的状态。

4-3.Data Table Iterations

Note:我们也可以通过导入外部数据表作为运行时的DataTable。方法是在Test SettingsResourceTab)中导入。

若想设置特定的Action的运行次数,切换到关键字视图,选择Action后在右键菜单中选择Action Call Properties。在弹出的Action Call Properties对话框中,可以设置单个Action的运行方式。

4-4.Action Call Properties – Keyword View

4-5. Action Call Properties设置

DataTable 对象模式

QTP中, 可以把DataTable当作对象处理,通过调用对象的方法来操作Data Table中任意单元格中的数据DataTable的对象有3种:

l DataTable – 把整个DataTable作为一个对象,包含全局表和局部表。

l DTSheet – 把单一的一张数据表作为一个对象。

l DTPatameter – 把数据表中的单独一列作为一个对象。

每种对象都有对应的属性和方法,我们可以在QTP的帮助文档中找到详细的解释说明。

常见的DataTable操作

当我们往数据表中添加数据时,QTP会将它转化为最优的数据格式。例如,我们在单元格中输入“12345678901”,那么它会自动转化为“1.23456789E+010”。如果我们添加的内容以单引号开头(‘),那么将会被当做文本处理,而不会进行其他格式的转化。也可以通过点击单元格的右键菜单来选择期望的数据格式。

范例4-1 如何从全局表中获取一个参数值

'Methods of getting a Data Table value

val = datatable.Value("ParamName",dtGlobalSheet)

val = datatable.Value("ParamName","Global")

'by giving the sheet index starting from 1 for the global sheet

val = datatable.Value("ParamName",1)

'Sheet name or id is a optional parameter and is assumed to be as for global data sheet in case not provided

val = datatable.Value("ParamName")

'Value property is the default property of the DataTable object so DataTable("ParamName",dtGlobalSheet)

'is equivalent to datatable.Value("ParamName",dtGlobalSheet)

val = datatable("ParamName",dtGlobalSheet)

val = datatable("ParamName")

'Using the data table object model

val = datatable.GlobalSheet.DeleteParameter("ParamName").value

val = datatable.GlobalSheet.DeleteParameter("ParamName").valueByRow(1)

范例4-2 如何从局部表中获取一个参数值

'Various methods to get data table value

val = datatable.Value("ParamName",dtLocalSheet)

val = datatable.Value("ParamName","<LocalActionName>")

val = datatable("ParamName",dtLocalSheet)

val = datatable("ParamName","<LocalActionName>")

'The local sheet of the action which is executing this statement

val = datatable.LocalSheet.DeleteParameter("ParamName").value

范例4-3 判断指定的数据表是否存在

'Function to check if DataTable exists

Function isSheetExists(sheetName)

   On error resume next

   isSheetExists = true

   err.clear

   Set objSheet = datatable.GetSheet(sheetName)

   If err.number <>0  Then

  isSheetExists= false

   End If

End Function

范例4-4 如何保存数据表的数据输出格式

'This would be modified to 1.23456789E+010 due to autoformatting

datatable("ParamName") = "12345678901"

'This will not be auto formatted and will be treated as text

datatable("ParamName")= "'" &  "12345678901"

范例4-5 判断指定的参数在数据表中是否存在

'Check if a parameter exists in data table

Function isParameterExists(sheetName, paramName)

On error resume next

    isParameterExists = TRUE

    Err.clear

    ParamTotal = DataTable.GetSheet(sheetName).GetParameter(paramName)

    'In case of error the parameter does not exist

    If err.number<>0 then

    isParameterExists = False

End if

End Function

范例4-6 如何从一个网页表格中将数据导入Datatable.我们假设表格的第一行是字段名称.我们把它们作为参数导入Datatable

'Variable declaration

Dim i,j

Dim rowCount,colCount

Dim cellText,objTable

'Get table object

Set objTable = Browser("").Page("").WebTable("")

'Get the row count of the webtable

rowCount = objTable.RowCount

'Get the column count of the webtable

rowCount = objTable.ColumnCount(1)

'Create a output sheet

Set outSheet = datatable.AddSheet("Output")

'Greate Parameters based on the 1st row of the web table

For i=2 to colCount

cellText = objTable.GetCellData(1,i)

'Note in case the CellText contains space in between Then QTP will automatically convert it to a "_" character

outSheet.AddParameter celltext,""

Next

'Skip first row as we assumed it to be a header rows 

For i = 2 to rowCount

outSheet.SetCurrentRow i-1

'Re-calculate the column count as some rows have different column sizes

colCount = objTable.ColumnCount(i)

For j = 2 to colCount

  cellText = objTable.GetCellData(i,j)

  outSheet.GetParameter(j-1).value = cellText

Next

Next

范例4-7 如何获取表中指定行的参数值

'Get a value by row

val = datatable.GetSheet("SheetName").GetParameter("ParamName").ValueByRow(RowNumber)

范例4-8 当脚本设置只运行一次时,如何使用代码来控制跑完全局表中的所有参数

'Declare variable

Dim i,iCount

'Get the global sheet object

Set oGlobal  = datatable.GlobalSheet

'Get # of rows

iCount = oGlobal.GetRowCount

For i = 1 to iCount

'set the current row

oGolbal.SetCurrentRow i

'Execute the code to be repeated here

msgbox DataTable("UserName")

Next

范例4-9 如何获取包含数据的行数

要解决这个问题,我们可以利用Excel的公式COUNTA(A1:A65536),我们在数据表中添加一个含有公式的参数,然后读取它的值

'Add a new parameter with the formula

'For Columns 1 of data table use A1:A65536

'For column 2 of data table use B1:B65536 and so on

DataTable.GlobalSheet.AddParameter "New","=COUNTA(A1:A65536)"

'Get the new value

Msgbox DataTable("New")

原创粉丝点击