Python operation ihtmldocument2 for automated testing)

Source: Internet
Author: User
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
The corresponding Python implementation code is as follows :#! /Usr/bin/ENV Python
# Coding: UTF-8

_ Author _ = 'coderzh'

Import sys

# Important for Multithreading
SYS. coinit_flags = 0 # pythoncom. coinit_multithreaded

Import win32com
Import win32com. Client
Import win32gui
Import win32con
Import pythoncom

Def getieserver (hwnd, ieserver ):
If win32gui. 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)

Print Doc. url
Doc. All ['id']. Click ()

# Pythoncom. couninitialize () multi-thread operation

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.

Author: coderzh (coderzh's technology blog-Blog)
Weibo: http://t.sina.com.cn/coderzh
Source: http://coderzh.cnblogs.com
The copyright of this article is owned by me. You are welcome to repost it. However, you must keep this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be entitled to pursue legal liability.

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
The corresponding Python implementation code is as follows :#! /Usr/bin/ENV Python
# Coding: UTF-8

_ Author _ = 'coderzh'

Import sys

# Important for Multithreading
SYS. coinit_flags = 0 # pythoncom. coinit_multithreaded

Import win32com
Import win32com. Client
Import win32gui
Import win32con
Import pythoncom

Def getieserver (hwnd, ieserver ):
If win32gui. 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)

Print Doc. url
Doc. All ['id']. Click ()

# Pythoncom. couninitialize () multi-thread operation

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.