Skip to content

Commit 48d886e

Browse files
committed
experimental: split exit method and logic for screenshots capture
1 parent afa4e2e commit 48d886e

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

test/e2e/driver/aut.py

+32-21
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,39 @@ def __enter__(self):
4747

4848
def __exit__(self, exc_type, exc_value, traceback):
4949
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()
7051
self.stop()
7152

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+
7283
def detach_context(self):
7384
if self.ctx is None:
7485
return
@@ -118,10 +129,10 @@ def startaut(self):
118129

119130
@allure.step('Close application')
120131
def stop(self):
132+
time.sleep(1) # Some time before stopping the app to capture screenshot.
121133
LOG.info('Stopping AUT: %s', self.path)
122134
self.detach_context()
123135
local_system.kill_process(self.pid)
124-
time.sleep(1) # FIXME: Implement waiting for process to actually exit.
125136

126137
@allure.step("Start and attach AUT")
127138
def launch(self) -> 'AUT':

0 commit comments

Comments
 (0)