QTP之drag and drop

 在我们做自动化测试的时候,难免会用到拖拽的功能,这个问题解决办法有两个,其一就是在录制的过程中切换到QTP的低级模式录制脚本,像下面的code一样:

[vb]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Window("Windows Internet Explorer").WinObject("Internet Explorer_Server").Drag 1372,329  
  2. Window("Windows Internet Explorer").WinObject("Internet Explorer_Server").Drop 879,249  

之后就是想办法把drag 和drop后面的数字参数化,具体方法可以参考接下来的第二种方法,这也是我在项目中用的方法;

方法二就是用对象“Mercury.DeviceReplay”,直接上code吧:

[vb]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Function AUT_DragAndDrop_In_RoleGroupAdmin(I_NowPosition,I_Name)  
  2.     On Error Resume Next  
  3.     Available_X=Browser("AUT").Page("Page_UserGroupRolePermission").Link("Link_AvailableUsers").GetROProperty( "abs_x" )  
  4.     Available_Y=Browser("AUT").Page("Page_UserGroupRolePermission").Link("Link_AvailableUsers").GetROProperty( "abs_y" )  
  5.     User_X=Browser("AUT").Page("Page_UserGroupRolePermission").WebElement("WebElement_Profile_Users").GetROProperty( "abs_x" )  
  6.     User_Y=Browser("AUT").Page("Page_UserGroupRolePermission").WebElement("WebElement_Profile_Users").GetROProperty( "abs_y" )  
  7.     Set obj_Name=Description.Create()  
  8.     obj_Name("micclass").Value = "WebElement"  
  9.     obj_Name( "html tag" ).Value = "LI"  
  10.     obj_Name("innertext").Value=I_Name  
  11.     obj_Name("class").Value="ui-draggable"  
  12.     obj_Count=Browser("AUT").Page("Page_UserGroupRolePermission").ChildObjects(obj_Name).Count  
  13.     If obj_Count=1 Then  
  14.         Set oWebElem1 =Browser("AUT").Page("Page_UserGroupRolePermission").WebElement(obj_Name)  
  15.     Else  
  16.         AUT_DragAndDrop_In_RoleGroupAdmin="Not found webelment "&I_Name&" or its count is "&obj_Count  
  17.         Exit Function  
  18.     End If  
  19.     nX1 = oWebElem1.GetROProperty( "abs_x" )  
  20.     nY1 = oWebElem1.GetROProperty( "abs_y" )  
  21.     Set RoleGroup = CreateObject( "Mercury.DeviceReplay" )  
  22.     If Ucase(I_NowPosition)="LEFT" Then  
  23.         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  
  24.     ElseIf Ucase(I_NowPosition)="RIGHT" Then  
  25.         RoleGroup.DragAndDrop nX1+5,nY1+10, User_X+5,User_Y+15, LEFT_MOUSE_BUTTON  
  26.     End If  
  27.     wait 1  
  28.     Set obj_Name=Nothing  
  29.     Set oWebElem1=Nothing  
  30.     Set RoleGroup=Nothing  
  31.     AUT_DragAndDrop_In_RoleGroupAdmin=Err.Description  
  32. 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,原因你懂的!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值