我正在尝试在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不会单击某行的值,而不是 存在。
我尝试用这段代码说明我面临的问题
dgRows = SwfWindow(“PWC - [PWC]”)。SwfWindow(“Region” 主 “)。SwfTable(” dgMaster“)。行数
删除行后,当for循环迭代时,它不会动态获取网格中的行数。因此,我的值变为等于3行,但实际上在网格中有一行,因为已删除了2条记录,因此QTP尝试单击i值为3的单元格但未找到它并抛出错误。
任何人都可以告诉我为什么不(“dgMaster”)。RowCount更新本身或如何在for循环运行时更新它?
答案 0 :(得分: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