@@ -21,6 +21,41 @@ def _find_button(parent, text):
2121 return None
2222
2323
24+ def _capture_window (window , output_path ):
25+ try :
26+ window .deiconify ()
27+ window .lift ()
28+ window .attributes ("-topmost" , True )
29+ window .focus_force ()
30+ window .update_idletasks ()
31+ window .update ()
32+ window .after (250 )
33+ window .update ()
34+
35+ left = window .winfo_rootx ()
36+ top = window .winfo_rooty ()
37+ right = left + window .winfo_width ()
38+ bottom = top + window .winfo_height ()
39+
40+ grab_options = {}
41+ if "DISPLAY" in os .environ :
42+ grab_options ["xdisplay" ] = os .environ ["DISPLAY" ]
43+
44+ screenshot = ImageGrab .grab (
45+ bbox = (left , top , right , bottom ),
46+ ** grab_options ,
47+ )
48+ screenshot .save (output_path )
49+ window .attributes ("-topmost" , False )
50+ except Exception as error :
51+ warnings .warn (
52+ f"Unable to capture { output_path .name } on { sys .platform } : "
53+ f"{ error } " ,
54+ RuntimeWarning ,
55+ stacklevel = 1 ,
56+ )
57+
58+
2459def test_gui_opens (tmp_path ):
2560 window = MainWindow ()
2661
@@ -43,6 +78,11 @@ def test_gui_opens(tmp_path):
4378 assert advanced_window .winfo_exists ()
4479 assert advanced_window .title () == "Advanced Preferences"
4580
81+ _capture_window (
82+ advanced_window ,
83+ tmp_path / "dna2graph_advanced_preferences.png" ,
84+ )
85+
4686 cancel_button = _find_button (advanced_window , "Cancel" )
4787 assert cancel_button is not None
4888
@@ -51,38 +91,6 @@ def test_gui_opens(tmp_path):
5191 window .update ()
5292
5393 assert not advanced_window .winfo_exists ()
54-
55- try :
56- window .deiconify ()
57- window .lift ()
58- window .attributes ("-topmost" , True )
59- window .focus_force ()
60- window .update_idletasks ()
61- window .update ()
62- window .after (250 )
63- window .update ()
64-
65- left = window .winfo_rootx ()
66- top = window .winfo_rooty ()
67- right = left + window .winfo_width ()
68- bottom = top + window .winfo_height ()
69-
70- grab_options = {}
71- if "DISPLAY" in os .environ :
72- grab_options ["xdisplay" ] = os .environ ["DISPLAY" ]
73-
74- screenshot = ImageGrab .grab (
75- bbox = (left , top , right , bottom ),
76- ** grab_options ,
77- )
78- screenshot .save (tmp_path / "dna2graph_gui.png" )
79- window .attributes ("-topmost" , False )
80- except Exception as error :
81- warnings .warn (
82- f"Unable to capture the GUI screenshot on { sys .platform } : "
83- f"{ error } " ,
84- RuntimeWarning ,
85- stacklevel = 1 ,
86- )
94+ _capture_window (window , tmp_path / "dna2graph_gui.png" )
8795 finally :
8896 window .destroy ()
0 commit comments