在我们做自动化测试的时候,难免会用到拖拽的功能,这个问题解决办法有两个,其一就是在录制的过程中切换到QTP的低级模式录制脚本,像下面的code一样:
Window("Windows Internet Explorer").WinObject("Internet Explorer_Server").Drag 1372,329
Window("Windows Internet Explorer").WinObject("Internet Explorer_Server").Drop 879,249
之后就是想办法把drag 和drop后面的数字参数化,具体方法可以参考接下来的第二种方法,这也是我在项目中用的方法;
方法二就是用对象“Mercury.DeviceReplay”,直接上code吧:
Function AUT_DragAndDrop_In_RoleGroupAdmin(I_NowPosition,I_Name)
On Error Resume Next
Available_X=Browser("AUT").Page("Page_UserGroupRolePermission").Link("Link_AvailableUsers").GetROProperty( "abs_x" )
Available_Y=Browser("AUT").Page("Page_UserGroupRolePermission").Link("Link_AvailableUsers").GetROProperty( "abs_y" )
User_X=Browser("AUT").Page("Page_UserGroupRolePermission").WebElement("WebElement_Profile_Users").GetROProperty( "abs_x" )
User_Y=Browser("AUT").Page("Page_UserGroupRolePermission").WebElement("WebElement_Profile_Users").GetROProperty( "abs_y" )
Set obj_Name=Description.Create()
obj_Name("micclass").Value = "WebElement"
obj_Name( "html tag" ).Value = "LI"
obj_Name("innertext").Value=I_Name
obj_Name("class").Value="ui-draggable"
obj_Count=Browser("AUT").Page("Page_UserGroupRolePermission").ChildObjects(obj_Name).Count
If obj_Count=1 Then
Set oWebElem1 =Browser("AUT").Page("Page_UserGroupRolePermission").WebElement(obj_Name)
Else
AUT_DragAndDrop_In_RoleGroupAdmin="Not found webelment "&I_Name&" or its count is "&obj_Count
Exit Function
End If
nX1 = oWebElem1.GetROProperty( "abs_x" )
nY1 = oWebElem1.GetROProperty( "abs_y" )
Set RoleGroup = CreateObject( "Mercury.DeviceReplay" )
If Ucase(I_NowPosition)="LEFT" Then
RoleGroup.DragAndDrop nX1+5,nY1+10, Available_X+10,Available_Y+15, LEFT_MOUSE_BUTTON 'plus 5/10/15 is aiming to make sure it works on IE8
ElseIf Ucase(I_NowPosition)="RIGHT" Then
RoleGroup.DragAndDrop nX1+5,nY1+10, User_X+5,User_Y+15, LEFT_MOUSE_BUTTON
End If
wait 1
Set obj_Name=Nothing
Set oWebElem1=Nothing
Set RoleGroup=Nothing
AUT_DragAndDrop_In_RoleGroupAdmin=Err.Description
End Function
解释一下这个function,首先先用GetROProperty( "abs_x" )和GetROProperty( "abs_y" )获取drag和drop的源和目的坐标,说明一下,左上角起为(0,0),横向向右为x,纵向向下为y,而后再用描述性编程的方式找到你想要拖拽的具体item,当然之后还是为了获取这个item的具体坐标,因为这个方法也是用的坐标,最后CreateObject( "Mercury.DeviceReplay" )并使用其DragAndDrop的方法就可以大功告成了,不过我在IE8上遇到了一个问题,就是拖拽的时候会出现像同时按住了Ctrl一样选中了拖拽路径上的items,这个结果可想而知一定会失败的,之后debug发现,GetROProperty( "abs_x" )和GetROProperty( "abs_y" )获取的坐标是item的左上角的位置,所以,为了能在IE8上更好的work,就需要将item的坐标位置进行人为变动,使其尽量接近中下方位以确保在DragAndDrop的时候可以不受其他items的影响,从而顺利的在IE8,9,10上work!最后,一定不要忘记释放掉你所set的object,原因你懂的!