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

来源:互联网 发布:比价神器 软件下载 编辑:程序博客网 时间:2024/05/17 09:05

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

[vb] view plaincopyprint?
  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:

[vb] view plaincopyprint?
  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

[VB] VIEW PLAINCOPYPRINT?
  1.   
[VB] VIEW PLAINCOPYPRINT?
  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

[vb] view plaincopyprint?
  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
原创粉丝点击