1

我正在尝试在 QTP 中运行此代码,该代码用于从网格中删除记录。这段代码

dgRows = SwfWindow("PWC - [PWC]").SwfWindow("Region Master").SwfTable("dgMaster").RowCount
For i = 1 To dgRows 

SwfWindow("PWC - [PWC]").SwfWindow("Region Master").SwfTable("dgMaster").SelectCell i-1,0 
'row of data grid begins with 0, hence i -1
SwfWindow("PWC - [PWC]").SwfWindow("Region Master").SwfButton("DELETE").Click
Swfwindow("PWC - [PWC]").SwfWindow("Region Master").SwfWindow("RegionMaster").SwfButton("Insert").Click
    deleteCode = closePrompt()

If deleteCode = 15 Then 'closePrompt returns 15 when record is successfully deleted
    i = i - 1           'As record is deleted, grid has lost one record and for loop will exit early by one record or throw error.
 dgoRows = SwfWindow("PWC - [PWC]").SwfWindow("Region Master").SwfTable("dgMaster").RowCount
End If
Next

这段代码从 1 到网格中的行数 (dgRows) 运行。

如果有 3 行,它将运行三次并尽可能删除记录。如果删除了 1 条记录,则网格会丢失一条记录。因此我试图通过代码调整 i 和 dgRows 的值

i = i - 1 '由于记录被删除,网格丢失了一条记录,for循环将提前退出一条记录或抛出错误。dgoRows = SwfWindow("PWC - [PWC]").SwfWindow("Region Master").SwfTable("dgMaster").RowCount

'更新 dgRows 的新值,以便 QTP 不会单击不存在的 i 值的行。

我试图用这段代码来说明我面临的问题

dgRows = SwfWindow("PWC - [PWC]").SwfWindow("Region Master").SwfTable("dgMaster").RowCount

删除行后,for循环迭代时不会动态获取网格中的行数。因此 i 值变为等于 3 行,但实际上在网格中只有一行,因为 2 条记录已被删除,因此 QTP 尝试单击 i 值为 3 的单元格但没有找到它并引发错误。

谁能告诉我为什么 ("dgMaster").RowCount 不更新自身或在 for 循环下一次运行时如何更新它?

4

1 回答 1

0

在 VBScript 中,for 循环只计算一次限制。

证明:

limit = 4
For i = 1 to limit
    limit = limit - 1
    document.write(i & " - " & limit & "<br>")
Next

输出:

1 - 3
2 - 2
3 - 1
4 - 0

您应该改用While循环

dgRows = SwfWindow("PWC - [PWC]").SwfWindow("Region Master").SwfTable("dgMaster").RowCount
i = 1 ' I would use 0 but I'm keeping the code as close to original as possible
While i <= dgRows
    i = i + 1
    SwfWindow("PWC - [PWC]").SwfWindow("Region Master").SwfTable("dgMaster").SelectCell i-1,0 
    'row of data grid begins with 0, hence i -1
    SwfWindow("PWC - [PWC]").SwfWindow("Region Master").SwfButton("DELETE").Click
    Swfwindow("PWC - [PWC]").SwfWindow("Region Master").SwfWindow("RegionMaster").SwfButton("Insert").Click
    deleteCode = closePrompt()

    If deleteCode = 15 Then 'closePrompt returns 15 when record is successfully deleted
        i = i - 1           'As record is deleted, grid has lost one record and for loop will exit early by one record or throw error.
        dgoRows = SwfWindow("PWC - [PWC]").SwfWindow("Region Master").SwfTable("dgMaster").RowCount
    End If
WEnd
于 2013-05-23T07:53:59.060 回答