Some software interfaces use Win32 windows to nest an IE control. With spy ++, only one Internet assumer_server control can be identified. Several common API functions cannot obtain the content in the IE control, and cannot operate the control in the control. Therefore, automation is troublesome. This article describes how to use python to obtain the ihtmldocument2 interface for automated testing.
Get ihtmldocument2 Interface
Reference: http://support.microsoft.com/kb/249232
Python implementationCodeAs follows: #! /Usr/bin/ENV Python
#Coding: UTF-8
_ Author __ = 'Coderzh'
ImportSys
# Important for Multithreading
SYS. coinit_flags = 0 # Pythoncom. coinit_multithreaded
Import Win32com
Import Win32com. Client
Import Win32gui
Import Win32con
Import Pythoncom
DefGetieserver (hwnd, ieserver ):
IfWin32gui. getclassname (hwnd)= 'Internet assumer_server':
Ieserver. append (hwnd)
If _ Name __ = ' _ Main __ ' :
# Pythoncom. coinitializeex (0) # not use this for Multithreading
Mainhwnd = Win32gui. findwindow ( ' Windowclass ' , ' Windowtitle ' )
If Mainhwnd:
Ieservers = []
Win32gui. enumchildwindows (mainhwnd, getieserver, ieservers)
If Len (ieservers) > 0:
Ieserver = Ieservers [0]
MSG = Win32gui. registerwindowmessage ( ' Wm_html_getobject ' )
RET, result = Win32gui. sendmessagetimeout (ieserver, MSG, 0, 0, win32con. smto_abortifhung, 1000 )
Ob = Pythoncom. objectfromlresult (result, pythoncom. iid_idispatch, 0)
Doc = Win32com. Client. Dynamic. Dispatch (OB)
PrintDoc. url
Doc. All ['ID']. Click ()
#Pythoncom. couninitialize ()Multithreading
Ihtmldocument2 is thread-safe and cannot be used in multiple threads by default. Otherwise, an exception is thrown. However, multithreading is required during the specific use process. The solution is in the above Code: #Important for Multithreading
SYS. coinit_flags=0#Pythoncom. coinit_multithreaded
This statement must be set at the beginning, and do not display calls to pythoncom. coinitializeex (0) and pythoncom. couninitialize ().
Reference: http://bytes.com/topic/python/answers/26897-multithreaded-com-server-problem
Ihtmldocument2 Interface
Ihtmldocument2 interface what methods can be queried http://msdn.microsoft.com/en-us/library/aa752574%28VS.85%29.aspx
It can basically meet the needs of automated testing. On this basis, it can encapsulate an easy-to-use automated UI testing framework.