Skip to content

Commit

Permalink
Allow to capture screenshot in step execution, #99
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamsc committed Aug 6, 2018
1 parent eaaa212 commit 7284859
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 65 deletions.
2 changes: 1 addition & 1 deletion gauge-proto
Submodule gauge-proto updated 1 files
+18 −2 spec.proto
2 changes: 1 addition & 1 deletion getgauge/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _get_args(execution_info, hook):

def _add_exception(e, response, continue_on_failure):
if os.getenv('screenshot_on_failure') == 'true':
response.executionStatusResponse.executionResult.screenShot = registry.screenshot_provider()()
response.executionStatusResponse.executionResult.screenShot.append(registry.screenshot_provider()())
response.executionStatusResponse.executionResult.failed = True
message = e.__str__()
if not message:
Expand Down
2 changes: 1 addition & 1 deletion getgauge/messages/api_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 112 additions & 56 deletions getgauge/messages/spec_pb2.py

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion getgauge/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from getgauge.messages.spec_pb2 import Parameter, Span
from getgauge.python import Table, create_execution_context_from, data_store
from getgauge.refactor import refactor_step
from getgauge.registry import registry, MessagesStore
from getgauge.registry import registry, MessagesStore, ScreenshotsStore
from getgauge.static_loader import reload_steps
from getgauge.util import get_step_impl_dir, get_impl_files, read_file_contents, get_file_name
from getgauge.validator import validate_step
Expand Down Expand Up @@ -91,52 +91,60 @@ def _execute_before_suite_hook(request, response, _socket, clear=True):
execution_info = create_execution_context_from(request.executionStartingRequest.currentExecutionInfo)
run_hook(request, response, registry.before_suite(), execution_info)
response.executionStatusResponse.executionResult.message.extend(MessagesStore.pending_messages())
response.executionStatusResponse.executionResult.screenShot.extend(ScreenshotsStore.pending_screenshots())


def _execute_after_suite_hook(request, response, _socket):
execution_info = create_execution_context_from(request.executionEndingRequest.currentExecutionInfo)
run_hook(request, response, registry.after_suite(), execution_info)
response.executionStatusResponse.executionResult.message.extend(MessagesStore.pending_messages())
response.executionStatusResponse.executionResult.screenShot.extend(ScreenshotsStore.pending_screenshots())


def _execute_before_spec_hook(request, response, _socket):
execution_info = create_execution_context_from(request.specExecutionStartingRequest.currentExecutionInfo)
run_hook(request, response, registry.before_spec(execution_info.specification.tags), execution_info)
response.executionStatusResponse.executionResult.message.extend(MessagesStore.pending_messages())
response.executionStatusResponse.executionResult.screenShot.extend(ScreenshotsStore.pending_screenshots())


def _execute_after_spec_hook(request, response, _socket):
execution_info = create_execution_context_from(request.specExecutionEndingRequest.currentExecutionInfo)
run_hook(request, response, registry.after_spec(execution_info.specification.tags), execution_info)
response.executionStatusResponse.executionResult.message.extend(MessagesStore.pending_messages())
response.executionStatusResponse.executionResult.screenShot.extend(ScreenshotsStore.pending_screenshots())


def _execute_before_scenario_hook(request, response, _socket):
execution_info = create_execution_context_from(request.scenarioExecutionStartingRequest.currentExecutionInfo)
tags = list(execution_info.scenario.tags) + list(execution_info.specification.tags)
run_hook(request, response, registry.before_scenario(tags), execution_info)
response.executionStatusResponse.executionResult.message.extend(MessagesStore.pending_messages())
response.executionStatusResponse.executionResult.screenShot.extend(ScreenshotsStore.pending_screenshots())


def _execute_after_scenario_hook(request, response, _socket):
execution_info = create_execution_context_from(request.scenarioExecutionEndingRequest.currentExecutionInfo)
tags = list(execution_info.scenario.tags) + list(execution_info.specification.tags)
run_hook(request, response, registry.after_scenario(tags), execution_info)
response.executionStatusResponse.executionResult.message.extend(MessagesStore.pending_messages())
response.executionStatusResponse.executionResult.screenShot.extend(ScreenshotsStore.pending_screenshots())


def _execute_before_step_hook(request, response, _socket):
execution_info = create_execution_context_from(request.stepExecutionStartingRequest.currentExecutionInfo)
tags = list(execution_info.scenario.tags) + list(execution_info.specification.tags)
run_hook(request, response, registry.before_step(tags), execution_info)
response.executionStatusResponse.executionResult.message.extend(MessagesStore.pending_messages())
response.executionStatusResponse.executionResult.screenShot.extend(ScreenshotsStore.pending_screenshots())


