2023-08-04 18:27:03 +00:00
|
|
|
import allure
|
2023-11-30 11:42:17 +00:00
|
|
|
import logging
|
2023-11-07 17:26:07 +00:00
|
|
|
import cv2
|
|
|
|
import numpy as np
|
2023-08-04 18:27:03 +00:00
|
|
|
import squish
|
2023-10-19 12:26:05 +00:00
|
|
|
from PIL import ImageGrab
|
2023-08-04 18:27:03 +00:00
|
|
|
|
|
|
|
import configs
|
|
|
|
import driver
|
2023-11-30 11:42:17 +00:00
|
|
|
from os import path
|
|
|
|
from datetime import datetime
|
2023-10-23 11:53:11 +00:00
|
|
|
from configs.system import IS_LIN
|
2023-08-04 18:27:03 +00:00
|
|
|
from driver import context
|
|
|
|
from driver.server import SquishServer
|
2024-02-13 09:04:24 +00:00
|
|
|
from gui.objects_map.names import statusDesktop_mainWindow
|
2023-08-04 18:27:03 +00:00
|
|
|
from scripts.utils import system_path, local_system
|
2023-09-11 18:24:13 +00:00
|
|
|
from scripts.utils.system_path import SystemPath
|
2023-12-06 10:28:19 +00:00
|
|
|
from scripts.utils.wait_for_port import wait_for_port
|
2023-08-04 18:27:03 +00:00
|
|
|
|
2023-11-30 13:07:26 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
2023-09-28 12:44:13 +00:00
|
|
|
|
2023-08-04 18:27:03 +00:00
|
|
|
|
|
|
|
class AUT:
|
|
|
|
def __init__(
|
|
|
|
self,
|
2023-11-30 11:05:59 +00:00
|
|
|
app_path: system_path.SystemPath = configs.AUT_PATH,
|
2023-09-11 18:24:13 +00:00
|
|
|
user_data: SystemPath = None
|
2023-08-04 18:27:03 +00:00
|
|
|
):
|
|
|
|
super(AUT, self).__init__()
|
|
|
|
self.path = app_path
|
|
|
|
self.ctx = None
|
2023-08-10 11:43:17 +00:00
|
|
|
self.pid = None
|
2023-10-19 12:26:05 +00:00
|
|
|
self.port = None
|
2023-09-13 07:47:51 +00:00
|
|
|
self.aut_id = f'AUT_{datetime.now():%H%M%S}'
|
2023-09-11 18:24:13 +00:00
|
|
|
self.app_data = configs.testpath.STATUS_DATA / f'app_{datetime.now():%H%M%S_%f}'
|
2023-09-15 08:15:28 +00:00
|
|
|
if user_data is not None:
|
|
|
|
user_data.copy_to(self.app_data / 'data')
|
2023-11-06 11:31:34 +00:00
|
|
|
self.options = ''
|
2023-08-04 18:27:03 +00:00
|
|
|
driver.testSettings.setWrappersForApplication(self.aut_id, ['Qt'])
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return type(self).__qualname__
|
|
|
|
|
2023-09-11 18:24:13 +00:00
|
|
|
def __enter__(self):
|
|
|
|
return self.launch()
|
|
|
|
|
2023-10-19 12:26:05 +00:00
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
if exc_type:
|
2023-11-07 17:26:07 +00:00
|
|
|
try:
|
|
|
|
self.attach()
|
|
|
|
driver.waitForObjectExists(statusDesktop_mainWindow).setVisible(True)
|
|
|
|
configs.testpath.TEST.mkdir(parents=True, exist_ok=True)
|
|
|
|
screenshot = configs.testpath.TEST / f'{self.aut_id}.png'
|
|
|
|
|
|
|
|
rect = driver.object.globalBounds(driver.waitForObject(statusDesktop_mainWindow))
|
|
|
|
img = ImageGrab.grab(
|
|
|
|
bbox=(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height),
|
|
|
|
xdisplay=configs.system.DISPLAY if IS_LIN else None)
|
|
|
|
view = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)
|
|
|
|
cv2.imwrite(str(screenshot), view)
|
|
|
|
|
|
|
|
allure.attach(
|
|
|
|
name=f'Screenshot on fail: {self.aut_id}',
|
|
|
|
body=screenshot.read_bytes(),
|
|
|
|
attachment_type=allure.attachment_type.PNG)
|
|
|
|
except Exception as err:
|
2023-11-30 13:07:26 +00:00
|
|
|
LOG.info(err)
|
2023-11-07 17:26:07 +00:00
|
|
|
|
2023-10-27 11:44:45 +00:00
|
|
|
self.stop()
|
2023-09-11 18:24:13 +00:00
|
|
|
|
2023-12-06 10:28:19 +00:00
|
|
|
def detach_context(self):
|
2023-11-30 10:34:11 +00:00
|
|
|
if self.ctx is None:
|
|
|
|
return
|
|
|
|
squish.currentApplicationContext().detach()
|
|
|
|
self.ctx = None
|
|
|
|
|
2023-12-06 10:28:19 +00:00
|
|
|
def kill_process(self):
|
2023-11-30 10:34:11 +00:00
|
|
|
if self.pid is None:
|
2024-02-09 14:30:35 +00:00
|
|
|
LOG.warning('No PID available for AUT.')
|
2023-11-30 10:51:00 +00:00
|
|
|
return
|
|
|
|
local_system.kill_process_with_retries(self.pid)
|
2023-11-30 10:34:11 +00:00
|
|
|
self.pid = None
|
|
|
|
|
2023-12-06 10:28:19 +00:00
|
|
|
@allure.step('Attach Squish to Test Application')
|
2024-02-09 14:30:35 +00:00
|
|
|
def attach(self):
|
2023-12-06 10:28:19 +00:00
|
|
|
LOG.info('Attaching to AUT: localhost:%d', self.port)
|
2024-02-09 14:30:35 +00:00
|
|
|
for i in range(3):
|
|
|
|
try:
|
|
|
|
SquishServer().add_attachable_aut(self.aut_id, self.port)
|
|
|
|
if self.ctx is None:
|
|
|
|
self.ctx = context.get_context(self.aut_id)
|
|
|
|
squish.setApplicationContext(self.ctx)
|
|
|
|
assert squish.waitFor(lambda: self.ctx.isRunning, configs.timeouts.PROCESS_TIMEOUT_SEC)
|
|
|
|
except Exception as err:
|
|
|
|
LOG.error('Failed to attach AUT: %s', err)
|
|
|
|
self.stop()
|
|
|
|
raise err
|
|
|
|
LOG.info('Successfully attached AUT!')
|
2023-12-06 10:28:19 +00:00
|
|
|
return self
|
2023-08-04 18:27:03 +00:00
|
|
|
|
2023-12-06 10:28:19 +00:00
|
|
|
@allure.step('Start AUT')
|
|
|
|
def startaut(self):
|
2023-11-30 11:42:17 +00:00
|
|
|
LOG.info('Launching AUT: %s', self.path)
|
|
|
|
self.port = local_system.find_free_port(configs.squish.AUT_PORT, 100)
|
|
|
|
command = [
|
|
|
|
str(configs.testpath.SQUISH_DIR / 'bin/startaut'),
|
|
|
|
f'--port={self.port}',
|
|
|
|
str(self.path),
|
|
|
|
f'-d={self.app_data}',
|
|
|
|
f'--LOG_LEVEL={configs.testpath.LOG_LEVEL}',
|
|
|
|
]
|
2023-10-19 12:26:05 +00:00
|
|
|
try:
|
2023-12-01 08:11:52 +00:00
|
|
|
with open(configs.AUT_LOG_FILE, "ab") as log:
|
|
|
|
self.pid = local_system.execute(command, stderr=log, stdout=log)
|
2023-11-30 11:42:17 +00:00
|
|
|
except Exception as err:
|
|
|
|
LOG.error('Failed to start AUT: %s', err)
|
2023-10-27 11:44:45 +00:00
|
|
|
self.stop()
|
2023-11-30 11:42:17 +00:00
|
|
|
raise err
|
2023-12-06 10:28:19 +00:00
|
|
|
LOG.info('Launched AUT under PID: %d', self.pid)
|
|
|
|
return self
|
|
|
|
|
|
|
|
@allure.step('Close application')
|
|
|
|
def stop(self):
|
2024-02-09 14:30:35 +00:00
|
|
|
LOG.info('Stopping AUT: %s', self.path)
|
2023-12-06 10:28:19 +00:00
|
|
|
self.detach_context()
|
|
|
|
self.kill_process()
|
|
|
|
|
|
|
|
@allure.step("Start and attach AUT")
|
|
|
|
def launch(self) -> 'AUT':
|
|
|
|
self.startaut()
|
|
|
|
self.wait()
|
|
|
|
self.attach()
|
|
|
|
return self
|
|
|
|
|
|
|
|
@allure.step('Waiting for port')
|
2024-02-09 14:30:35 +00:00
|
|
|
def wait(self, timeout: int = 3, retries: int = 10):
|
2023-12-06 10:28:19 +00:00
|
|
|
LOG.info('Waiting for AUT port localhost:%d...', self.port)
|
|
|
|
try:
|
|
|
|
wait_for_port('localhost', self.port, timeout, retries)
|
|
|
|
except TimeoutError as err:
|
|
|
|
LOG.error('Wait for AUT port timed out: %s', err)
|
|
|
|
self.stop()
|
|
|
|
raise err
|
|
|
|
LOG.info('AUT port available!')
|
2023-08-04 18:27:03 +00:00
|
|
|
|
2023-09-15 08:15:28 +00:00
|
|
|
@allure.step('Restart application')
|
|
|
|
def restart(self):
|
2023-10-27 11:44:45 +00:00
|
|
|
self.stop()
|
2023-09-15 08:15:28 +00:00
|
|
|
self.launch()
|