QTP脚本开发过程中,可以使用with语句来简化代码。
With 语句通过将连续语句与相同的父层次分到一组。默认情况下,with选择是不启动的,也就是录制时,QTP不会自动加入该语句,若要启动自动添加with功能,选择选择“tools”>“Options”>“General”,勾选“Automatically generate "With"
statements after recording”
比对:
勾选前:
Dialog("Login").WinEdit("Agent
Name:").Set "mercury"
Dialog("Login").WinEdit("Password:").SetSecure
"4d7efbf62bc8a280d9a4600245d218b1fe1b6553"
Dialog("Login").WinButton("OK").Click
Window("Flight
Reservation").WinObject("Date of Flight:").Click 2,8
Window("Flight
Reservation").WinObject("Date of Flight:").Type
"122411"
Window("Flight
Reservation").WinComboBox("Fly From:").Select
"Frankfurt"
Window("Flight
Reservation").WinComboBox("Fly To:").Select "London"
Window("Flight
Reservation").WinButton("FLIGHT").Click
Window("Flight
Reservation").Dialog("Flights
Table").WinList("From").Select "13552 FRA
08:00 AM LON 08:45 AM
SR $164.50"
Window("Flight
Reservation").Dialog("Flights
Table").WinButton("OK").Click
Window("Flight
Reservation").WinEdit("Name:").Set "test"
Window("Flight
Reservation").WinButton("Insert Order").Click
Window("Flight
Reservation").Close
勾选后:
With Dialog("Login")
.WinEdit("Agent
Name:").Set "mercury"
.WinEdit("Password:").SetSecure
"4d7efc61842c44fa533b96c8981187a0d9887eff"
.WinButton("OK").Click
End With
With Window("Flight
Reservation")
.WinObject("Date
of Flight:").Click 5,5
.WinObject("Date
of Flight:").Click 3,5
.WinObject("Date
of Flight:").Type "112411"
.WinComboBox("Fly
From:").Select "Frankfurt"
.WinComboBox("Fly
To:").Select "Los Angeles"
.WinButton("FLIGHT").Click
With
.Dialog("Flights Table")
.WinList("From").Select
"20324 FRA 09:12 AM
LAX 04:23 PM AA
$112.20"
.WinButton("OK").Click
End
With
.WinEdit("Name:").Set
"test"
.WinButton("Insert
Order").Click
.Close
End With
如果录制过程中,没勾选自动生成功能,而录制完成后,又想使用该功能,那么可以使用“Edit” >”advanced” >”Apply“with”to script…”
同样,如果不想使用该语句,那么可以对已经使用的with进行删除,使用“Edit” >“advanced” >“Remove“with” to script…”