在我们做自动化测试的时候,难免会用到拖拽的功能,这个问题解决办法有两个,其一就是在录制的过程中切换到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