def _execute_after_step_hook(request, response, _socket):
execution_info = create_execution_context_from(request.stepExecutionEndingRequest.currentExecutionInfo)
tags = list(execution_info.scenario.tags) + list(execution_info.specification.tags)
run_hook(request, response, registry.after_step(tags), execution_info)
response.executionStatusResponse.executionResult.message.extend(MessagesStore.pending_messages())
response.executionStatusResponse.executionResult.screenShot.extend(ScreenshotsStore.pending_screenshots())


def _init_scenario_data_store(request, response, _socket):
Expand Down
21 changes: 20 additions & 1 deletion getgauge/python.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
import warnings

from getgauge.registry import registry, MessagesStore
from getgauge.registry import registry, MessagesStore, ScreenshotsStore

try:
from collections.abc import MutableMapping
Expand Down Expand Up @@ -56,10 +56,23 @@ def after_step(obj=None):


def screenshot(func):
_warn_screenshot_deprecation()
registry.set_screenshot_provider(func)
return func


def custom_screen_grabber(func):
registry.set_screenshot_provider(func)
return func


def _warn_screenshot_deprecation():
warnings.warn(
"'screenshot' is deprecated in favour of 'custom_screen_grabber'",
DeprecationWarning, stacklevel=3)
warnings.simplefilter('default', DeprecationWarning)


class Table:
def __init__(self, proto_table):
self.__headers = proto_table.headers.cells
Expand Down Expand Up @@ -341,3 +354,9 @@ def func(function):
return function

return func


class Screenshots:
@staticmethod
def capture_screenshot():
ScreenshotsStore.capture()
18 changes: 18 additions & 0 deletions getgauge/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,21 @@ def _take_screenshot():


registry = Registry()


class ScreenshotsStore:
__screenshots = []

@staticmethod
def pending_screenshots():
screenshots = ScreenshotsStore.__screenshots
ScreenshotsStore.__screenshots = []
return screenshots

@staticmethod
def capture():
ScreenshotsStore.__screenshots.append(registry.screenshot_provider()())

@staticmethod
def clear():
ScreenshotsStore.__screenshots = []
6 changes: 3 additions & 3 deletions tests/test_data/impl_stubs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from getgauge.python import (before_step, step, after_step, before_scenario,
after_scenario, before_spec, after_spec,
before_suite, after_suite, screenshot,
before_suite, after_suite, custom_screen_grabber,
continue_on_failure)


Expand Down Expand Up @@ -85,6 +85,6 @@ def after_suite2():
pass


@screenshot
@custom_screen_grabber
def take_screenshot():
return ""
return "foo"
26 changes: 25 additions & 1 deletion tests/test_python.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest import TestCase, main

from getgauge.messages.messages_pb2 import Message
from getgauge.registry import registry, MessagesStore
from getgauge.registry import registry, MessagesStore, ScreenshotsStore
from getgauge.python import (Messages, DataStore, DataStoreFactory, DictObject,
DataStoreContainer, data_store, Table, Specification,
Scenario, Step, ExecutionContext,
Expand Down Expand Up @@ -507,5 +507,29 @@ def test_screenshot_decorator(self):
self.assertEqual(expected, func.__name__)


class ScreenshotsTests(TestCase):

def test_pending_screenshots(self):
ScreenshotsStore.capture()
pending_screenshots = ScreenshotsStore.pending_screenshots()
self.assertEqual(['foo'], pending_screenshots)

def test_clear(self):
ScreenshotsStore.capture()
ScreenshotsStore.clear()
pending_screenshots = ScreenshotsStore.pending_screenshots()
self.assertEqual([], pending_screenshots)

def test_pending_screenshots_gives_only_those_screenshots_which_are_not_collected(self):
ScreenshotsStore.capture()
pending_screenshots = ScreenshotsStore.pending_screenshots()
self.assertEqual(['foo'], pending_screenshots)
pending_screenshots = ScreenshotsStore.pending_screenshots()
self.assertEqual([], pending_screenshots)
ScreenshotsStore.capture()
pending_screenshots = ScreenshotsStore.pending_screenshots()
self.assertEqual(['foo'], pending_screenshots)


if __name__ == '__main__':
main()

0 comments on commit 7284859

Please sign in to comment.