testExplorer.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # testExplorer -
  2. import sys
  3. import os
  4. import win32com.client.dynamic
  5. from win32com.client import Dispatch
  6. import win32api
  7. import win32gui
  8. import win32con
  9. import winerror
  10. import glob
  11. import pythoncom
  12. import time
  13. from win32com.test.util import CheckClean
  14. bVisibleEventFired = 0
  15. class ExplorerEvents:
  16. def OnVisible(self, visible):
  17. global bVisibleEventFired
  18. bVisibleEventFired = 1
  19. def TestExplorerEvents():
  20. global bVisibleEventFired
  21. iexplore = win32com.client.DispatchWithEvents("InternetExplorer.Application", ExplorerEvents)
  22. iexplore.Visible = 1
  23. if not bVisibleEventFired:
  24. raise RuntimeError("The IE event did not appear to fire!")
  25. iexplore.Quit()
  26. iexplore = None
  27. bVisibleEventFired = 0
  28. ie = win32com.client.Dispatch("InternetExplorer.Application")
  29. ie_events = win32com.client.DispatchWithEvents(ie, ExplorerEvents)
  30. ie.Visible = 1
  31. if not bVisibleEventFired:
  32. raise RuntimeError("The IE event did not appear to fire!")
  33. ie.Quit()
  34. ie = None
  35. print "IE Event tests worked."
  36. def TestObjectFromWindow():
  37. # Check we can use ObjectFromLresult to get the COM object from the
  38. # HWND - see KB Q249232
  39. # Locating the HWND is different than the KB says...
  40. hwnd = win32gui.FindWindow('IEFrame', None)
  41. for child_class in ['TabWindowClass', 'Shell DocObject View',
  42. 'Internet Explorer_Server']:
  43. hwnd = win32gui.FindWindowEx(hwnd, 0, child_class, None)
  44. # ack - not working for markh on vista with IE8 (or maybe it is the
  45. # lack of the 'accessibility' components mentioned in Q249232)
  46. # either way - not working!
  47. return
  48. # But here is the point - once you have an 'Internet Explorer_Server',
  49. # you can send a message and use ObjectFromLresult to get it back.
  50. msg = win32gui.RegisterWindowMessage("WM_HTML_GETOBJECT")
  51. rc, result = win32gui.SendMessageTimeout(hwnd, msg, 0, 0, win32con.SMTO_ABORTIFHUNG, 1000)
  52. ob = pythoncom.ObjectFromLresult(result, pythoncom.IID_IDispatch, 0)
  53. doc = Dispatch(ob)
  54. # just to prove it works, set the background color of the document.
  55. for color in "red green blue orange white".split():
  56. doc.bgColor = color
  57. time.sleep(0.2)
  58. def TestExplorer(iexplore):
  59. if not iexplore.Visible: iexplore.Visible = -1
  60. iexplore.Navigate(win32api.GetFullPathName('..\\readme.htm'))
  61. win32api.Sleep(1000)
  62. TestObjectFromWindow()
  63. win32api.Sleep(3000)
  64. try:
  65. iexplore.Quit()
  66. except (AttributeError, pythoncom.com_error):
  67. # User got sick of waiting :)
  68. pass
  69. def TestAll():
  70. try:
  71. try:
  72. iexplore = win32com.client.dynamic.Dispatch("InternetExplorer.Application")
  73. TestExplorer(iexplore)
  74. win32api.Sleep(1000)
  75. iexplore = None
  76. # Test IE events.
  77. TestExplorerEvents()
  78. # Give IE a chance to shutdown, else it can get upset on fast machines.
  79. time.sleep(2)
  80. # Note that the TextExplorerEvents will force makepy - hence
  81. # this gencache is really no longer needed.
  82. from win32com.client import gencache
  83. gencache.EnsureModule("{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1)
  84. iexplore = win32com.client.Dispatch("InternetExplorer.Application")
  85. TestExplorer(iexplore)
  86. except pythoncom.com_error, exc:
  87. if exc.hresult!=winerror.RPC_E_DISCONNECTED: # user closed the app!
  88. raise
  89. finally:
  90. iexplore = None
  91. if __name__=='__main__':
  92. TestAll()
  93. CheckClean()