With 语句
相关项: GUI 操作和脚本化 GUI 组件
当在 VBScript 中编写脚本时,With 语句将具有相同父层次结构的连续语句分到一组,使脚本更简明且更易于阅读、编写或编辑。
此外,使用 With 语句可能会帮助您更快地运行脚本。运行 With 语句时, 在运行第一个语句之前识别应用程序中的对象,但在运行每个语句之前不进行重新识别。
另一方面,运行 With 语句时,如果由 With 语句引用的对象在应用程序中以某些方式刷新、重绘或更改,这会影响测试的运行。要指定在运行下一个语句之前重新识别应用程序中的对象,请添加将调用 RefreshObject 测试对象操作的语句。
With 语句有以下语法:
With object 语句 End With
|
项 |
描述 |
|---|---|
|
对象 |
对象或返回对象的函数。 |
|
语句 |
要在对象上执行的一个或多个语句。 |
可以将下列脚本:
Window("Flight Reservation").WinComboBox("Fly From:").Select "London"
Window("Flight Reservation").WinComboBox("Fly To:").Select "Los Angeles"
Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinList("From").
Select "19097 LON"
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
替换为:
With Window("Flight Reservation")
.WinComboBox("Fly From:").Select "London"
.WinComboBox("Fly To:").Select "Los Angeles"
.WinButton("FLIGHT").Click
With .Dialog("Flights Table")
.WinList("From").Select "19097 LON"
.WinButton("OK").Click
End With 'Dialog("Flights Table")
End With 'Window("Flight Reservation")
除手动输入 With 语句外,还可以指示 OpenText Functional Testing 在您录制时自动生成 With 语句,或为现有测试生成 With 语句。有关更多详细信息,请参阅请参阅 "生成 With 语句"。

