testExplorer.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. # These are errors we might see when this is run in automation (eg, on github)
  16. # Not sure exactly what -2125463506 is, but google shows it's a common error
  17. # possibly related to how IE is configured WRT site permissions etc.
  18. HRESULTS_IN_AUTOMATION = [-2125463506, winerror.MK_E_UNAVAILABLE]
  19. class ExplorerEvents:
  20. def OnVisible(self, visible):
  21. global bVisibleEventFired
  22. bVisibleEventFired = 1
  23. def TestExplorerEvents():
  24. global bVisibleEventFired
  25. try:
  26. iexplore = win32com.client.DispatchWithEvents("InternetExplorer.Application", ExplorerEvents)
  27. except pythoncom.com_error as exc:
  28. # In automation we see this error trying to connect to events
  29. # It's a little surprising that the non-event tests seem to work, but
  30. # whatever...
  31. if exc.hresult not in HRESULTS_IN_AUTOMATION:
  32. raise
  33. print("IE events appear to not be available, so skipping this test")
  34. return
  35. iexplore.Visible = 1
  36. if not bVisibleEventFired:
  37. raise RuntimeError("The IE event did not appear to fire!")
  38. iexplore.Quit()
  39. iexplore = None
  40. bVisibleEventFired = 0
  41. ie = win32com.client.Dispatch("InternetExplorer.Application")
  42. ie_events = win32com.client.DispatchWithEvents(ie, ExplorerEvents)
  43. ie.Visible = 1
  44. if not bVisibleEventFired:
  45. raise RuntimeError("The IE event did not appear to fire!")
  46. ie.Quit()
  47. ie = None
  48. print("IE Event tests worked.")
  49. def TestObjectFromWindow():
  50. # Check we can use ObjectFromLresult to get the COM object from the
  51. # HWND - see KB Q249232
  52. # Locating the HWND is different than the KB says...
  53. hwnd = win32gui.FindWindow('IEFrame', None)
  54. for child_class in ['TabWindowClass', 'Shell DocObject View',
  55. 'Internet Explorer_Server']:
  56. hwnd = win32gui.FindWindowEx(hwnd, 0, child_class, None)
  57. # ack - not working for markh on vista with IE8 (or maybe it is the
  58. # lack of the 'accessibility' components mentioned in Q249232)
  59. # either way - not working!
  60. return
  61. # But here is the point - once you have an 'Internet Explorer_Server',
  62. # you can send a message and use ObjectFromLresult to get it back.
  63. msg = win32gui.RegisterWindowMessage("WM_HTML_GETOBJECT")
  64. rc, result = win32gui.SendMessageTimeout(hwnd, msg, 0, 0, win32con.SMTO_ABORTIFHUNG, 1000)
  65. ob = pythoncom.ObjectFromLresult(result, pythoncom.IID_IDispatch, 0)
  66. doc = Dispatch(ob)
  67. # just to prove it works, set the background color of the document.
  68. for color in "red green blue orange white".split():
  69. doc.bgColor = color
  70. time.sleep(0.2)
  71. def TestExplorer(iexplore):
  72. if not iexplore.Visible: iexplore.Visible = -1
  73. filename = os.path.join(os.path.dirname(__file__), '..\\readme.htm')
  74. iexplore.Navigate(win32api.GetFullPathName(filename))
  75. win32api.Sleep(1000)
  76. TestObjectFromWindow()
  77. win32api.Sleep(3000)
  78. try:
  79. iexplore.Quit()
  80. except (AttributeError, pythoncom.com_error):
  81. # User got sick of waiting :)
  82. pass
  83. def TestAll():
  84. try:
  85. try:
  86. try:
  87. iexplore = win32com.client.dynamic.Dispatch("InternetExplorer.Application")
  88. except pythoncom.com_error as exc:
  89. if exc.hresult not in HRESULTS_IN_AUTOMATION:
  90. raise
  91. print("IE appears to not be available, so skipping this test")
  92. return
  93. TestExplorer(iexplore)
  94. win32api.Sleep(1000)
  95. iexplore = None
  96. # Test IE events.
  97. TestExplorerEvents()
  98. # Give IE a chance to shutdown, else it can get upset on fast machines.
  99. time.sleep(2)
  100. # Note that the TextExplorerEvents will force makepy - hence
  101. # this gencache is really no longer needed.
  102. from win32com.client import gencache
  103. gencache.EnsureModule("{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1)
  104. iexplore = win32com.client.Dispatch("InternetExplorer.Application")
  105. TestExplorer(iexplore)
  106. except pythoncom.com_error as exc:
  107. if exc.hresult!=winerror.RPC_E_DISCONNECTED: # user closed the app!
  108. raise
  109. finally:
  110. iexplore = None
  111. if __name__=='__main__':
  112. TestAll()
  113. CheckClean()