3

The Allure framework is a really beautiful framework for test reporting. Yet it has rather bad documentation for C#.

I want to add some things to my allure report:

  • Debug log (like all things I write to debug)
  • Screenshot
  • A file

How to do it? I have no idea, please help me if you know how to do it. It seems like AllureLifecycle class can help me but I'm not sure how to use it.

In case it matters I use Allure together with SpecFlow and MS test.

Denis Koreyba
  • 3,144
  • 1
  • 31
  • 49

4 Answers4

2

I searched more and seems I found the Truth.

And the Truth is it's possible to add all attachments I wanted but they can be added only as a file:

byte[] log = Encoding.ASCII.GetBytes(Log.GetAllLog());
AllureLifecycle.Instance.AddAttachment("DebugLog", "application/json", log, "json");

If you want to add a file from actually a path (location) you can do it with the same method but a different overload.

So just place this code in a "teardown\afterscenario" method or at any other place (for example at "afterstep" method) where you want to make this attachment. I use SpecFlow so if I add this to "AfterStep" hook then Allure displays those files attached to a specific step! That's amazing!)

Denis Koreyba
  • 3,144
  • 1
  • 31
  • 49
1

it seems that allure has some events that can be used. See : https://github.com/allure-framework/allure-csharp-commons/blob/master/AllureCSharpCommons.Tests/IntegrationTests.cs for more information.

haven't tried it myself, but something like this should work according to the documentation.

  _lifecycle = Allure.DefaultLifecycle;  
  _lifecycle.Fire(new
 MakeAttachmentEvent(AllureResultsUtils.TakeScreenShot(),
                 "Screenshot",
                 "image/png"));
 _lifecycle.Fire(new MakeAttachmentEvent(File.ReadAllBytes("TestData/attachment.json"),
            "JsonAttachment",
            "application/json"));

Hope this helps.

Jeroen Lamberts
  • 191
  • 1
  • 8
0

Using this kind of code in AfterScenario method:

if (_scenarioContext.TestError != null)
            {
                var path = WebElementsUtils.MakeScreenshot(_driver);
                _allureLifecycle.AddAttachment(path);
            }

First it verifies, if Scenario passed, if not then

WebElementsUtils.MakeScreenshot(_driver)

method makes screenshot and returns it's path. Then this path I giving to Allure. As a second parameter in the same method I can give a name of the screenshot. As a result I am getting a screenshot in AfterScenario block in Allure report. P.S. This is only for screenshots, about logs can't tell nothing.

Ukrainis
  • 534
  • 2
  • 16
0

With this example you can add an attachment exactly to the failed step

        [AfterStep(Order = 0)]
        public void RecordScreenFailure(ScenarioContext scenarioContext)
        {
            if (scenarioContext.TestError != null)
            {
                Allure.Commons.AllureLifecycle allureInstance = Allure.Commons.AllureLifecycle.Instance;
                string screenshotPath = MagicMethodMakingScreenshotAndReturningPathToIt(); 
                allureInstance.UpdateTestCase(testResult => {
                    Allure.Commons.StepResult failedStepRsult = 
                        testResult.steps.First(step => step.status == Allure.Commons.Status.failed);
                    failedStepRsult.attachments.Add(new Allure.Commons.Attachment() { 
                        name = "failure screen", 
                        source = screenshotPath, 
                        type = "image/png"
                    });

                });
            }
        }