测试类清理失败 - 在虚拟机中运行 Specflow Selenium 测试时清理不起作用 (Azure Pipeline)

Test Class Cleanup Failure - Cleanup not working when running Specflow Selenium tests in a Virtual Machine (Azure Pipeline)

提问人:João Pacheco 提问时间:11/3/2023 更新时间:11/3/2023 访问量:11

问:

我在 .NET 项目中实现了一些 Specflow selenium 测试,并使它们在虚拟机的 Azure 管道中自动运行。一切都运行良好,但现在我有大约 20 个测试用例,当它们在我的本地机器上运行时,它们都通过了,但如果我按预期在虚拟机中运行测试,它将失败并给出以下错误:(示例)

 Failed Verify Monitoring Module Porto [1 ms]
  Error Message:
   [Test Class Cleanup Failure (Armis.EODREAM.QualityAssurance.Specs.Features.MonitoringModule.Map.VerifyMonitoringModuleFeature)]: System.NullReferenceException : Object reference not set to an instance of an object.
  Stack Trace:
     at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.GetHookContainer(HookType hookType)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireEvents(HookType hookType)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnFeatureEnd()
   at TechTalk.SpecFlow.TestRunner.OnFeatureEnd()
   at Armis.EODREAM.QualityAssurance.Specs.Features.MonitoringModule.Map.VerifyMonitoringModuleFeature.FeatureTearDown()
   at Armis.EODREAM.QualityAssurance.Specs.Features.MonitoringModule.Map.VerifyMonitoringModuleFeature.FixtureData.System.IDisposable.Dispose()

我已经尝试限制 xUnit 运行器 JSON 文件中的线程和并行执行,但 specflow 没有任何工作按预期工作。 这是我的 Hooks.cs 类,我在场景之前和之后初始化并释放驱动程序:

[Binding]
public class Hooks
{
    public IWebDriver driver;
    public TestConfiguration envConfiguration { get; set; }

    [BeforeScenario]
    public void InitializeWebDriver(ScenarioContext scenarioContext)
    {
        var driverContext = new WebDriverContext();
        var driver = driverContext.GetDriver();
        
        string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        environment = "Dev";
        if (string.IsNullOrWhiteSpace(environment))
        {
            Console.WriteLine("Environment variable ASPNETCORE_ENVIRONMENT not set.");
            return;
        }
        

        Console.WriteLine($"Environment: {environment}");

        envConfiguration = ConfigurationService.GetConfiguration(environment);
        scenarioContext.Set(envConfiguration, "TestConfiguration");

        // Set global timeouts
        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
        driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(60);
        driver.Manage().Cookies.DeleteAllCookies();
        driver.Manage().Window.Maximize();

        // Register the WebDriver instance with SpecFlow's DI container
        scenarioContext.Set(driver, "WebDriver");

        // Create a WebDriverWait instance and register it as well (optional)
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
        scenarioContext.Set(wait, "WebDriverWait");
    }


    [AfterScenario]
    public void TearDownWebDriver(ScenarioContext scenarioContext)
    {
        if (scenarioContext.TryGetValue<IWebDriver>("WebDriver", out var driver))
        {
            Console.WriteLine("Tearing down WebDriver...");

            try
            {
                driver.Quit();
                driver.Dispose();
                Console.WriteLine("WebDriver successfully disposed.");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred while disposing the WebDriver: {ex.Message}");
            }
        }

        Console.WriteLine("Scenario Finished!");
    }
}

我真的不知道发生了什么,我没有想法了,所以如果你们中的任何人可以帮助我,我很感激!

我已经尝试创建一个xunit.runner.json文件:

{
  "$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
  "parallelizeTestCollections": false,
  "maxParallelThreads": 2
}

并创建一个功能更强大的虚拟机(我有一个具有 4GB RAM 和 2vcpus 的 VM,另一个具有 16GB RAM 和 4vcppu)。

测试以前总是运行良好。

C# Selenium-Chromedriver specflow

评论


答: 暂无答案