ChromiumWebBrowser Offscreen 不适用于 MsTest/NUnit

ChromiumWebBrowser Offscreen does not work with MsTest/NUnit

我正在尝试在 OffScreen 模式下使用 MsTest 单元测试框架中的 ChromiumWebBrowser 来提供一些测试...但是 Chromium 构造函数总是崩溃,我无法用它做任何事情。

我已经在 github 上的类似话题中留下了评论:

Issue #1

Isuee #2

我也尝试在 gitter 频道上寻求帮助。 我创建了 MsTest 项目并添加了使用 Chromium 的简单方法:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CefSharp;
using CefSharp.OffScreen;
using System.Threading;

namespace ChromTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            ChromiumWebBrowser browser = null;

            // Create the offscreen Chromium browser.
            using (browser = new ChromiumWebBrowser("http://google.com")) 
            {

            }

            Cef.Shutdown();
        }
    }
}

我的解决方案结构:

我的测试设置文件(将所有依赖项部署到测试引擎 "TestResults...\Out folder" 的工作目录):

<?xml version="1.0" encoding="UTF-8"?>
<TestSettings name="Release" id="88aeb968-8fa1-409d-95e4-4d1cb52532e2" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
  <Description>These are default test settings for a local test run.</Description>
  <Deployment>
    <DeploymentItem filename="bin\x64\Release\natives_blob.bin" />
    <DeploymentItem filename="bin\x64\Release\libcef.dll" />
    <DeploymentItem filename="bin\x64\Release\cef.pak" />
    <DeploymentItem filename="bin\x64\Release\CefSharp.OffScreen.xml" />
    <DeploymentItem filename="bin\x64\Release\cef_200_percent.pak" />
    <DeploymentItem filename="bin\x64\Release\cef_100_percent.pak" />
    <DeploymentItem filename="bin\x64\Release\CefSharp.dll" />
    <DeploymentItem filename="bin\x64\Release\cef_extensions.pak" />
    <DeploymentItem filename="bin\x64\Release\widevinecdmadapter.dll" />
    <DeploymentItem filename="bin\x64\Release\snapshot_blob.bin" />
    <DeploymentItem filename="bin\x64\Release\CefSharp.BrowserSubprocess.Core.pdb" />
    <DeploymentItem filename="bin\x64\Release\CefSharp.OffScreen.dll" />
    <DeploymentItem filename="bin\x64\Release\CefSharp.xml" />
    <DeploymentItem filename="bin\x64\Release\CefSharp.BrowserSubprocess.exe" />
    <DeploymentItem filename="bin\x64\Release\CefSharp.Core.dll" />
    <DeploymentItem filename="bin\x64\Release\devtools_resources.pak" />
    <DeploymentItem filename="bin\x64\Release\libEGL.dll" />
    <DeploymentItem filename="bin\x64\Release\CefSharp.BrowserSubprocess.Core.dll" />
    <DeploymentItem filename="bin\x64\Release\CefSharp.pdb" />
    <DeploymentItem filename="bin\x64\Release\CefSharp.Core.pdb" />
    <DeploymentItem filename="bin\x64\Release\CefSharp.BrowserSubprocess.pdb" />
    <DeploymentItem filename="bin\x64\Release\icudtl.dat" />
    <DeploymentItem filename="bin\x64\Release\CefSharp.Core.xml" />
    <DeploymentItem filename="bin\x64\Release\d3dcompiler_47.dll" />
    <DeploymentItem filename="bin\x64\Release\chrome_elf.dll" />
    <DeploymentItem filename="bin\x64\Release\libGLESv2.dll" />
    <DeploymentItem filename="bin\x64\Release\CefSharp.OffScreen.pdb" />
    <DeploymentItem filename="bin\x64\Release\locales\kn.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\ml.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\lt.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\et.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\da.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\cs.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\sk.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\he.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\id.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\sl.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\fr.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\lv.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\de.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\tr.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\fa.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\zh-TW.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\hr.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\ar.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\pt-BR.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\nl.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\vi.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\gu.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\hu.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\ta.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\fi.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\ms.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\sr.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\en-US.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\el.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\zh-CN.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\en-GB.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\pl.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\es.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\th.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\bg.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\pt-PT.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\it.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\te.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\fil.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\bn.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\ro.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\sw.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\hi.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\nb.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\ca.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\sv.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\uk.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\ko.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\mr.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\ja.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\es-419.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\am.pak" outputDirectory="locales\" />
    <DeploymentItem filename="bin\x64\Release\locales\ru.pak" outputDirectory="locales\" />
  </Deployment>
  <Execution hostProcessPlatform="MSIL">
    <TestTypeSpecific>
      <UnitTestRunConfig testTypeId="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b">
        <AssemblyResolution>
          <TestDirectory useLoadContext="true" />
        </AssemblyResolution>
      </UnitTestRunConfig>
    </TestTypeSpecific>
    <AgentRule name="Execution Agents">
    </AgentRule>
  </Execution>
  <Properties />
</TestSettings>

我从 Visual Studio Test Explorer 运行 测试并得到这样的错误:

请帮忙,需要任何支持或想法...

可以使用 mstest 方法,但不够完整。也许,它会帮助某人。下面的方法类似于 NUnit 中描述的:https://github.com/cefsharp/CefSharp/issues/700#issuecomment-297209620

测试逻辑可以封装在控制台应用程序中,方便调试。 测试应用程序可以在 mstest.exe

的帮助下从控制台执行

MSTest.exe 可以 运行 在与 cefsharp 进程相同的应用程序域中进行测试,但仅限于 x86 模式。

命令示例:

>> ChromTests\bin\x86\Release>mstest.exe /testcontainer:chromtests.dll /testsettings:..\..\..\release.testsettings /noisolation
  • /testcontainer - 测试 dll 名称
  • /testsettings - 设置文件,类似于我在上面发布的文件
  • /noisolation - 让我们有可能 运行 在同一个应用程序域中测试和 cefsharp

有测试运行ning vstest.console.exe的工具,它也应该运行默认在非隔离模式下测试,但是这是行不通的。它有标志/inisolation,但似乎没用。

这是我描述的方法示例:

// Console app class for running Chromium:

using CefSharp;
using CefSharp.OffScreen;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading;

namespace ConsoleWebChromiumSample
{
    public class ChromiumRunner
    {
        private ChromiumWebBrowser browser = null;

        public void RunChromiumDemo()
        {
            var settings = new CefSettings()
            {
                MultiThreadedMessageLoop = true
            };

            //Perform dependency check to make sure all relevant resources are in our output directory.
            Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);

            // Create the offscreen Chromium browser.
            browser = new ChromiumWebBrowser("");
            browser.BrowserInitialized += BrowserInitialized;

            while (!browser.IsBrowserInitialized)
            {
                Thread.Sleep(100);
            }

            Cef.Shutdown();
        }

        private void BrowserInitialized(object sender, EventArgs e)
        {
            Assert.IsTrue(browser.IsBrowserInitialized);
            browser.BrowserInitialized -= BrowserInitialized;
        }
    }
}

单元测试项目代码:

using ConsoleWebChromiumSample;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ChromTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestChromiumDemo()
        {
            ChromiumRunner runner = new ChromiumRunner();
            runner.RunChromiumDemo();
        }
    }
}

下一步 - 从 Visual Studio 为 运行 添加适配器。关于如何的示例:write a visual studio unit test adapter/