Skip to content

Commit

Permalink
Automated inventory of captured screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
npilon committed Mar 14, 2014
1 parent 5101982 commit c734e3e
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions lettuce_webdriver/screenshot.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)

0 comments on commit c734e3e

Please sign in to comment.