首页 > 解决方案 > 如何在 .NetCore 中使用 Selenium C# 记录测试执行

问题描述

我正在尝试使用Selenium, C#, MSTestin记录测试执行.NetCore

尝试使用 Microsoft.Express.Encoder nuget pkg,它在创建 ScreenCaptureJob 实例时引发错误

 "System.BadImageFormatException: Could not load file or assembly 'Microsoft.Expression.Encoder, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. An attempt was made to load a program with an incorrect format." To resolve this, tried running test using x86, x64 and Any CPU, but none of them works.

Also tried using Nunit.Video.Recorder, with Nunit Framework, when did this, test are not discovered, tried changing to x86, x64 and Any CPU




1>C:\Users\Sunny\source\repos\ScreenRecorder\NUnitTestProject1\NUnitTestProject1.csproj : warning NU1701: Package 'Nunit.Video.Recorder 1.0.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.2'. This package may not be fully compatible with your project.
1>C:\Users\Sunny\source\repos\ScreenRecorder\NUnitTestProject1\NUnitTestProject1.csproj : warning NU1701: Package 'SharpAvi 2.1.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.2'. This package may not be fully compatible with your project.
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3270: There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "C:\Users\Sunny\.nuget\packages\nunit.video.recorder\1.0.0\lib\net452\NunitVideoRecorder.dll", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project.
1>NUnitTestProject1 -> C:\Users\Sunny\source\repos\ScreenRecorder\NUnitTestProject1\bin\Debug\netcoreapp2.2\NUnitTestProject1.dll
1>Done building project "NUnitTestProject1.csproj".
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

[8/9/2019 4:18:30.549 PM Informational] ---------- Run started ----------
[8/9/2019 4:18:31.686 PM Informational] NUnit Adapter 3.11.0.0: Test execution started
[8/9/2019 4:18:31.699 PM Informational] Running all tests in C:\Users\Sunny\source\repos\ScreenRecorder\NUnitTestProject1\bin\Debug\netcoreapp2.2\NUnitTestProject1.dll
[8/9/2019 4:18:31.806 PM Informational]    NUnit failed to load C:\Users\Sunny\source\repos\ScreenRecorder\NUnitTestProject1\bin\Debug\netcoreapp2.2\NUnitTestProject1.dll
[8/9/2019 4:18:31.807 PM Informational] NUnit Adapter 3.11.0.0: Test execution complete
[8/9/2019 4:18:31.810 PM Warning] No test matches the given testcase filter `FullyQualifiedName=Tests.Tests.Test1` in C:\Users\Sunny\source\repos\ScreenRecorder\NUnitTestProject1\bin\Debug\netcoreapp2.2\NUnitTestProject1.dll
[8/9/2019 4:18:31.936 PM Informational] ========== Run finished: 0 tests run (0:00:01.3376823) ==========

标签: c#selenium.net-coreselenium-chromedriver

解决方案


如果您在安装了 Visual Studio 的 Windows 环境中,则可以在 . 运行设置文件。

<DataCollector uri="datacollector://microsoft/VideoRecorder/1.0" assemblyQualifiedName="Microsoft.VisualStudio.TestTools.DataCollection.VideoRecorder.VideoRecorderDataCollector, Microsoft.VisualStudio.TestTools.DataCollection.VideoRecorder, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" friendlyName="Screen and Voice Recorder">
<!--Video data collector was introduced in Visual Studio 2017 version 15.5 -->
</DataCollector>

如果您使用数据收集器,我建议从测试运行器输出 TRX 文件日志,以便您可以映射哪些视频与哪些测试一起使用。

其他选项包括使用 Selenium Grid 或使用 ffmpeg.net 通过类似 ffmpeg 的方式实现您自己的记录器调用,请参阅此问题

这是启动和停止ffmpeg的直接方法

    var si = new ProcessStartInfo
    {
        Arguments = "-y -f gdigrab -framerate 10 -video_size 1920x1080 -i desktop output.mp4",
        FileName = _fixture.FFmpegPath,
        RedirectStandardInput = true,            
    };

    var ffmpegProcess = Process.Start(si);
    await Task.Delay(15000);
    ffmpegProcess.StandardInput.Write("q");
    ffmpegProcess.WaitForExit();

推荐阅读