如何在 中使用 Selenium C# 记录测试执行.网芯



我正在尝试使用SeleniumC#MSTest.NetCore中记录测试执行

尝试使用 Microsoft.Express.Encoder nuget pkg,这会在创建 ScreenCapture Job 实例时引发错误

"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:UsersSunnysourcereposScreenRecorderNUnitTestProject1NUnitTestProject1.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:UsersSunnysourcereposScreenRecorderNUnitTestProject1NUnitTestProject1.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 Studio2019ProfessionalMSBuildCurrentBinMicrosoft.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:UsersSunny.nugetpackagesnunit.video.recorder1.0.0libnet452NunitVideoRecorder.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:UsersSunnysourcereposScreenRecorderNUnitTestProject1binDebugnetcoreapp2.2NUnitTestProject1.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:UsersSunnysourcereposScreenRecorderNUnitTestProject1binDebugnetcoreapp2.2NUnitTestProject1.dll
[8/9/2019 4:18:31.806 PM Informational]    NUnit failed to load C:UsersSunnysourcereposScreenRecorderNUnitTestProject1binDebugnetcoreapp2.2NUnitTestProject1.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:UsersSunnysourcereposScreenRecorderNUnitTestProject1binDebugnetcoreapp2.2NUnitTestProject1.dll
[8/9/2019 4:18:31.936 PM Informational] ========== Run finished: 0 tests run (0:00:01.3376823) ==========

如果您处于安装了Visual Studio的Windows环境中,则可以在.runsettings文件中使用数据收集器。

<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 之类的东西实现您自己的录音机调用 ffmpeg.net 请参阅此问题。

这是启动和停止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();

所以我遇到了同样的麻烦,并设法为 .netcore 硒 Nunit(在您的情况下为 mstest(测试创建了一个简单的解决方案。Microsoft编码器已被弃用,所以我选择了本地屏幕录像机ffmpeg。只需下载 FFMPEG 并通过从 Selenium C# 测试中调用此 exe 开始录制。请注意,此方法仅适用于本地执行。您可以修改它以满足您的其他需求,如硒网格或远程执行。

步骤1:- 在本地机器中下载ffmpeg (https://ffmpeg.org/download.html( 按照本文下载(https://www.wikihow.com/Install-FFmpeg-on-Windows(,请手动检查一次您的ffmpeg是否正常工作。

第 2 步:- 创建一个 bat 文件来启动 ffmpeg(您将从 c# 代码调用此 batfile( 蝙蝠文件的内容

@echo off
mkdir "C:ffmpegffmpeg"
set outputpath="path to your recording output folder"\record_20%date:~-2,2%_%date:~-10,2%_%date:~-7,2%_%time:~-11,2%%time:~-8,2%.mp4
ffmpeg -f gdigrab -framerate 30 -i desktop "%outputpath%"

第 3 步:- 在硒测试中,创建一个记录类和 2 个方法来开始和停止记录(在我的情况下,我在所有测试之前启动了蝙蝠文件,例如在 onetimesetup 属性中调用 executeScreenRecordingBatFile 方法来开始录制并在一次性拆解中调用 StopScreenRecording 方法(下面的示例代码。

using System.Diagnostics;
using System.IO;
namespace FunctionalTests
{
public class Recording
{


public static Process process;

public static void executeScreenRecordingBatFile()
{
try
{
process = new Process();
process.StartInfo.FileName = @"C:Program Files (x86)StartScreenRecording.bat";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;// this required to send input to the current process window.
bool started =  process.Start();
if (started==true) 
{
Console.WriteLine("Bat file started");                                    

}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace.ToString());
throw;
}
}
public static void StopScreenRecording() 
{
StreamWriter myStreamWriter = process.StandardInput; // this required to send StandardInput stream, nothing fancy 
myStreamWriter.WriteLine("q"); //this will send q as an input to the ffmpeg process window making it stop , please cross check in task manager once if the ffmpeg is still running or closed.
}

}

相关内容

  • 没有找到相关文章

最新更新