QTP 自定义方法将IE最大化最小化

This article demonstrates a quick tip to activate, minimize or maximize browsers. Unlike a Standard Windows Window object, Browser does not support theActivateMinimizeMaximize methods. Therefore, we can create our custom function and tie it with the Browser object using RegisterUserFunc.

The only trick is to retrieve the Browser Handle and substitute the handle in the description of a Window object, and use the Window’s Activate method instead.

ACTIVATE BROWSER

  1. Function BrowserActivate(Object)  
  2.     Dim hWnd  
  3.    
  4.     hWnd = Object.GetROProperty("hwnd")  
  5.    
  6.     On Error Resume Next  
  7.         Window("hwnd:=" & hWnd).Activate  
  8.    
  9.         If Err.Number <> 0 Then  
  10.             Window("hwnd:=" & Browser("hwnd:=" & hWnd).Object.hWnd).Activate  
  11.             Err.Clear  
  12.         End If  
  13.     On Error Goto 0  
  14. End Function  
  15.    
  16. RegisterUserFunc "Browser""Activate""BrowserActivate"  


After registering the BrowserActivate function with the Browser object as Activate we can use it just like we would use it for a Window object:

  1. Browser("title:=Relevant Codes.*").Activate  


BrowserActivate can be extended to maximize and minimize a browser window as well. The only extra statement to be included in the function would be the maximize and minimize methods of the window object.

MINIMIZE BROWSERS

  1.   
  1. FUNCTION BROWSERMINIMIZE(OBJECT)  
  2.     DIM HWND  
  3.    
  4.     HWND = OBJECT.GETROPROPERTY("HWND")  
  5.    
  6.     ON ERROR RESUME NEXT  
  7.         WINDOW("HWND:=" & HWND).ACTIVATE  
  8.    
  9.         IF ERR.NUMBER <> 0 THEN  
  10.             HWND = BROWSER("HWND:=" & HWND).OBJECT.HWND  
  11.             WINDOW("HWND:=" & HWND).ACTIVATE  
  12.             ERR.CLEAR  
  13.         END IF  
  14.    
  15.         WINDOW("HWND:=" & HWND).MINIMIZE  
  16.     ON ERROR GOTO 0  
  17. END FUNCTION  
  18.    
  19. REGISTERUSERFUNC "BROWSER""MINIMIZE""BROWSERMINIMIZE"  




MAXIMIZE BROWSERS

  1. Function BrowserMaximize(Object)  
  2.     Dim hWnd  
  3.    
  4.     hWnd = Object.GetROProperty("hwnd")  
  5.    
  6.     On Error Resume Next  
  7.         Window("hwnd:=" & hWnd).Activate  
  8.    
  9.         If Err.Number <> 0 Then  
  10.             hWnd = Browser("hwnd:=" & hWnd).Object.hWnd  
  11.             Window("hwnd:=" & hWnd).Activate  
  12.             Err.Clear  
  13.         End If  
  14.    
  15.         Window("hwnd:=" & hWnd).Maximize  
  16.     On Error Goto 0  
  17. End Function  
  18.    
  19. RegisterUserFunc "Browser""Maximize""BrowserMaximize"  



If you would like to use the above 3 methods through a single function or class, they can be coupled together through Execute statements or through If-Then or Switch-Case blocks. Happy reading!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值