You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio C# Selenium运行测试浏览器未启动且测试不显示

问题描述

在Visual Studio中使用C#开发Selenium自动化测试项目,运行历史旧项目时已修复所有编译错误,运行全程无报错,但执行测试时Chrome、Firefox浏览器均无法正常启动。

控制台输出

C:\Program Files\dotnet\dotnet.exe (process 23320) exited with code 0. To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .

调试输出

'dotnet.exe' (CoreCLR: DefaultDomain): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.25\System.Private.CoreLib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'dotnet.exe' (CoreCLR: clrhost): Loaded 'C:\Users\Ariana\OneDrive\Desktop\AutomationPractice\AutomationPractice\bin\Debug\netcoreapp3.1\AutomationPractice.dll'. Symbols loaded.
'dotnet.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.25\System.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The program '[8276]

Hooks类代码

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TestingProjectWebsite.StepDefinitions;
using Microsoft.Azure.WebJobs.Description;
using TechTalk.SpecFlow;

namespace TestingProjectWebsite
{

    public enum BrowserType
    {
        Chrome,
        Firefox
    }

    [TechTalk.SpecFlow.Binding]
    public class Hooks
    {
        private BrowserType _browserType;
        protected static IWebDriver Driver;

        [BeforeScenario]
        public void BeforeScenario()
        {
            var browserType = TestContext.Parameters.Get("Browser", "Chrome");
            _browserType = (BrowserType)Enum.Parse(typeof(BrowserType), browserType);
            if (Driver is null)
            {
                ChooseDriverInstance(_browserType);
                Driver.Manage().Window.Maximize();
            }

            Driver.Navigate().GoToUrl("http://automationpractice.com/index.php");

        }

        public void ChooseDriverInstance(BrowserType browserType)
        {
            switch (browserType)
            {
                case BrowserType.Chrome:
                    Driver = new ChromeDriver();
                    break;
                case BrowserType.Firefox:
                    Driver = new FirefoxDriver();
                    break;
            }
        }

        [AfterScenario]
        public void AfterScenario()
        {
            if (!(Driver is null))
            {
                Driver.Quit();
                Driver = null;
            }
        }
    }
}

其他异常

  • 测试资源管理器(Test Explorer)中未展示任何测试用例,参考截图:
    测试资源管理器截图

解决方案

根因

测试资源管理器未加载任何测试用例,说明NUnit+SpecFlow测试适配器依赖缺失或版本不兼容,测试运行器未识别到项目内的测试逻辑,程序启动后直接退出返回退出码0,浏览器驱动初始化逻辑根本没有被执行,因此不会启动浏览器。

修复步骤

  • 清理无用引用,补全测试依赖

    • 首先删除代码中无用的using Microsoft.Azure.WebJobs.Description;引用,卸载项目中无关的Azure Functions相关NuGet包,避免程序集加载冲突。
    • 通过NuGet包管理器安装以下和netcoreapp3.1框架兼容的依赖,注意不要选择过新的版本,高版本仅支持.NET 6+,会出现兼容性问题:
      • Microsoft.NET.Test.Sdk(17.1.x版本适配.NET Core 3.1)
      • NUnit(3.13.x版本)
      • NUnit3TestAdapter(4.2.x版本)
      • SpecFlow.NUnit(3.9.x版本,和项目内已安装的SpecFlow版本保持一致)
      • 浏览器驱动包:Selenium.WebDriver.ChromeDriver(大版本和本地Chrome浏览器一致)、Selenium.WebDriver.GeckoDriver(大版本和本地Firefox一致),如果使用Selenium 4.x版本可省略这两个包,内置的Selenium Manager会自动下载匹配版本的驱动。
  • 清理项目缓存重新生成
    关闭Visual Studio,删除项目根目录下的.vs隐藏文件夹、bin、obj文件夹,重新打开项目后执行NuGet包还原,重新生成整个解决方案。

  • 验证测试识别
    打开测试资源管理器,点击顶部刷新按钮,此时所有SpecFlow场景对应的测试用例都会正常展示。如果仍然看不到用例,检查测试资源管理器的筛选配置,关闭“仅显示已运行测试”等过滤规则。

  • 可选优化:修复驱动并发问题
    当前代码中Driver定义为静态变量,多场景并行执行时会出现实例冲突,建议将Driver改为SpecFlow Scenario上下文注入的实例,避免测试间互相干扰。


内容的提问来源于stack exchange,提问作者Uti Anm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.03 05:45:40