55import subprocess
66import sys
77import time
8+ from typing import TypedDict
89
910import pywinctl
1011
1112
13+ class GetWindowKwargs (TypedDict ):
14+ title : str
15+ condition : int # TODO: Consider making pywinctl.Re an IntEnum
16+
17+
1218def test_basic ():
1319
1420 print ("PLATFORM:" , sys .platform )
@@ -26,52 +32,41 @@ def test_basic():
2632 print ()
2733
2834 if sys .platform == "win32" :
29- subprocess .Popen ('notepad' )
30- time .sleep (2 )
31-
32- testWindows = [pywinctl .getActiveWindow ()]
33- assert len (testWindows ) == 1
34-
35- npw = testWindows [0 ]
36- wait = True
37- timelap = 0.50
38-
39- basic_test (npw , wait , timelap )
40-
35+ process = "notepad"
36+ get_window_kwargs : GetWindowKwargs = {
37+ "title" : "Notepad" ,
38+ "condition" : pywinctl .Re .ENDSWITH ,
39+ }
4140 elif sys .platform == "linux" :
42- subprocess .Popen ('gedit' )
43- time .sleep (2 )
44-
45- testWindows = [pywinctl .getActiveWindow ()]
46- assert len (testWindows ) == 1
47-
48- npw = testWindows [0 ]
49- wait = True
50- timelap = 0.50
51-
52- basic_test (npw , wait , timelap )
53-
41+ process = "gedit"
42+ get_window_kwargs : GetWindowKwargs = {
43+ "title" : "gedit" ,
44+ "condition" : pywinctl .Re .ENDSWITH ,
45+ }
5446 elif sys .platform == "darwin" :
5547 if not pywinctl .checkPermissions (activate = True ):
5648 exit ()
57- subprocess .Popen (['touch' , 'test.py' ])
58- time .sleep (2 )
59- subprocess .Popen (['open' , '-a' , 'TextEdit' , 'test.py' ])
60- time .sleep (5 )
61-
62- testWindows = pywinctl .getWindowsWithTitle ('test.py' )
63- assert len (testWindows ) == 1
64-
65- npw = testWindows [0 ]
66- wait = True
67- timelap = 0.50
49+ process = ["open" , "-a" , "TextEdit" , __file__ ]
50+ get_window_kwargs : GetWindowKwargs = {
51+ "title" : "test_pywinctl.py" ,
52+ "condition" : pywinctl .Re .IS ,
53+ }
54+ else :
55+ raise NotImplementedError (
56+ "PyWinCtl currently does not support this platform. "
57+ + "If you have useful knowledge, please contribute! https://github.com/Kalmat/PyWinCtl"
58+ )
6859
69- basic_test (npw , wait , timelap )
70- subprocess .Popen (['rm' , 'test.py' ])
60+ subprocess .Popen (process )
7161
72- else :
73- raise NotImplementedError ('PyWinCtl currently does not support this platform. If you have useful knowledge, please contribute! https://github.com/Kalmat/pywinctl' )
62+ testWindows : list [pywinctl .Window ] = []
63+ deadline = time .time () + 15
64+ while not testWindows and time .time () < deadline :
65+ time .sleep (0.5 )
66+ testWindows = pywinctl .getWindowsWithTitle (** get_window_kwargs )
67+ assert len (testWindows ) == 1
7468
69+ basic_test (npw = testWindows [0 ], wait = True , timelap = 0.50 )
7570
7671def basic_test (npw : pywinctl .Window | None , wait : bool , timelap : float ):
7772 assert npw is not None
@@ -249,8 +244,9 @@ def activeCB(isActive):
249244 print ("PARENT INFO" )
250245 parent = npw .getParent ()
251246 if parent :
252- print ("WINDOW PARENT:" , parent , npw .isChild (parent ))
253- assert npw .isChild (parent )
247+ is_child = npw .isChild (parent )
248+ print ("WINDOW PARENT:" , parent , is_child )
249+ assert is_child
254250 children = npw .getChildren ()
255251 for child in children :
256252 if child and isinstance (child , int ):
0 commit comments