-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
109 lines (82 loc) · 3.56 KB
/
runner.py
1
import datetimeimport osimport shutilimport subprocessfrom datetime import datefrom time import sleepimport pytestfrom jproperties import Propertiesfrom Cron.config_allure_report import allure_misc_configfrom Cron.cron_image import delete_imgfrom Cron.cron_video import delete_vidparent_dir = os.getcwd()date_time = datetime.datetime.now(datetime.timezone.utc).strftime('%m-%d-%H%M')date = datetime.datetime.now(datetime.timezone.utc).strftime('%m-%d')ALLURE_HOME = shutil.which("allure")def main(): """ :return: 1. This main will Delete the Old Images and videos if Present. 2. This main method will execute Pytest command to execute and run tests mentioned in the Pytest.ini files 3. This main method will generate allure report from the allure results 4. This main method will make some modification in the allure report at the end to generate custom allure report. """ ALLURE_FILE_HTML_NAME_OLD = get_value_from_prop('allure_file_html_name') ALLURE_RESULT_FILE = f'{parent_dir}/reports/allure_results-{date_time}' ALLURE_HTML_FILE = f'{parent_dir}/reports/html/allure_report-html-{date_time}' delete_img() delete_vid() update_prop_file(f'date_time', date_time) update_prop_file(f'allure_results', ALLURE_RESULT_FILE) update_prop_file(f'allure_file_html_name', ALLURE_HTML_FILE) RUN = ['-c', f'{parent_dir}/Test_Cases/pytest.ini', f'--alluredir={ALLURE_RESULT_FILE}', f'--log-file={parent_dir}/Test_Cases/Test_output/Logs/pytest_logs_{date}.log', '-v', f'--junitxml={parent_dir}/reports/xml/allure_results_xml_{date_time}.xml'] pytest.main(RUN) if os.path.exists(ALLURE_RESULT_FILE): if 'history' not in os.listdir(ALLURE_RESULT_FILE): os.mkdir(f'{ALLURE_RESULT_FILE}/history') copy_tree(src=ALLURE_FILE_HTML_NAME_OLD, destination=ALLURE_RESULT_FILE) sleep(2) if ALLURE_HOME is None: ALLURE = get_value_from_prop('allure_home') try: subprocess.call([f'{ALLURE}', 'generate', ALLURE_RESULT_FILE, '-o', ALLURE_HTML_FILE, '--clean']) except FileNotFoundError as e: print(e) else: try: subprocess.call([f'{ALLURE_HOME}', 'generate', ALLURE_RESULT_FILE, '-o', ALLURE_HTML_FILE, '--clean']) except FileNotFoundError as e: print(e) sleep(2) allure_misc_config(ALLURE_HTML_FILE)def update_prop_file(key, val): CONFIG_FILE_PATH_PROP = os.path.join(os.getcwd(), 'Config', 'Environment.properties') prop_config = Properties() with open(CONFIG_FILE_PATH_PROP, 'rb') as r_file: prop_config.load(r_file) data = prop_config.get(key).data print(f'Current Data in the {key}:: {data} ===') prop_config[key] = val with open(CONFIG_FILE_PATH_PROP, 'wb') as w_file: prop_config.store(w_file) data = prop_config.get(key).data print(f'New value of {key}:: {data}')def get_value_from_prop(key): CONFIG_FILE_PATH_PROP = os.path.join(os.getcwd(), 'Config', 'Environment.properties') with open(CONFIG_FILE_PATH_PROP, 'r') as file: for line in file.readlines(): if key in line: file_path = line.split('=')[1].strip() return file_pathdef copy_tree(src, destination): if os.path.exists(src): root_dir = os.listdir(src) for dirs in root_dir: if dirs == 'history': shutil.copytree(src=f'{src}/history', dst=f'{destination}/history', dirs_exist_ok=True) breakif __name__ == '__main__': main()