Search code examples
c#selenium.net-coreselenium-chromedriver

How to Record test execution using Selenium C# in .NetCore


I'm Trying to record test execution using Selenium, C#, MSTest in .NetCore

Tried using Microsoft.Express.Encoder nuget pkg, which throws error on creating instance of 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) ==========

Solution

  • If you are in a windows environment with Visual Studio installed you can use a datacollector in the .runsettings file.

    <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>
    

    If you use the datacollector I suggest outputting a TRX file log from the test runner so that you can map what video goes with what test.

    Other options include using Selenium Grid or implementing your own recorder call via something like ffmpeg using ffmpeg.net see this issue.

    Here is a direct way to start and stop 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();