|
| 1 | +import contextlib |
| 2 | +import time |
1 | 3 | import unittest |
2 | 4 | from ctypes import POINTER |
3 | 5 |
|
4 | 6 | import comtypes |
| 7 | +from comtypes import GUID |
5 | 8 | from comtypes.client import CreateObject, GetModule |
6 | 9 |
|
7 | | -GetModule("oleacc.dll") |
8 | | -from comtypes.gen.Accessibility import IAccessible |
| 10 | +with contextlib.redirect_stdout(None): # supress warnings |
| 11 | + GetModule("mshtml.tlb") |
| 12 | +import comtypes.gen.MSHTML as mshtml |
9 | 13 |
|
| 14 | +SID_SHTMLEditServices = GUID("{3050F7F9-98B5-11CF-BB82-00AA00BDCE0B}") |
10 | 15 |
|
11 | | -@unittest.skip( |
12 | | - "This IE test is not working. We need to move it to using some other win32 API." |
13 | | -) |
14 | | -class TestCase(unittest.TestCase): |
15 | | - def setUp(self): |
16 | | - self.ie = CreateObject("InternetExplorer.application") |
17 | | - |
18 | | - def tearDown(self): |
19 | | - self.ie.Quit() |
20 | | - del self.ie |
21 | 16 |
|
| 17 | +class TestCase(unittest.TestCase): |
22 | 18 | def test(self): |
23 | | - ie = self.ie |
24 | | - ie.navigate2("about:blank", 0) |
25 | | - sp = ie.Document.Body.QueryInterface(comtypes.IServiceProvider) |
26 | | - pacc = sp.QueryService(IAccessible._iid_, IAccessible) |
27 | | - self.assertEqual(type(pacc), POINTER(IAccessible)) |
| 19 | + doc = CreateObject(mshtml.HTMLDocument, interface=mshtml.IHTMLDocument2) |
| 20 | + doc.designMode = "On" |
| 21 | + doc.write("<html><body><div id='test'>Hello</div></body></html>") |
| 22 | + doc.close() |
| 23 | + while doc.readyState != "complete": |
| 24 | + time.sleep(0.01) |
| 25 | + sp = doc.QueryInterface(comtypes.IServiceProvider) |
| 26 | + # This behavior is described in Microsoft documentation: |
| 27 | + # https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa704048(v=vs.85) |
| 28 | + es = sp.QueryService(SID_SHTMLEditServices, mshtml.IHTMLEditServices) |
| 29 | + self.assertIsInstance(es, POINTER(mshtml.IHTMLEditServices)) |
| 30 | + mc = doc.QueryInterface(mshtml.IMarkupContainer) |
| 31 | + ss = es.GetSelectionServices(mc) |
| 32 | + # QueryInterface for `IHTMLDocument3` to access `getElementById`. |
| 33 | + element = doc.QueryInterface(mshtml.IHTMLDocument3).getElementById("test") |
| 34 | + self.assertEqual(element.innerHTML, "Hello") |
| 35 | + # MarkupPointer related tests: |
| 36 | + ms = doc.QueryInterface(mshtml.IMarkupServices) |
| 37 | + p_start = ms.CreateMarkupPointer() |
| 38 | + p_end = ms.CreateMarkupPointer() |
| 39 | + # QueryInterface for `IHTMLBodyElement` to access `createTextRange`. |
| 40 | + rng = doc.body.QueryInterface(mshtml.IHTMLBodyElement).createTextRange() |
| 41 | + rng.moveToElementText(element) |
| 42 | + ms.MovePointersToRange(rng, p_start, p_end) |
| 43 | + self.assertTrue(p_start.IsLeftOf(p_end)) |
| 44 | + self.assertTrue(p_end.IsRightOf(p_start)) |
| 45 | + self.assertFalse(p_start.IsEqualTo(p_end)) |
| 46 | + seg = ss.AddSegment(p_start, p_end) |
| 47 | + q_start = ms.CreateMarkupPointer() |
| 48 | + q_end = ms.CreateMarkupPointer() |
| 49 | + self.assertFalse(p_start.IsEqualTo(q_start)) |
| 50 | + self.assertFalse(p_end.IsEqualTo(q_end)) |
| 51 | + seg.GetPointers(q_start, q_end) |
| 52 | + self.assertTrue(p_start.IsEqualTo(q_start)) |
| 53 | + self.assertTrue(p_end.IsEqualTo(q_end)) |
| 54 | + ss.RemoveSegment(seg) |
| 55 | + # Verify state changes of `p_start` and `p_end`. |
| 56 | + p_start.MoveToPointer(p_end) |
| 57 | + self.assertTrue(p_start.IsEqualTo(p_end)) |
28 | 58 |
|
29 | 59 |
|
30 | 60 | if __name__ == "__main__": |
|
0 commit comments