2024-06-26 07:21:20 +00:00
|
|
|
import time
|
|
|
|
|
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
|
2024-03-06 13:41:54 +00:00
|
|
|
import shortuuid
|
2023-11-30 11:42:17 +00:00
|
|
|
from datetime import datetime
|
2024-06-18 06:53:37 +00:00
|
|
|
from configs.system import get_platform
|
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
|
|
|
):
|
|
|
|
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}'
|
2024-03-06 13:41:54 +00:00
|
|
|
self.app_data = configs.testpath.STATUS_DATA / f'app_{shortuuid.ShortUUID().random(length=10)}'
|
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),
|
2024-06-18 06:53:37 +00:00
|
|
|
xdisplay=configs.system.DISPLAY if get_platform() == "Linux" else None)
|
2023-11-07 17:26:07 +00:00
|
|
|
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:
|
2024-03-06 15:05:02 +00:00
|
|
|
LOG.error(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
|
2024-10-04 07:54:43 +00:00
|
|
|
driver.currentApplicationContext().detach()
|
2023-11-30 10:34:11 +00:00
|
|
|
self.ctx = None
|
|
|
|
|
2023-12-06 10:28:19 +00:00
|
|
|
@allure.step('Attach Squish to Test Application')
|
2024-09-08 07:33:42 +00:00
|
|
|
def attach(self):
|
2023-12-06 10:28:19 +00:00
|
|
|
LOG.info('Attaching to AUT: localhost:%d', self.port)
|
2024-02-29 07:40:37 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
SquishServer().add_attachable_aut(self.aut_id, self.port)
|
|
|
|
if self.ctx is None:
|
|
|
|
self.ctx = context.get_context(self.aut_id)
|
2024-10-04 07:54:43 +00:00
|
|
|
driver.setApplicationContext(self.ctx)
|
2024-02-29 07:40:37 +00:00
|
|
|
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
|
2024-02-09 14:30:35 +00:00
|
|
|
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'),
|
2024-02-26 08:14:13 +00:00
|
|
|
'--verbose',
|
2023-11-30 11:42:17 +00:00
|
|
|
f'--port={self.port}',
|
|
|
|
str(self.path),
|
2024-02-29 15:51:23 +00:00
|
|
|
f'--datadir={self.app_data}',
|
2023-11-30 11:42:17 +00:00
|
|
|
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()
|
2024-06-11 14:04:06 +00:00
|
|
|
local_system.kill_process(self.pid)
|
2024-06-28 13:39:47 +00:00
|
|
|
time.sleep(1) # FIXME: Implement waiting for process to actually exit.
|
2023-12-06 10:28:19 +00:00
|
|
|
|
|
|
|
@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-29 07:40:37 +00:00
|
|
|
def wait(self, timeout: int = 1, 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()
|