Skip to content

Instantly share code, notes, and snippets.

@rasmuskl
Last active August 29, 2015 14:19
Show Gist options
  • Save rasmuskl/a61c07541074440c370f to your computer and use it in GitHub Desktop.
Save rasmuskl/a61c07541074440c370f to your computer and use it in GitHub Desktop.
SpecFlow XTC screenshot workaround
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Bindings;
using TechTalk.SpecFlow.Bindings.Reflection;
using TechTalk.SpecFlow.Tracing;
using Xamarin.UITest;
using Xamarin.UITest.Events;
namespace SpecFlowHooks
{
[Binding]
public class XamarinTestCloudHooks
{
static IApp _app;
[AfterAppStarted]
public static void AfterAppStarted(IApp app)
{
_app = app;
}
[BeforeScenario]
public void BeforeScenario()
{
var testRunner = TestRunnerManager.GetTestRunner();
var executionEngineField = testRunner.GetType().GetField("executionEngine", BindingFlags.Instance | BindingFlags.NonPublic);
var executionEngine = executionEngineField.GetValue(testRunner);
var testTracerField = executionEngine.GetType().GetField("testTracer", BindingFlags.Instance | BindingFlags.NonPublic);
var testTracerValue = testTracerField.GetValue(executionEngine);
if (testTracerValue is TestTracerDecorator)
{
// Already decorated.
return;
}
testTracerField.SetValue(executionEngine, new TestTracerDecorator(testTracerValue as ITestTracer));
}
class TestTracerDecorator : ITestTracer
{
readonly ITestTracer _testTracer;
StepInstance _currentStepInstance;
public TestTracerDecorator(ITestTracer testTracer)
{
_testTracer = testTracer;
}
public void TraceStep(StepInstance stepInstance, bool showAdditionalArguments)
{
_currentStepInstance = stepInstance;
_testTracer.TraceStep(stepInstance, showAdditionalArguments);
}
public void TraceWarning(string text)
{
_testTracer.TraceWarning(text);
}
public void TraceStepDone(BindingMatch match, object[] arguments, TimeSpan duration)
{
if (_currentStepInstance != null && _app != null)
{
_app.Screenshot(string.Format("{0} {1}", _currentStepInstance.Keyword, _currentStepInstance.Text));
}
_testTracer.TraceStepDone(match, arguments, duration);
}
public void TraceStepSkipped()
{
_testTracer.TraceStepSkipped();
}
public void TraceStepPending(BindingMatch match, object[] arguments)
{
_testTracer.TraceStepPending(match, arguments);
}
public void TraceBindingError(BindingException ex)
{
_testTracer.TraceBindingError(ex);
}
public void TraceError(Exception ex)
{
_testTracer.TraceError(ex);
}
public void TraceNoMatchingStepDefinition(StepInstance stepInstance, ProgrammingLanguage targetLanguage,
CultureInfo bindingCulture, List<BindingMatch> matchesWithoutScopeCheck)
{
_testTracer.TraceNoMatchingStepDefinition(stepInstance, targetLanguage, bindingCulture, matchesWithoutScopeCheck);
}
public void TraceDuration(TimeSpan elapsed, IBindingMethod method, object[] arguments)
{
_testTracer.TraceDuration(elapsed, method, arguments);
}
public void TraceDuration(TimeSpan elapsed, string text)
{
_testTracer.TraceDuration(elapsed, text);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment