diff --git a/lettuce_webdriver/screenshot.py b/lettuce_webdriver/screenshot.py index d8385cf..25b42af 100644 --- a/lettuce_webdriver/screenshot.py +++ b/lettuce_webdriver/screenshot.py @@ -1,10 +1,14 @@ """Steps and utility functions for taking screenshots.""" +import uuid + from lettuce import ( + after, step, world, ) import os.path +import json def set_save_directory(base, source): """Sets the root save directory for saving screenshots. @@ -18,23 +22,52 @@ def set_save_directory(base, source): world.screenshot_root = root -@step(r'I capture a screenshot named "(.*?)"$') -def capture_screenshot(step, name): +def resolution_path(world): window_size = world.browser.get_window_size() - dir_path = os.path.join( + return os.path.join( world.screenshot_root, '{}x{}'.format(window_size['width'], window_size['height']), ) - if not os.path.isdir(dir_path): - os.makedirs(dir_path) + + +@step(r'I capture a screenshot$') +def capture_screenshot(step): + feature = step.scenario.feature + step.shot_name = '{}.png'.format(uuid.uuid4()) + if getattr(feature, 'dir_path', None) is None: + feature.dir_path = resolution_path(world) + if not os.path.isdir(feature.dir_path): + os.makedirs(feature.dir_path) filename = os.path.join( - dir_path, - '{}.png'.format(name) + feature.dir_path, + step.shot_name, ) world.browser.get_screenshot_as_file(filename) -@step(r'I capture a screenshot named "(.*?)" after (\d+) seconds?$') -def capture_screenshot_delay(step, name, delay): +@step(r'I capture a screenshot after (\d+) seconds?$') +def capture_screenshot_delay(step, delay): time.sleep(delay) - capture_screenshot(name) + capture_screenshot() + + +@after.each_feature +def record_run_feature_report(feature): + if getattr(feature, 'dir_path', None) is None: + return + feature_name_json = '{}.json'.format(os.path.splitext( + os.path.basename(feature.described_at.file) + )[0]) + report = {} + for scenario in feature.scenarios: + scenario_report = [] + for step in scenario.steps: + shot_name = getattr(step, 'shot_name', None) + if shot_name is not None: + scenario_report.append(shot_name) + if scenario_report: + report[scenario.name] = scenario_report + + if report: + with open(os.path.join(feature.dir_path, feature_name_json), 'w') as f: + json.dump(report, f)