2

我正在尝试在 OffScreen 模式下使用 MsTest 单元测试框架中的 ChromiumWebBrowser 来提供一些测试......但是 Chromium 构造函数一直崩溃,我对此无能为力。

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

问题 #1

发行人#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();
        }
    }
}

我的解决方案结构:

我的解决方案结构

我的 testsettings 文件(将所有依赖项部署到测试引擎“TestResults...\Out 文件夹”的工作目录中):

<?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 测试资源管理器运行测试并得到这样的错误: 测试引擎在测试运行时崩溃

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

4

1 回答 1

1

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

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

这是我描述的方法示例:

// 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 运行它。有关如何:编写 Visual Studio 单元测试适配器/的示例

于 2017-09-08T09:04:59.200 回答