From b82072f49aea3980aab6abedc0d833805ce14137 Mon Sep 17 00:00:00 2001 From: junkmd Date: Sat, 20 Dec 2025 08:20:13 +0900 Subject: [PATCH 1/5] test: Remove Internet Explorer dependency from `test_QueryService.py`. The `test_QueryService.py` has been changed to remove its dependency on `InternetExplorer.Application`. --- comtypes/test/test_QueryService.py | 37 ++++++++++++++++-------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/comtypes/test/test_QueryService.py b/comtypes/test/test_QueryService.py index c15a43634..eb8050a64 100644 --- a/comtypes/test/test_QueryService.py +++ b/comtypes/test/test_QueryService.py @@ -1,30 +1,33 @@ +import contextlib +import time import unittest from ctypes import POINTER import comtypes +from comtypes import GUID from comtypes.client import CreateObject, GetModule -GetModule("oleacc.dll") -from comtypes.gen.Accessibility import IAccessible +with contextlib.redirect_stdout(None): # supress warnings + GetModule("mshtml.tlb") +import comtypes.gen.MSHTML as mshtml +SID_SHTMLEditServices = GUID("{3050F7F9-98B5-11CF-BB82-00AA00BDCE0B}") -@unittest.skip( - "This IE test is not working. We need to move it to using some other win32 API." -) -class TestCase(unittest.TestCase): - def setUp(self): - self.ie = CreateObject("InternetExplorer.application") - - def tearDown(self): - self.ie.Quit() - del self.ie +class TestCase(unittest.TestCase): def test(self): - ie = self.ie - ie.navigate2("about:blank", 0) - sp = ie.Document.Body.QueryInterface(comtypes.IServiceProvider) - pacc = sp.QueryService(IAccessible._iid_, IAccessible) - self.assertEqual(type(pacc), POINTER(IAccessible)) + doc = CreateObject(mshtml.HTMLDocument, interface=mshtml.IHTMLDocument2) + doc.designMode = "On" + doc.write("
Hello
") + doc.close() + while doc.readyState != "complete": + time.sleep(0.01) + sp = doc.QueryInterface(comtypes.IServiceProvider) + es = sp.QueryService(SID_SHTMLEditServices, mshtml.IHTMLEditServices) + self.assertIsInstance(es, POINTER(mshtml.IHTMLEditServices)) + mc = doc.QueryInterface(mshtml.IMarkupContainer) + ss = es.GetSelectionServices(mc) + self.assertIsInstance(ss, POINTER(mshtml.ISelectionServices)) if __name__ == "__main__": From 44992d7f7fdb3618b6458cc38add0bf2deeafe42 Mon Sep 17 00:00:00 2001 From: junkmd Date: Sat, 20 Dec 2025 08:20:13 +0900 Subject: [PATCH 2/5] docs: Refine comment on obtaining `IHTMLEditServices` in test. --- comtypes/test/test_QueryService.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/comtypes/test/test_QueryService.py b/comtypes/test/test_QueryService.py index eb8050a64..b902e0dfc 100644 --- a/comtypes/test/test_QueryService.py +++ b/comtypes/test/test_QueryService.py @@ -23,6 +23,8 @@ def test(self): while doc.readyState != "complete": time.sleep(0.01) sp = doc.QueryInterface(comtypes.IServiceProvider) + # This behavior is described in Microsoft documentation: + # https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa704048(v=vs.85) es = sp.QueryService(SID_SHTMLEditServices, mshtml.IHTMLEditServices) self.assertIsInstance(es, POINTER(mshtml.IHTMLEditServices)) mc = doc.QueryInterface(mshtml.IMarkupContainer) From e69a39639a74aa509307da3f8a702b38cf8d056e Mon Sep 17 00:00:00 2001 From: junkmd Date: Sat, 20 Dec 2025 08:20:13 +0900 Subject: [PATCH 3/5] test: Enhance `ISelectionServices` and `IMarkupServices` testing. --- comtypes/test/test_QueryService.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/comtypes/test/test_QueryService.py b/comtypes/test/test_QueryService.py index b902e0dfc..c3d9fca60 100644 --- a/comtypes/test/test_QueryService.py +++ b/comtypes/test/test_QueryService.py @@ -29,7 +29,28 @@ def test(self): self.assertIsInstance(es, POINTER(mshtml.IHTMLEditServices)) mc = doc.QueryInterface(mshtml.IMarkupContainer) ss = es.GetSelectionServices(mc) - self.assertIsInstance(ss, POINTER(mshtml.ISelectionServices)) + # QueryInterface for `IHTMLDocument3` to access `getElementById`. + element = doc.QueryInterface(mshtml.IHTMLDocument3).getElementById("test") + # MarkupPointer related tests: + ms = doc.QueryInterface(mshtml.IMarkupServices) + p_start = ms.CreateMarkupPointer() + p_end = ms.CreateMarkupPointer() + # QueryInterface for `IHTMLBodyElement` to access `createTextRange`. + rng = doc.body.QueryInterface(mshtml.IHTMLBodyElement).createTextRange() + rng.moveToElementText(element) + ms.MovePointersToRange(rng, p_start, p_end) + self.assertTrue(p_start.IsLeftOf(p_end)) + self.assertTrue(p_end.IsRightOf(p_start)) + self.assertFalse(p_start.IsEqualTo(p_end)) + seg = ss.AddSegment(p_start, p_end) + q_start = ms.CreateMarkupPointer() + q_end = ms.CreateMarkupPointer() + self.assertFalse(p_start.IsEqualTo(q_start)) + self.assertFalse(p_end.IsEqualTo(q_end)) + seg.GetPointers(q_start, q_end) + self.assertTrue(p_start.IsEqualTo(q_start)) + self.assertTrue(p_end.IsEqualTo(q_end)) + ss.RemoveSegment(seg) if __name__ == "__main__": From 3daa9a5b2868e5b88e871b8728ce6f70f315b9ba Mon Sep 17 00:00:00 2001 From: junkmd Date: Sat, 20 Dec 2025 08:20:13 +0900 Subject: [PATCH 4/5] test: Add verification for `ISelectionServices` state changes. --- comtypes/test/test_QueryService.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/comtypes/test/test_QueryService.py b/comtypes/test/test_QueryService.py index c3d9fca60..692d151e6 100644 --- a/comtypes/test/test_QueryService.py +++ b/comtypes/test/test_QueryService.py @@ -51,6 +51,9 @@ def test(self): self.assertTrue(p_start.IsEqualTo(q_start)) self.assertTrue(p_end.IsEqualTo(q_end)) ss.RemoveSegment(seg) + # Verify state changes of `p_start` and `p_end`. + p_start.MoveToPointer(p_end) + self.assertTrue(p_start.IsEqualTo(p_end)) if __name__ == "__main__": From edc370245fd58c399449c817c0583a7f80debf6c Mon Sep 17 00:00:00 2001 From: junkmd Date: Sat, 20 Dec 2025 08:20:13 +0900 Subject: [PATCH 5/5] test: Verify `IHTMLDocument3.getElementById` content. --- comtypes/test/test_QueryService.py | 1 + 1 file changed, 1 insertion(+) diff --git a/comtypes/test/test_QueryService.py b/comtypes/test/test_QueryService.py index 692d151e6..debad8530 100644 --- a/comtypes/test/test_QueryService.py +++ b/comtypes/test/test_QueryService.py @@ -31,6 +31,7 @@ def test(self): ss = es.GetSelectionServices(mc) # QueryInterface for `IHTMLDocument3` to access `getElementById`. element = doc.QueryInterface(mshtml.IHTMLDocument3).getElementById("test") + self.assertEqual(element.innerHTML, "Hello") # MarkupPointer related tests: ms = doc.QueryInterface(mshtml.IMarkupServices) p_start = ms.CreateMarkupPointer()