@@ -47,28 +47,39 @@ def __enter__(self):
47
47
48
48
def __exit__ (self , exc_type , exc_value , traceback ):
49
49
if exc_type :
50
- try :
51
- self .attach ()
52
- driver .waitForObjectExists (statusDesktop_mainWindow ).setVisible (True )
53
- configs .testpath .TEST .mkdir (parents = True , exist_ok = True )
54
- screenshot = configs .testpath .TEST / f'{ self .aut_id } .png'
55
-
56
- rect = driver .object .globalBounds (driver .waitForObject (statusDesktop_mainWindow ))
57
- img = ImageGrab .grab (
58
- bbox = (rect .x , rect .y , rect .x + rect .width , rect .y + rect .height ),
59
- xdisplay = configs .system .DISPLAY if get_platform () == "Linux" else None )
60
- view = cv2 .cvtColor (np .array (img ), cv2 .COLOR_BGR2RGB )
61
- cv2 .imwrite (str (screenshot ), view )
62
-
63
- allure .attach (
64
- name = f'Screenshot on fail: { self .aut_id } ' ,
65
- body = screenshot .read_bytes (),
66
- attachment_type = allure .attachment_type .PNG )
67
- except Exception as err :
68
- LOG .error (err )
69
-
50
+ self ._capture_screenshot ()
70
51
self .stop ()
71
52
53
+ def _capture_screenshot (self ):
54
+ """Captures a screenshot of the main application window if it is still running."""
55
+ try :
56
+ if driver .isRunning (self .aut_id ): # Проверяем, работает ли приложение
57
+ main_window = driver .waitForObjectExists (statusDesktop_mainWindow , 500 )
58
+ if main_window :
59
+ rect = driver .object .globalBounds (main_window )
60
+ screenshot = configs .testpath .TEST / f'{ self .aut_id } .png'
61
+ configs .testpath .TEST .mkdir (parents = True , exist_ok = True )
62
+
63
+ img = ImageGrab .grab (
64
+ bbox = (rect .x , rect .y , rect .x + rect .width , rect .y + rect .height ),
65
+ xdisplay = configs .system .DISPLAY if get_platform () == "Linux" else None
66
+ )
67
+ view = cv2 .cvtColor (np .array (img ), cv2 .COLOR_BGR2RGB )
68
+ cv2 .imwrite (str (screenshot ), view )
69
+
70
+ allure .attach (
71
+ name = f'Screenshot on fail: { self .aut_id } ' ,
72
+ body = screenshot .read_bytes (),
73
+ attachment_type = allure .attachment_type .PNG
74
+ )
75
+ LOG .info (f"Screenshot saved at { screenshot } " )
76
+ else :
77
+ LOG .warning ("Main window not found; skipping screenshot." )
78
+ else :
79
+ LOG .warning ("Application is no longer running; skipping screenshot." )
80
+ except Exception as err :
81
+ LOG .error (f"Failed to capture screenshot: { err } " )
82
+
72
83
def detach_context (self ):
73
84
if self .ctx is None :
74
85
return
@@ -118,10 +129,10 @@ def startaut(self):
118
129
119
130
@allure .step ('Close application' )
120
131
def stop (self ):
132
+ time .sleep (1 ) # Some time before stopping the app to capture screenshot.
121
133
LOG .info ('Stopping AUT: %s' , self .path )
122
134
self .detach_context ()
123
135
local_system .kill_process (self .pid )
124
- time .sleep (1 ) # FIXME: Implement waiting for process to actually exit.
125
136
126
137
@allure .step ("Start and attach AUT" )
127
138
def launch (self ) -> 'AUT' :
0 commit comments