Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testExplorer.py 4.6KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # testExplorer -
  2. import os
  3. import time
  4. import pythoncom
  5. import win32api
  6. import win32com.client.dynamic
  7. import win32con
  8. import win32gui
  9. import winerror
  10. from win32com.client import Dispatch
  11. from win32com.test.util import CheckClean
  12. bVisibleEventFired = 0
  13. # These are errors we might see when this is run in automation (eg, on github)
  14. # Not sure exactly what -2125463506 is, but google shows it's a common error
  15. # possibly related to how IE is configured WRT site permissions etc.
  16. HRESULTS_IN_AUTOMATION = [-2125463506, winerror.MK_E_UNAVAILABLE]
  17. class ExplorerEvents:
  18. def OnVisible(self, visible):
  19. global bVisibleEventFired
  20. bVisibleEventFired = 1
  21. def TestExplorerEvents():
  22. global bVisibleEventFired
  23. try:
  24. iexplore = win32com.client.DispatchWithEvents(
  25. "InternetExplorer.Application", ExplorerEvents
  26. )
  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 [
  55. "TabWindowClass",
  56. "Shell DocObject View",
  57. "Internet Explorer_Server",
  58. ]:
  59. hwnd = win32gui.FindWindowEx(hwnd, 0, child_class, None)
  60. # ack - not working for markh on vista with IE8 (or maybe it is the
  61. # lack of the 'accessibility' components mentioned in Q249232)
  62. # either way - not working!
  63. return
  64. # But here is the point - once you have an 'Internet Explorer_Server',
  65. # you can send a message and use ObjectFromLresult to get it back.
  66. msg = win32gui.RegisterWindowMessage("WM_HTML_GETOBJECT")
  67. rc, result = win32gui.SendMessageTimeout(
  68. hwnd, msg, 0, 0, win32con.SMTO_ABORTIFHUNG, 1000
  69. )
  70. ob = pythoncom.ObjectFromLresult(result, pythoncom.IID_IDispatch, 0)
  71. doc = Dispatch(ob)
  72. # just to prove it works, set the background color of the document.
  73. for color in "red green blue orange white".split():
  74. doc.bgColor = color
  75. time.sleep(0.2)
  76. def TestExplorer(iexplore):
  77. if not iexplore.Visible:
  78. iexplore.Visible = -1
  79. filename = os.path.join(os.path.dirname(__file__), "..\\readme.html")
  80. iexplore.Navigate(win32api.GetFullPathName(filename))
  81. win32api.Sleep(1000)
  82. TestObjectFromWindow()
  83. win32api.Sleep(3000)
  84. try:
  85. iexplore.Quit()
  86. except (AttributeError, pythoncom.com_error):
  87. # User got sick of waiting :)
  88. pass
  89. def TestAll():
  90. try:
  91. try:
  92. try:
  93. iexplore = win32com.client.dynamic.Dispatch(
  94. "InternetExplorer.Application"
  95. )
  96. except pythoncom.com_error as exc:
  97. if exc.hresult not in HRESULTS_IN_AUTOMATION:
  98. raise
  99. print("IE appears to not be available, so skipping this test")
  100. return
  101. TestExplorer(iexplore)
  102. win32api.Sleep(1000)
  103. iexplore = None
  104. # Test IE events.
  105. TestExplorerEvents()
  106. # Give IE a chance to shutdown, else it can get upset on fast machines.
  107. time.sleep(2)
  108. # Note that the TextExplorerEvents will force makepy - hence
  109. # this gencache is really no longer needed.
  110. from win32com.client import gencache
  111. gencache.EnsureModule("{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1)
  112. iexplore = win32com.client.Dispatch("InternetExplorer.Application")
  113. TestExplorer(iexplore)
  114. except pythoncom.com_error as exc:
  115. if exc.hresult != winerror.RPC_E_DISCONNECTED: # user closed the app!
  116. raise
  117. finally:
  118. iexplore = None
  119. if __name__ == "__main__":
  120. TestAll()
  121. CheckClean()