2023-09-11 18:24:13 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2023-08-04 18:27:03 +00:00
|
|
|
import allure
|
|
|
|
import squish
|
|
|
|
|
|
|
|
import configs
|
|
|
|
import driver
|
|
|
|
from driver import context
|
|
|
|
from driver.server import SquishServer
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
class AUT:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
app_path: system_path.SystemPath = configs.APP_DIR,
|
|
|
|
host: str = '127.0.0.1',
|
2023-09-11 18:24:13 +00:00
|
|
|
port: int = configs.squish.AUT_PORT,
|
|
|
|
user_data: SystemPath = None
|
2023-08-04 18:27:03 +00:00
|
|
|
):
|
|
|
|
super(AUT, self).__init__()
|
|
|
|
self.path = app_path
|
|
|
|
self.host = host
|
|
|
|
self.port = int(port)
|
|
|
|
self.ctx = None
|
2023-08-10 11:43:17 +00:00
|
|
|
self.pid = 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-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()
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
self.detach().stop()
|
|
|
|
|
2023-08-04 18:27:03 +00:00
|
|
|
@allure.step('Attach Squish to Test Application')
|
|
|
|
def attach(self, timeout_sec: int = configs.timeouts.PROCESS_TIMEOUT_SEC, attempt: int = 2):
|
|
|
|
if self.ctx is None:
|
2023-09-13 07:47:51 +00:00
|
|
|
self.ctx = context.attach(self.aut_id, timeout_sec)
|
2023-08-04 18:27:03 +00:00
|
|
|
try:
|
|
|
|
squish.setApplicationContext(self.ctx)
|
|
|
|
except TypeError as err:
|
|
|
|
if attempt:
|
|
|
|
return self.attach(timeout_sec, attempt - 1)
|
|
|
|
else:
|
|
|
|
raise err
|
|
|
|
|
|
|
|
@allure.step('Detach Squish and Application')
|
|
|
|
def detach(self):
|
|
|
|
if self.ctx is not None:
|
|
|
|
squish.currentApplicationContext().detach()
|
|
|
|
self.ctx = None
|
|
|
|
return self
|
|
|
|
|
2023-08-22 15:15:25 +00:00
|
|
|
@allure.step('Close application')
|
2023-08-10 06:58:50 +00:00
|
|
|
def stop(self):
|
2023-09-11 18:24:13 +00:00
|
|
|
local_system.kill_process(self.pid, verify=True)
|
2023-08-04 18:27:03 +00:00
|
|
|
|
|
|
|
@allure.step("Start application")
|
2023-09-15 08:15:28 +00:00
|
|
|
def launch(self, ) -> 'AUT':
|
2023-08-04 18:27:03 +00:00
|
|
|
SquishServer().set_aut_timeout()
|
2023-09-11 18:24:13 +00:00
|
|
|
|
2023-08-04 18:27:03 +00:00
|
|
|
if configs.ATTACH_MODE:
|
2023-09-13 07:47:51 +00:00
|
|
|
if local_system.find_process_by_port(self.port):
|
|
|
|
self.port += 100
|
|
|
|
SquishServer().add_attachable_aut(self.aut_id, self.port)
|
2023-08-04 18:27:03 +00:00
|
|
|
command = [
|
2023-09-11 18:24:13 +00:00
|
|
|
configs.testpath.SQUISH_DIR / 'bin' / 'startaut',
|
|
|
|
f'--port={self.port}',
|
|
|
|
f'"{self.path}"',
|
|
|
|
f'-d={self.app_data}'
|
|
|
|
]
|
2023-08-04 18:27:03 +00:00
|
|
|
local_system.execute(command)
|
|
|
|
else:
|
|
|
|
SquishServer().add_executable_aut(self.aut_id, self.path.parent)
|
2023-09-11 18:24:13 +00:00
|
|
|
command = [self.aut_id, f'-d={self.app_data}']
|
|
|
|
self.ctx = squish.startApplication(' '.join(command), configs.timeouts.PROCESS_TIMEOUT_SEC)
|
2023-08-04 18:27:03 +00:00
|
|
|
|
|
|
|
self.attach()
|
2023-08-10 11:43:17 +00:00
|
|
|
self.pid = self.ctx.pid
|
2023-08-04 18:27:03 +00:00
|
|
|
assert squish.waitFor(lambda: self.ctx.isRunning, configs.timeouts.PROCESS_TIMEOUT_SEC)
|
|
|
|
return self
|
2023-09-15 08:15:28 +00:00
|
|
|
|
|
|
|
@allure.step('Restart application')
|
|
|
|
def restart(self):
|
|
|
|
self.detach().stop()
|
|
|
|
self.launch()
|