26 Working with MS IE browser
Concept: QTP can control an IE window using test object, butalso use IE COM interface. In this chapter we will work on how to work with theIE COM APIs, automation of web pages using HTML DOM and change configuration ofIE through the system registry.
26.1 Launching IE
'Method 1: USE QTP SystemUtil.Run "iexplore.exe" |
'Method 2 : Use Shell Set oShell = CreateObject("WScript.Shell") oShell.Run "iexplore.exe" |
'Method 3: USE IE COM Set oIEApp = CreateObject("InternetExplorer.Application") oIEApp.Visible = True oIEApp.Navigate2 "www.baidu.com" |
Get the IE Window’s handle to QTP test object(经过实验,oIEApp.HWND和实际IE的HWND的值不等,所以找不到对象browser;其他的属性如width值也不等)
iehwnd = oIEApp.HWND Browser("hwnd:="& iehwnd).Close |
26.2 Useful IE COM Methods and Properties:
Method Name | Description |
Navigate2 | Opens a specified URL in the browser. |
Stop | Stops navigation in the browser. |
Refresh | Refreshes the current page. |
Quit | Close the browser |
GoBack | Navigates one page back |
GoHome | Navigates to homepage |
GoForward | Navigates one page forward |
GoSearch | Open the search page |
Property Name | Type | Description |
Left | Long | Left position of the window |
Top | Long | Top position of the window |
Width | Long | Width of the window |
Height | Long | Height of the window |
AddressBar | Boolean | Controls whether address bar is shown |
FullScreen | Boolean | Maximizes window and turn off bars |
LocationName | String | Gets the short name of the current URL/filev |
LocationURL | String | Gets the full name of the current URL |
Path | String | Return application path |
Resizable | Boolean | Controls if the window is resizable |
Slient | Boolean | Controls if the dialog box can be shown |
Type | String | Returns the type of contained document object |
Visible | Boolean | Determines if the application is visible or not |
Busy | Boolean | Query to see if something is still in process |
ReadyState | Long | Have one of the following value: READYSTATE_UNINITIALIZED; READYSTATE_LOADING; READYSTATE_LOADED; READYSTATE_INTERACTIVE; READYSTATE_COMPLETE. |
E.g. page synchronization(not work when page have frames):
Do DoEvent Loop while IE.Busy = True |
26.3 Enumerating all IE windows
'Function to enumerate all open IE windows Function EnumerateIE() 'This is needed in case an IE window is closed 'while this function is executing On Error Resume Next
'Create a dictionary for returning the collection of open IE 'windows Set EnumerateIE = CreateObject("Scripting.Dictionary")
'Get the windows shell application Set oWinShell = CreateObject("Shell.Application") 'Get a collection all open explore windows, 'Microsoft Explorer + Internet Explorer Set allWindows = oWinShell.Windows
'Loop through each window For Each oWindow In allWindows 'Check if it is internet explorer process then only add it If InStr(1, oWindow.FullName, "iexplore.exe",vbTextCompare) Then EnumerateIE.Add oWindow.hwnd, oWindow End if Next End Function |
26.4 Close all the IE windows
SystemUtil.CloseProcessByName("iexplore.exe") |
或者用上面的functionEnumerateIE().
Set allIE = EnumerateIE()
'Lets quit all the IE For Each oIE In allIE.Items oIE.quit Next |
26.5 Finding an IE window
Set allIE = EnumerateIE()
'Lets quit all the IE For Each oIE In allIE.Items If oIE.hwnd = iehWnd Then 'We found the associated IE window. Return its COM interface object Set GetIECOMByhWnd = oIE End If Next |
Similarly we can search for IE using its RUL:
Set allIE = EnumerateIE()
'Lets quit all the IE For Each oIE In allIE.Items If InStr(oIE.LocationURL, ieURL) Then 'We found the associated IE window. Return its COM interface object Set GetIECOMByURL = oIE End If Next |
26.6 Getting Web Page DOM
Set oIE = CreateObject("InternetExplorer.Application")
'Make the application visible oIE.Visible = True
'Navigate to a web site oIE.Navigate2 "www.mywebsite.com" While oIE.Busy: Wend
'Get DOM document object Set oDOMDoc = oIE.Document ‘The following is the same as Chapter 19: html DOM. |
26.7 Accessing webpage script variables: Using Chapter 19: html DOM.
26.8 Custom Browser Application:
Note: 有的应用程序把Browser嵌在里面,就要把这个程序注册为Browser。
To identify embedded browser we first need to register, useQTP tool: Start MenuàAll Programs àQTP àTools àRegister New Browser Control àInput the application full path and registered.
26.9 Using a Browser Object as a Window Object
Note: 有时候你要用Window的自带方法,就把Browser转换为Window。
‘Use windows handle property (hwnd) hwnd = Brwser(“”).GetROProperty(“hwnd”) Set oBrowserWindow = Window(“hwnd:= &hwnd”) ‘now you can use any window method |
26.10 Checking for Broken images on a web page
Note: Broken image has following property values:
--fileSize = -1
--readyState = “uninitialized”
思路: 利用htmlDom 得到image的属性值, 和上面的value相比。
Public Function CheckBrokenImages(oPage) pgTitle = oPage.GetROProperty("title") Set allImages = oPage.object.Images iCount = allImages.Length - 1
For i = 0 To iCount Set curIMG = allImages.item(i) sHTML = "HTML = " & curIMG.outerHTML If curIMG.fileSize = -1 Then 'The image did not load Reporter.ReportEvent micFail, pgTitle & "Broken Image", sHTML Else Reporter.ReportEvent micPass, pgTitle & "Valid Image", sHTML End if Next End Function |
26.11 IE Popup Dialogs
Note: two methods to handle: 1st method is towrite code which checks for various dialogs and take nessary action to skipthem; 2nd method to change the IE setting manually ofprogrammatically to disable display of these popups.
26.11-1 Disabling IE dialogs:
Note: Not good. The appoarch take default action on thedialog, if dialog default “No” to save file, you will not click “Yes” to savefiles from IE.
Browser(“..”).Object.Silent = True |
26.11-2 Changing IE setting using the Registry:
思路:这中办法在项目中经常应用,可以改变windows注册信息。 不过要小心点使用,而且建议改完后再恢复原值。
'Create a Window's shell object Set WshShell = WScript.CreateObject("WScript.Shell")
'Write to the registry WshShell.RegWrite "HKCU\Software\Microsoft\TestThis", "TESTValue", "REG_SZ"
'Read from the Registry Msgbox WshShell.RegRead("HKCU\Software\Microsoft\TestThis")
'Delete a registry key Msgbox WshShell.RegDelete("HKCU\Software\Microsoft\TestThis") |
26.11-3 Popup blocked
Dialog infor: Pop-up blocked. To see this popup or additionalaptions click here…….
1st method:
popupKeyPath = “HKCU\Software\Microsoft\Internet Explorer\New Windows\PopupMgr” ‘Disable IE popup blocked WshShell.RegWrite popupKeyPath, “no”, “REG_SZ” ‘Able IE popup blocked WshShell.RegWrite popupKeyPath, “yes”, “REG_SZ” |
2nd method: Add this website to the “popup allowed” list byfollowing code:
sDomain= “www.google.com” popupAllowKey = “HKCU\Software\Microsoft\Internet Explorer\New Windows\Allow\” WshShell.RegWrite popupAllowKey & sDomain, 0 , “REG_BINARY” |
26.11-4 Disable Sript Error dialog
Note: QTP error dialogs, e.g. runtime error dialogs:
‘Disable all script debug dialogs WshShell.RegWrite “HKCU\Software\Microsoft\Internet Explorer\Main\Disable Script Debugger”, “yes”, “REG_SZ” WshShell.RegWrite “HKCU\Software\Microsoft\Internet Explorer\Main\Disable Script DebuggerIE”, “yes”, “REG_SZ” WshShell.RegWrite “HKCU\Software\Microsoft\Internet Explorer\Main\Error Dlg Displayed On Every Error”, “no”, “REG_SZ” |
26.11-5 Security Alert –Redirection popup
Note: Infor: You areredirected to a connection that is not secure …….
‘Disable the dialogs WshShell.RegWrite “HKCU\Software\Microsoft\Windows\CurrentVersion\Internet_ Setting\WarnOnPosRedirect”, “no”, “REG_SZ” |
26.11-6 Security Alert –Certificate warning
Note: Infor: Informationyou exchange with this site cannot be viewed or changed by others. However …….
‘Disable the dialogs WshShell.RegWrite “HKCU\Software\Microsoft\Windows\CurrentVersion\Internet_ Setting\”, “no”, “REG_SZ” |
26.11-7 Security Alert –Secure Connection
Note: Infor: You areabout to view pages over a secure connection……..
‘Disable the dialogs WshShell.RegWrite “HKCU HKCU\Software\Microsoft\Windows\CurrentVersion\Internet_ Setting\WarnOnPost”, “no”, “REG_SZ” |
26.11-8 Security Information –Secure and non-secure item
Note1: Infor: Thispage contains both secure and nonsecure items…….
Note2: Security information settings are stored in a zone. Thereare five different zones in IE setting:
-- My Computer -- Local Intranet Zone -- Trusted sites Zone -- Internet Zone -- Restricted Sites Zone |
Each setting belonging to a above zone has its correspondingcode. Pls find codes in MS KB article 182569 (http://support.microsoft.com/kb/182569)
'Various zone codes 'Value Setting '------------------------------ '0 My Computer '1 Local Intranet Zone '2 Trusted sites Zone '3 Internet Zone '4 Restricted Sites Zone
'0 - enabled, 1 - prompt, 3 - disabled newValue = 0
'Disable the Security information secure non-secure popup in all the Zones SettingCode = "1609"
'Change the setting for all 5 zones For i = 0 To 4 ChangeIEZoneSetting CStr(i), SettingCode, newValue Next
'Function to change specified setting in a specified zone Function ChangeIEZoneSetting(ByVal ZoneCode, ByVal SettingCode, ByVal newValue) Dim WshShell Set WshShell = CreateObject("WScript.Shell")
keyPath = "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones" WshShell.RegWrite keyPath & "\" & ZoneCode & "\" & SettingCode, newValue, "REG_DWORD" End Function |
26.11-9 Active content/ Java Script prompt
Note: Infor: To helpprotect your security, Internet Explorer has restricted this file……
'Various zone codes 'Value Setting '------------------------------ '0 My Computer '1 Local Intranet Zone '2 Trusted sites Zone '3 Internet Zone '4 Restricted Sites Zone
'0 - enabled, 1 - prompt, 3 - disabled newValue = 0
'Disable all the Zones SettingCode = "1400"
'Change the setting for all 5 zones For i = 0 To 4 ChangeIEZoneSetting CStr(i), SettingCode, newValue Next
|
26.11-10 File Download -- Information Bar
Note: Infor: To helpprotect your security, Internet Explorer has blocked this site from downloading files ……
'Various zone codes 'Value Setting '------------------------------ '0 My Computer '1 Local Intranet Zone '2 Trusted sites Zone '3 Internet Zone '4 Restricted Sites Zone
'0 - enabled, 1 - prompt, 3 - disabled newValue = 0
'Disable all the Zones SettingCode = "2200"
'Change the setting for all 5 zones For i = 0 To 4 ChangeIEZoneSetting CStr(i), SettingCode, newValue Next
|
26.12 Handing popup dialog by using a code:
思考: 就是通过DP拿到对象。
Public Function CleanBrowserPopups(oBrw) 'Get the popup dialog Set oPopDlg = oBrw.Dialog("ispopupwindow:=True")
CleanBrowserPopups = False If oPopDlg .Exist(0) Then 'There is a popup dialog 'Get its title sPopTitle = oPopDlg.GetROProperty("title")
Select Case LCase(sPopTitle) Case "security information", "security alert", "security warning" "error" 'Either a OK button would be there or a yes button 'Use a regular expression to cater both in a single statement oPopDlg.WinButton("text:=(&Yes|OK)").Click CleanBrowserPopups = True Reporter.ReportEvent micPass, sPopTitle, "Closed IE dialog popup" Case "enter network password" 'CODE to handle enter network password widows 'this is application to Windows 2000 OS Case Else If Left(LCase(sPopTitle),10) = "connect to" Then 'CODE to handle enter network password windows 'this is application to Windows XP OS Else MsgBox "Unknown dialog box - " & sPopTitle End If End Select End If End Function
'We can register this as user defined user method RegisterUserFunc "Browser", "CleanBrowserPopups", "CleanBrowserPopups"
If Browser("creationtime:=0").CleanBrowserPopups = True Then MsgBox "Cleaned all dialogs in the browser" End if |
26.12 File Dowloading –Security Warning popup
思考: 就是通过DP拿到对象。
There are 3 dialogs occur:
--File Download – SecurityWarning
--Save As
--File Download
'Function to download a file from IE Public Function IEDownloadFile(ByVal oDlg, ByVal sFileName, ByVal bWaitComplete) sPopup = "ispopupwindow:=true" sBtn = "nativeclass:=button"
'We will use window handle because the title will change later 'From "File Download" to "Download Complete", but handle will remain 'the same hWnd = oDlg.GetROProperty("hwnd")
With Dialog("hwnd:=" & hwnd) 'Get the value of the close on download complete checkbox sCloseAfterDownload=.WinCheckBox(sBtn,"text:=&Close.*").GetROProperty("checked")
'Check if the security wanring dialog is present If .Dialog(sPopup).Exist(0) Then 'Two click events needed sometimes as the first one does not do anything .Dialog(sPopup).WinButton("text:=&Save",sBtn).Click 'Make the second click option in case the first one works 'And to use OptionalStep we need to use the Complete statement OptionalStep.Dialog("hwnd:=" & hwnd).Dialog(sPopup).WinButton("text:=&Save",sBtn).Click End if
'Enter the complete file name in the save windo hwnd=.Dialog(sPopup).GetROProperty("hwnd") .Dialog(sPopup).Activate .Dialog(sPopup).WinEdit("nativeclass:=Edit","attached text:=File &name\:").Set "" .Dialog(sPopup).WinEdit("nativeclass:=Edit","attached text:=File &name\:").Type sFileName .Dialog(sPopup).WinButton(sBtn,"text:=&Save").Click
'Check if the file already exist dialog has appeared 'We get the Save As dialog handle and then check for the 'File allready exist. Going by the normal way we would have used 'Dialog("hwnd:=" & hwnd).Dialog(sPopup).Dialog(sPopup).Exist(0) 'Which is expected to work but it does not. So to reduce one level 'of hierarchy we take the hWnd and use it If Dialog("hwnd:=" & hwnd).Dialog(sPopup).Exist(0) Then Dialog("hwnd:=" & hwnd).Dialog(sPopup).WinButton(sBtn,"text:=&Yes").Click End If
If bWaitComplete Then 'Once download is complete either the window is closed if the checkbox 'was checked or the Open Folder on the window will change get enabled While .Exist(0) and Not .WinButton("text:=Open &Folder").GetROProperty("enabled") Wait 1 Wend
'If the checkbox OFF then we need to close the dialog If sCloseAfterDownload = "OFF" then .Close End if End with End Function |
‘Use the above function Set oDlg = Dialog(“text:=File Download”, “Index:=0”) ‘Download file synchronously IEDownloadFile oDlg, “c:\Test.exe”, True ‘Download file asynchronously IEDownloadFile oDlg, “c:\Test.exe”, Fals |