2023-08-04 18:27:03 +00:00
|
|
|
import logging
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
import allure
|
2023-07-10 15:55:10 +00:00
|
|
|
import pytest
|
2023-08-04 18:27:03 +00:00
|
|
|
from PIL import ImageGrab
|
|
|
|
|
|
|
|
import configs
|
2023-08-10 06:58:50 +00:00
|
|
|
import driver
|
2023-09-28 12:44:13 +00:00
|
|
|
from scripts.utils import local_system
|
2023-08-04 18:27:03 +00:00
|
|
|
from scripts.utils.system_path import SystemPath
|
2023-09-28 12:44:13 +00:00
|
|
|
from fixtures.path import generate_test_info
|
2023-08-04 18:27:03 +00:00
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
2023-07-10 15:55:10 +00:00
|
|
|
|
|
|
|
pytest_plugins = [
|
2023-09-28 12:44:13 +00:00
|
|
|
'fixtures.aut',
|
|
|
|
'fixtures.path',
|
|
|
|
'fixtures.squish',
|
|
|
|
'fixtures.testrail',
|
2023-07-10 15:55:10 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
|
|
|
def setup_session_scope(
|
2023-08-04 18:27:03 +00:00
|
|
|
init_testrail_api,
|
|
|
|
prepare_test_directory,
|
|
|
|
start_squish_server,
|
|
|
|
):
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_function_scope(
|
2023-08-10 11:43:17 +00:00
|
|
|
caplog,
|
2023-08-04 18:27:03 +00:00
|
|
|
generate_test_data,
|
|
|
|
check_result
|
2023-07-10 15:55:10 +00:00
|
|
|
):
|
2023-08-10 11:43:17 +00:00
|
|
|
caplog.set_level(configs.LOG_LEVEL)
|
2023-07-10 15:55:10 +00:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
2023-08-04 18:27:03 +00:00
|
|
|
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
|
|
|
|
def pytest_runtest_makereport(item, call):
|
|
|
|
outcome = yield
|
|
|
|
rep = outcome.get_result()
|
|
|
|
setattr(item, 'rep_' + rep.when, rep)
|
|
|
|
|
|
|
|
|
2023-07-10 15:55:10 +00:00
|
|
|
def pytest_exception_interact(node):
|
2023-08-04 18:27:03 +00:00
|
|
|
try:
|
|
|
|
test_path, test_name, test_params = generate_test_info(node)
|
|
|
|
node_dir: SystemPath = configs.testpath.RUN / test_path / test_name / test_params
|
|
|
|
node_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
screenshot = node_dir / 'screenshot.png'
|
|
|
|
if screenshot.exists():
|
|
|
|
screenshot = node_dir / f'screenshot_{datetime.now():%H%M%S}.png'
|
|
|
|
ImageGrab.grab().save(screenshot)
|
|
|
|
allure.attach(
|
|
|
|
name='Screenshot on fail',
|
|
|
|
body=screenshot.read_bytes(),
|
|
|
|
attachment_type=allure.attachment_type.PNG)
|
2023-08-10 06:58:50 +00:00
|
|
|
driver.context.detach()
|
2023-09-28 12:44:13 +00:00
|
|
|
for port in [configs.squish.AUT_PORT, configs.squish.MLT_AUT_PORT]:
|
|
|
|
pid = local_system.find_process_by_port(port)
|
|
|
|
if pid is not None:
|
|
|
|
local_system.kill_process(pid, verify=True)
|
2023-08-04 18:27:03 +00:00
|
|
|
except Exception as ex:
|
|
|
|
_logger.debug(ex)
|