Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attachments aren't shown under each step in case of using [AfterStep] specflow Hook Attribute #52

Open
1 of 3 tasks
Mik1Br opened this issue Oct 4, 2019 · 3 comments
Labels

Comments

@Mik1Br
Copy link

Mik1Br commented Oct 4, 2019

I'm submitting a:

  • bug report
  • feature request
  • support request => Please do not submit support request here, see note at the top of this template.

What is the current behavior?

when we use AddAttachment(filePath) method in [AfterStep] specflow Hook then attachments aren't shown under each step. All attachments are added after all feature steps.

Untitled

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem

Create simple Specflow project where screen can be made:
1) Add feature file:
@regression
Feature: Sample test
In order to verify mobile app
As logged in user
I want to see correct behavior

Scenario: Sample test
Given I click on "Sample button"
When I Swipe Left to "sample element" element
Then I should see "sample name" is "sample value" for card

    [StepDefinition(@"I click on ""(.*)""")]
    public void GivenIClickOnElement(string elementName)
    {
    }

    [StepDefinition(@"I Swipe Left to ""(.*)"" element")]
    public void SwipeLeft(string elementName)
    {
    }

    [Then(@"I should see ""(.*)"" is ""(.*)"" for card)]
    public void ThenIShouldSeeIsForCardWithLastDigits(string elementName, string expectedValue, string cardDigits)
    {
    }

2)
Add [AfterStep] hook
[Binding]
public class BaseHook {
private readonly ScenarioContext _scenarioContext;
public BaseHook(ScenarioContext scenarioContext)
{
_scenarioContext = scenarioContext;
}
[BeforeTestRun]
public static void RegisterServices()
{
//there run browser, or mobile app
}
[AfterStep]
public void TakeScreenShot()
{
var screenShotFilePath = ScreenShotHelper.SaveScreenShot(_scenarioContext);
AllureLifecycle.Instance.AddAttachment(screenShotFilePath, _scenarioContext.StepContext.StepInfo.Text);
}
}

What is the expected behavior?

Attachments are shown under each step in case of using [AfterStep] specflow Hook Attribute

What is the motivation / use case for changing the behavior?

It's more convenient to see screens under each step. Because if we have 20 steps for example it will be a bit messy

Please tell us about your environment:

NUnit.Allure Version 1.0.6
Allure.Commons Version 2.4.2.4
Specflow.Allure Version 2.4.2.4
Specflow Version 2.4.1
SpecFlow.NUnit Version 2.4.1

allure serve allure-results

Other information

In .json result I see:
"steps": [
{
"name": "Given I click on "Sample Element"",
"status": "passed",
"statusDetails": {
"known": false,
"muted": false,
"flaky": false
},
"stage": "finished",
"steps": [],
"attachments": [],
"parameters": [],
"start": 1570178164235,
"stop": 1570178164284
},
{
"name": "When I Swipe Left to "Sample" element",
"status": "passed",
"statusDetails": {
"known": false,
"muted": false,
"flaky": false
},
"stage": "finished",
"steps": [],
"attachments": [],
"parameters": [],
"start": 1570178164870,
"stop": 1570178164871
},
{
"name": "Then I should see "Sample Name" is "Samle" for card "",
"status": "passed",
"statusDetails": {
"known": false,
"muted": false,
"flaky": false
},
"stage": "finished",
"steps": [],
"attachments": [],
"parameters": [],
"start": 1570178165161,
"stop": 1570178165162
}
],
"attachments": [
{
"name": "I click on "Sample element"",
"source": "7b8d735728114f0c87ea75e8324933de-attachment.png",
"type": "image/png"
},
{
"name": "I Swipe Left to "Sample" element",
"source": "642e3d30d3944fce9b9cf10b2622d1f6-attachment.png",
"type": "image/png"
},
{
"name": "I should see "Sample name" is "Sample" for card"",
"source": "e7e4d3a8c6fc4e76900a05329084fb73-attachment.png",
"type": "image/png"
},
],
"parameters": [],
"start": 1570178164227,
"stop": 1570178165426
}
NOTE
If we add attachment in each step definition everything works fine.
[StepDefinition]
public void AddAttachment() {
var fileNameBase = $"{TestContext.CurrentContext.WorkerId}{DateTime.Now:yyyyMMdd_HHmmss}";
var artifactDirectory = Path.Combine(EnvironmentManager.AssemblyDirectoryPath, "Results");
var fileName = Path.Combine(artifactDirectory, fileNameBase + "_log.txt");
using (File.Create(fileName)) { }
AllureLifecycle.Instance.AddAttachment(fileName);
}

@Bakanych Bakanych added the bug label Apr 13, 2020
@toniis
Copy link

toniis commented Dec 7, 2020

+1

@toniis
Copy link

toniis commented Dec 12, 2020

I've managed to fix this by updating the test case with the attachments:

 AllureLifecycle.Instance.UpdateTestCase(testResult => {
                StepResult stepResult = 
                    testResult.steps.Find(stepItem => stepItem.name.Contains(stepName));
                if (stepResult != null)
                {
                    Attachment att = stepResult?.attachments.Find(attachment => attachment.name.Equals(attachmentName));
                    if (att!= null) stepResult?.attachments.Remove(att);
                    string filePath = $"{Environment.CurrentDirectory}/{path}";
                    stepResult?.attachments.Add(new Attachment() { 
                        name = attachmentName, 
                        source = filePath, 
                        type = "text/html"
                    });    
                }
            });

@msergein
Copy link

my workaround: add an attachment and immediately move the added attachment to the last step

        private void AddAttachmentToStepAllure(string name, string type, byte[] content, 
            string fileExtension = "") {
            AllureLifecycle.Instance.AddAttachment(name, type, content, fileExtension);
            AllureLifecycle.Instance.UpdateTestCase(testResult => {
                var stepResult = testResult.steps.Last();
                var attachment = testResult.attachments.Last();
                testResult.attachments.Remove(attachment);
                stepResult.attachments.Add(attachment);
            });
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants