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

[BeforeTestRun] and [BeforeFeature] are not executed #2755

Open
avinashs15 opened this issue Jul 3, 2024 · 2 comments
Open

[BeforeTestRun] and [BeforeFeature] are not executed #2755

avinashs15 opened this issue Jul 3, 2024 · 2 comments
Labels

Comments

@avinashs15
Copy link

avinashs15 commented Jul 3, 2024

SpecFlow Version

3.9.74

Which test runner are you using?

NUnit

Test Runner Version Number

3.14

.NET Implementation

.NET 6.0

Project Format of the SpecFlow project

Classic project format using <PackageReference> tags

    <PackageReference Include="Allure.SpecFlow" Version="2.12.1" />
    <PackageReference Include="DotNetEnv" Version="3.0.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
    <PackageReference Include="Microsoft.Playwright" Version="1.44.0" />
    <PackageReference Include="NUnit" Version="3.14.0" />
    <PackageReference Include="SpecFlow" Version="3.9.74" />
    <PackageReference Include="SpecFlow.NUnit" Version="3.9.74" />
    <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
    <PackageReference Include="SpecFlow.Tools.MsBuild.Generation" Version="3.9.74" />

.feature.cs files are generated using

SpecFlow.Tools.MsBuild.Generation NuGet package

Test Execution Method

Command line – PLEASE SPECIFY THE FULL COMMAND LINE
I use the command dotnet test

SpecFlow Section in app.config or content of specflow.json


{
  "specFlow": {
    "bindingCulture": {
      "name": "en-US"
    },
    "language": {
      "feature": "en-US"
    }
  },
  "stepAssemblies": [
    {
      "assembly": "Allure.SpecFlowPlugin"
    }
  ]
}


Issue Description

I have hooks.cs defined which has [BeforeScenario][AfterScenario][AfterStep][TearDown] which gets executed without an issues but [BeforeTestRun] and [BeforeFeature] are not getting executed.
Im using Playwright libraries which uses aysnc Task methods.

Steps to Reproduce

My hooks.cs looks like this
//Comment shows which hoo attribute gets executed


using Allure.Net.Commons;
using Microsoft.Playwright;
using NUnit.Framework;
using DotNetEnv;

[assembly: Parallelizable(ParallelScope.Fixtures)]
[assembly: LevelOfParallelism(5)]

namespace cicd_playwright_specflow.src.Hooks
{
    [Binding]
    public class Hooks
    {
        public IPage page { get; private set; } = null!; //-> We'll call this property in the tests
        public IAPIRequestContext RequestContext { get; private set; } = null!;
        public IPlaywright playwrightAPI { get; private set; } = null!;

        [BeforeTestRun] //Does not get executed
        public static async Task BeforeTestRun()
        {
            Console.WriteLine("------Before Test Run");
            // Console.WriteLine("----------InBeforeTestRun");
            // // Load environment variables from .env file
            //  // Call the synchronous static method
            await Task.Run (()=>  LoadEnvironmentVariables());
        }

        [BeforeFeature] //Does not get executed
        public static async Task BeforeAPIFeature()
        {
            await Task.Run (() => Console.WriteLine("Before API Feature ----------"));
        }

        public static void LoadEnvironmentVariables()
        {
            // Load environment variables from .env file
            try
            {
                Env.Load();

                //Somecode
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception ------" + e.Data);
            }
        }

        [BeforeScenario("@UI")] // Gets executed
        public async Task RegisterSingleInstancePractitioner()
        {

            //Initialise Playwright
            var playwright = await Playwright.CreateAsync();
            //Initialise a browser - 'Chromium' can be changed to 'Firefox' or 'Webkit'
            var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
            {
                Headless = false, // -> Use this option to be able to see your test running
                Channel = "chrome",
                SlowMo = 0
                //Channel = "firefox"
            });
            //Setup a browser context
            var context1 = await browser.NewContextAsync();

            //Initialise a page on the browser context.
            page = await context1.NewPageAsync();
        }

        [BeforeScenario("@API")] //Gets executed
        public async Task BeforeAPIScenario()
        {
            Console.WriteLine("Running before Scenario for scenario with API tag");
            playwrightAPI = await Playwright.CreateAsync();
            RequestContext = await playwrightAPI.APIRequest.NewContextAsync(new APIRequestNewContextOptions
            {
                IgnoreHTTPSErrors = true
            });
        }

        [AfterScenario("@UI")] //Gets executed
        public async Task CloseBrowsers()
        {
            if (page != null)
                await page.CloseAsync();
        }

        [AfterStep("@UI")] //gets exeecuted
        public async Task AfterStep(ScenarioContext context)
        {
            if (context.TestError != null)
            {
                Console.WriteLine("Step Failed");
                AllureApi.AddAttachment("FailedScreenshot_" + context.StepContext.StepInfo.Text, "image/png", await page.ScreenshotAsync(new() { FullPage = true }));
            }
        }

        [TearDown]
        public async Task TearDown()
        {
            // Gets executed
        }
    }
}

Link to Repro Project

No response

@avinashs15 avinashs15 added the Bug label Jul 3, 2024
@archie098
Copy link

I am also facing same issue. Can someone help please.

@DeLaphante
Copy link

@avinashs15 @archie098 use the latest .net 8 and VS 2022 - Specflow is working perfectly fine for me here in my repo https://github.com/DeLaphante/CynkyAutomation

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

3 participants