2023-09-28 12:44:13 +00:00
|
|
|
import logging
|
2023-09-11 18:24:13 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2023-08-04 18:27:03 +00:00
|
|
|
import allure
|
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-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
|
2023-11-07 17:26:07 +00:00
|
|
|
from gui.objects_map 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-08-04 18:27:03 +00:00
|
|
|
|
2023-09-28 12:44:13 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2023-08-04 18:27:03 +00:00
|
|
|
|
|
|
|
class AUT:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
app_path: system_path.SystemPath = configs.APP_DIR,
|
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:
|
|
|
|
_logger.info(err)
|
|
|
|
|
2023-10-27 11:44:45 +00:00
|
|
|
self.stop()
|
2023-09-11 18:24:13 +00:00
|
|
|
|
2023-08-04 18:27:03 +00:00
|
|
|
@allure.step('Attach Squish to Test Application')
|
2023-10-27 11:44:45 +00:00
|
|
|
def attach(self, timeout_sec: int = configs.timeouts.PROCESS_TIMEOUT_SEC):
|
2023-08-04 18:27:03 +00:00
|
|
|
if self.ctx is None:
|
2023-09-13 07:47:51 +00:00
|
|
|
self.ctx = context.attach(self.aut_id, timeout_sec)
|
2023-10-27 11:44:45 +00:00
|
|
|
squish.setApplicationContext(self.ctx)
|
2023-08-04 18:27:03 +00:00
|
|
|
|
2023-10-27 11:44:45 +00:00
|
|
|
@allure.step('Close application')
|
|
|
|
def stop(self):
|
2023-08-04 18:27:03 +00:00
|
|
|
if self.ctx is not None:
|
|
|
|
squish.currentApplicationContext().detach()
|
2023-10-27 11:44:45 +00:00
|
|
|
self.ctx = None
|
2023-08-04 18:27:03 +00:00
|
|
|
|
2023-10-19 12:26:05 +00:00
|
|
|
if self.port is not None:
|
2023-10-27 11:44:45 +00:00
|
|
|
pid_list = local_system.find_process_by_port(self.port)
|
|
|
|
if pid_list:
|
|
|
|
for pid in pid_list:
|
|
|
|
local_system.kill_process(pid, verify=True)
|
|
|
|
assert driver.waitFor(
|
|
|
|
lambda: not local_system.find_process_by_port(self.port), configs.timeouts.PROCESS_TIMEOUT_SEC), \
|
2023-10-19 12:26:05 +00:00
|
|
|
f'Port {self.port} still in use by process: {local_system.find_process_by_port(self.port)}'
|
|
|
|
self.port = None
|
2023-10-27 11:44:45 +00:00
|
|
|
if self.pid in pid_list:
|
|
|
|
self.pid = None
|
|
|
|
|
|
|
|
if self.pid is not None:
|
|
|
|
local_system.kill_process(self.pid, verify=True)
|
|
|
|
self.pid = None
|
2023-08-04 18:27:03 +00:00
|
|
|
|
|
|
|
@allure.step("Start application")
|
2023-11-06 11:31:34 +00:00
|
|
|
def launch(self, options='', attempt: int = 2) -> 'AUT':
|
|
|
|
self.options = options
|
2023-10-19 12:26:05 +00:00
|
|
|
try:
|
|
|
|
self.port = local_system.find_free_port(configs.squish.AUT_PORT, 1000)
|
|
|
|
if configs.ATTACH_MODE:
|
|
|
|
SquishServer().add_attachable_aut(self.aut_id, self.port)
|
|
|
|
command = [
|
|
|
|
configs.testpath.SQUISH_DIR / 'bin' / 'startaut',
|
|
|
|
f'--port={self.port}',
|
|
|
|
f'"{self.path}"',
|
2023-11-06 11:31:34 +00:00
|
|
|
f'-d={self.app_data}',
|
2023-11-17 14:27:51 +00:00
|
|
|
f'--LOG_LEVEL={configs.testpath.LOG_LEVEL}',
|
2023-11-06 11:31:34 +00:00
|
|
|
options
|
2023-10-19 12:26:05 +00:00
|
|
|
]
|
2023-10-27 11:44:45 +00:00
|
|
|
self.pid = local_system.execute(command)
|
2023-10-19 12:26:05 +00:00
|
|
|
else:
|
|
|
|
SquishServer().add_executable_aut(self.aut_id, self.path.parent)
|
|
|
|
command = [self.aut_id, f'-d={self.app_data}']
|
|
|
|
self.ctx = squish.startApplication(' '.join(command), configs.timeouts.PROCESS_TIMEOUT_SEC)
|
2023-10-27 11:44:45 +00:00
|
|
|
self.pid = self.ctx.pid
|
2023-09-11 18:24:13 +00:00
|
|
|
|
2023-09-28 12:44:13 +00:00
|
|
|
self.attach()
|
2023-10-19 12:26:05 +00:00
|
|
|
assert squish.waitFor(lambda: self.ctx.isRunning, configs.timeouts.PROCESS_TIMEOUT_SEC)
|
|
|
|
return self
|
2023-10-27 11:44:45 +00:00
|
|
|
except (AssertionError, TypeError) as err:
|
2023-10-19 12:26:05 +00:00
|
|
|
_logger.debug(err)
|
2023-10-27 11:44:45 +00:00
|
|
|
self.stop()
|
2023-10-19 12:26:05 +00:00
|
|
|
if attempt:
|
2023-11-06 11:31:34 +00:00
|
|
|
return self.launch(options, attempt - 1)
|
2023-10-19 12:26:05 +00:00
|
|
|
else:
|
|
|
|
raise err
|
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()
|