M1芯片Mac无法运行基于BrowserStack SDK的C#自动化测试套件
问题:M1芯片Mac上BrowserStack SDK迁移后Appium+NUnit测试无法运行
环境信息
- C#自动化测试项目,基于Appium、NUnit框架
- 已完成BrowserStack从旧版集成到SDK的迁移
- 运行环境:M1芯片Mac + VS Code,项目在Intel芯片Mac上可正常执行
报错信息
Starting test execution, please wait... A total of 1 test files matched the specified pattern. Failed to load extensions from file '/Users/darko/Developer/project_app_tests/IOS/bin/Debug/net6.0/ZZStack.TestAdapter.dll'. Please use /diag for more information. Setup failed for test fixture IOS.Tests.DownloadTest System.TypeInitializationException : The type initializer for '<Module>' threw an exception. ----> HarmonyLib.HarmonyException : Patching exception in method System.Void OpenQA.Selenium.Remote.HttpCommandExecutor::.ctor(System.Uri addressOfRemoteServer, System.TimeSpan timeout, System.Boolean enableKeepAlive) ----> System.NotImplementedException : The method or operation is not implemented. StackTrace: at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.ConstructorInfo.Invoke(Object[] parameters) at NUnit.Framework.Internal.Reflect.Construct(Type type, Object[] arguments) at NUnit.Framework.Internal.TypeWrapper.Construct(Object[] args) at NUnit.Framework.Internal.Commands.ConstructFixtureCommand.<.ctor>b__0_0(TestExecutionContext context) at NUnit.Framework.Internal.Commands.BeforeTestCommand.Execute(TestExecutionContext context) at NUnit.Framework.Internal.Execution.CompositeWorkItem.PerformOneTimeSetUp() --HarmonyException at HarmonyLib.PatchClassProcessor.ReportException(Exception exception, MethodBase original) at HarmonyLib.PatchClassProcessor.Patch() at HarmonyLib.Harmony.<PatchAll>b__10_0(Type type) at HarmonyLib.CollectionExtensions.Do[T](IEnumerable`1 sequence, Action`1 action) at HarmonyLib.Harmony.PatchAll(Assembly assembly) at BrowserstackPatcher.DoPatching() in /Users/darko/Developer/project_app_tests/IOS/obj/Debug/net6.0/BrowserStackPatch.cs:line 114 at Initializer.Run() in /Users/darko/Developer/project_app_tests/IOS/obj/Debug/net6.0/BrowserStackPatch.cs:line 91 at .cctor() --NotImplementedException at HarmonyLib.PatchFunctions.UpdateWrapper(MethodBase original, PatchInfo patchInfo) at HarmonyLib.PatchClassProcessor.ProcessPatchJob(Job job)
已尝试但无效的操作
- 安装.NET 6/7/8不同版本
- 卸载后重新配置.NET环境(无法确认是否彻底清除)
- 安装BrowserStack文档指定的Arm64版本.NET
- 尝试配置x64别名并执行
dotnet64 test(别名配置可能有误)
核心原因分析
报错根源是HarmonyLib在Arm64架构下的方法补丁逻辑未实现,BrowserStack SDK依赖的HarmonyLib版本对M1的Arm64支持不完善,导致HttpCommandExecutor构造方法的补丁操作失败。
解决步骤
1. 强制项目以x64架构运行
M1 Mac默认用Arm64运行.NET程序,而BrowserStack SDK的补丁逻辑仅适配x64,可通过以下方式强制切换:
- 修改项目文件(
.csproj),添加架构指定:<PropertyGroup> <PlatformTarget>x64</PlatformTarget> <RuntimeIdentifier>osx-x64</RuntimeIdentifier> </PropertyGroup> - 清理并重建项目:
dotnet clean dotnet build - 运行测试时明确指定架构:
dotnet test -r osx-x64
2. 升级BrowserStack SDK及依赖库
检查并升级项目中BrowserStack相关包到最新稳定版,同时确保依赖的HarmonyLib版本支持Arm64:
- 更新
BrowserStack.TestAdapter等相关NuGet包至最新版 - 若直接引用HarmonyLib,升级至v2.2.2及以上版本(该版本修复了部分Arm64补丁问题)
3. 彻底清理并重新安装x64版本.NET
之前的卸载可能残留文件,按以下步骤彻底清理:
- 删除所有.NET相关文件:
sudo rm -rf /usr/local/share/dotnet sudo rm -rf ~/.dotnet sudo rm -rf /etc/dotnet - 下载并安装x64版本的.NET 6/7/8(不要选择Arm64版本)
- 验证安装结果:
确认输出中dotnet --infoRID包含osx-x64,架构显示为x64
4. 临时禁用BrowserStack自动补丁(应急方案)
若上述方法无效,可尝试手动关闭BrowserStack的Harmony补丁(此方法可能导致部分SDK功能失效,仅作临时测试用):
BrowserStackOptions options = new BrowserStackOptions(); options.DisableAutoPatch = true;
内容的提问来源于stack exchange,提问作者darko_s
相关产品推荐
相关产品推荐

