AI Resource Pro / website / tests / selenium / utils.py
| 1 | import os |
| 2 | import datetime |
| 3 | |
| 4 | # Track the current screenshot folder for the test run |
| 5 | SCREENSHOT_FOLDER = None |
| 6 | |
| 7 | # Create a function to handle screenshot saving in datetime folders |
| 8 | def save_screenshot(browser, filename): |
| 9 | """ |
| 10 | Save a screenshot to a datetime folder within the screenshots directory. |
| 11 | Creates a new folder for each test run. |
| 12 | """ |
| 13 | global SCREENSHOT_FOLDER |
| 14 | |
| 15 | # Create screenshots base directory if it doesn't exist |
| 16 | screenshots_base_dir = os.path.join(os.path.dirname(__file__), 'screenshots') |
| 17 | if not os.path.exists(screenshots_base_dir): |
| 18 | os.makedirs(screenshots_base_dir) |
| 19 | |
| 20 | # Create a datetime folder for this test run if it doesn't exist |
| 21 | if SCREENSHOT_FOLDER is None: |
| 22 | timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") |
| 23 | SCREENSHOT_FOLDER = os.path.join(screenshots_base_dir, timestamp) |
| 24 | if not os.path.exists(SCREENSHOT_FOLDER): |
| 25 | os.makedirs(SCREENSHOT_FOLDER) |
| 26 | print(f"Created new screenshot folder: {SCREENSHOT_FOLDER}") |
| 27 | |
| 28 | # Save screenshot to the datetime folder with original filename |
| 29 | screenshot_path = os.path.join(SCREENSHOT_FOLDER, filename) |
| 30 | browser.save_screenshot(screenshot_path) |
| 31 | print(f"Screenshot saved: {screenshot_path}") |