Skip to main content

Capturing screenshot in BeforeFeature

Comments

4 comments

  • Andreas Willich

    ScenarioContext is only available, when a scenario is executed. 

    Which TestContext are you using? Is this NUnit?

    0
  • Brittany Lazarski

    We are using MSTest.  Any thoughts on how to get a screenshot taken in a [BeforeFeature] and storing it with the test run? (we use Azure DevOps for our test runs) Normally we attach it to the test, but that is not an option if ScenarioContext is not available.

    0
  • Andreas Willich

    Hmm, can you do it with normal MSTest in the `ClassInitialize` method? The BeforeFeature hook is executed via this method. 

    0
  • Brittany Lazarski

    I discovered that as long as you put a try/catch in the BeforeFeature, the tests in that feature will not auto-fail. (without a try/catch, the feature fails and all of its tests are auto-failed, so they are never actually run)

    So, my workaround was to add a try/catch in the BeforeFeature, capture the error in the Catch, and then logged that error in each test in that feature. And since I already had tests capturing screenshots on failure, each test now has a screenshot and the feature failure message in the test output.

    Not exactly what I wanted, but serves a similar purpose.

     

    [BeforeFeature]
            public static void BeforeFeature()
            {
                try
                {
                  ….
                }
                catch (Exception ex)
                {
                    LogBeforeFeatureError(ex);
                }
            }

    private static void LogBeforeFeatureError(Exception ex)
            {
                //When a [BeforeFeature] fails, capture the error. This will be written to each of this feature's test outputs.
                string thisFeature = FeatureContext.Current.FeatureInfo.Title.ToIdentifier();
                Globals.BeforeFeatureFailMessage = $"BeforeFeature Failed for {thisFeature}: {ex}";
            }


    [BeforeScenario]
         public static void BeforeScenario()
            {
               //If the [BeforeFeature] failed, log it with this test so it's clear why this test will likely fail
                if (Globals.BeforeFeatureFailMessage !=null)
                {
                    Console.WriteLine(Globals.BeforeFeatureFailMessage);
                }
            }
    0

Please sign in to leave a comment.

Powered by Zendesk