|
| 1 | +""" |
| 2 | +Manages the environment config. |
| 3 | +""" |
| 4 | + |
| 5 | +import logging |
| 6 | +import os |
| 7 | +import shutil |
| 8 | +from datetime import datetime |
| 9 | + |
| 10 | +from behave.contrib.scenario_autoretry import patch_scenario_with_autoretry |
| 11 | +from selenium import webdriver |
| 12 | +from selenium.webdriver.chrome.service import Service |
| 13 | +from support.settings import settings |
| 14 | +from utils.testdatahelper import TestDataHelper |
| 15 | + |
| 16 | +PRIMARY_DEV_ENV = "dev" |
| 17 | +SECONDARY_DEV_ENVS = ["int1", "int2", "int3"] |
| 18 | + |
| 19 | + |
| 20 | +def before_all(context): |
| 21 | + """runs before all tests and sets the settings""" |
| 22 | + context.test_settings = settings |
| 23 | + context.test_config = TestDataHelper(context) |
| 24 | + |
| 25 | + |
| 26 | +def before_scenario(context, scenario): |
| 27 | + """define before scenario""" |
| 28 | + if context.test_settings.env not in scenario.effective_tags: |
| 29 | + if ( |
| 30 | + PRIMARY_DEV_ENV in scenario.effective_tags |
| 31 | + and context.test_settings.env in SECONDARY_DEV_ENVS |
| 32 | + ): |
| 33 | + logging.info("Mapping primary development env to secondary envs") |
| 34 | + else: |
| 35 | + scenario.skip( |
| 36 | + f"Scenario {scenario} - skipping, test not defined for {context.test_settings.env} environment" |
| 37 | + ) |
| 38 | + |
| 39 | + logging.info("Scenario %s - starting", scenario) |
| 40 | + |
| 41 | + if "api" not in context.tags and "database" not in context.tags: |
| 42 | + if settings.browser.lower() == "chrome": |
| 43 | + logging.info("Chrome browser") |
| 44 | + options = webdriver.ChromeOptions() |
| 45 | + options.add_argument("--no-sandbox") |
| 46 | + options.add_argument("--start-maximized") |
| 47 | + options.add_argument("--window-size=1920,1080") |
| 48 | + options.add_argument("--disable-extensions") |
| 49 | + options.add_argument("--disable-dev-shm-usage") |
| 50 | + options.add_argument("--allow-running-insecure-content") |
| 51 | + options.add_argument("--ignore-certificate-errors") |
| 52 | + if settings.is_headless_browser: |
| 53 | + # https://www.selenium.dev/blog/2023/headless-is-going-away/ |
| 54 | + logging.info("Chrome running in headless mode") |
| 55 | + options.add_argument("--headless=new") |
| 56 | + logging.info("Web driver: %s", settings.driver_executable_path) |
| 57 | + service = Service(settings.driver_executable_path) |
| 58 | + context.driver = webdriver.Chrome(service=service, options=options) |
| 59 | + elif settings.browser.lower() == "edge": |
| 60 | + logging.info("Edge browser") |
| 61 | + options = webdriver.EdgeOptions() |
| 62 | + options.add_argument("--no-sandbox") |
| 63 | + options.add_argument("--start-maximized") |
| 64 | + options.add_argument("--window-size=1920,1080") |
| 65 | + options.add_argument("--disable-extensions") |
| 66 | + options.add_argument("--disable-dev-shm-usage") |
| 67 | + options.add_argument("--allow-running-insecure-content") |
| 68 | + options.add_argument("--ignore-certificate-errors") |
| 69 | + if settings.is_headless_browser: |
| 70 | + # https://www.selenium.dev/blog/2023/headless-is-going-away/ |
| 71 | + logging.info("Edge running in headless mode") |
| 72 | + options.add_argument("--headless=new") |
| 73 | + logging.info("Web driver: %s", settings.driver_executable_path) |
| 74 | + service = Service(settings.driver_executable_path) |
| 75 | + context.driver = webdriver.Edge(service=service, options=options) |
| 76 | + else: |
| 77 | + logging.error("Browser %s is not supported", settings.browser) |
| 78 | + raise ValueError("Browser is not supported") |
| 79 | + |
| 80 | + |
| 81 | +def after_scenario(context, scenario): |
| 82 | + """runs after scenario and quits the driver""" |
| 83 | + logging.info("Scenario %s - complete", scenario) |
| 84 | + if hasattr(context, "driver"): |
| 85 | + context.driver.quit() |
| 86 | + |
| 87 | + |
| 88 | +def before_tag(context, tag): |
| 89 | + """runs before a section tagged with the given name""" |
| 90 | + if "auto" in tag.lower(): |
| 91 | + logging.info("Executing %s", tag) |
| 92 | + # set the AUTO test reference in the context |
| 93 | + context.test_id = tag |
| 94 | + |
| 95 | + |
| 96 | +def before_feature(context, feature): |
| 97 | + """runs before a feature""" |
| 98 | + for scenario in feature.scenarios: |
| 99 | + if "autoretry" in scenario.effective_tags: |
| 100 | + patch_scenario_with_autoretry( |
| 101 | + scenario, max_attempts=int(settings.autoretry_attempts) |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +def after_step(context, step): |
| 106 | + """save screenshot if step fails""" |
| 107 | + logging.info("Completed step %s", step) |
| 108 | + if hasattr(context, "driver"): |
| 109 | + if step.status == "failed": |
| 110 | + logging.info(step.status) |
| 111 | + if not hasattr(context, "test_id"): |
| 112 | + context.test_id = "" |
| 113 | + logging.info("Saving screenshot for failed test %s", context.test_id) |
| 114 | + step_name = step.name.replace(" ", "_").lower() |
| 115 | + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") |
| 116 | + file_name = f"{context.test_id}_{step_name}_{timestamp}.png" |
| 117 | + |
| 118 | + screenshot_dir = "screenshots" |
| 119 | + if not os.path.exists(screenshot_dir): |
| 120 | + os.makedirs(screenshot_dir) |
| 121 | + |
| 122 | + screenshot_path = os.path.join(screenshot_dir, file_name) |
| 123 | + if context.driver: |
| 124 | + context.driver.save_screenshot(screenshot_path) |
| 125 | + |
| 126 | + |
| 127 | +def after_all(context): |
| 128 | + """runs after all tests have finished""" |
| 129 | + if os.path.exists(context.temp_folder_path): |
| 130 | + shutil.rmtree(context.temp_folder_path) |
| 131 | + logging.info("Temp folder deleted...") |
0 commit comments