ChromiumWebBrowser Offscreen不适用于MsTest / NUnit

时间:2017-09-06 12:51:24

标签: c# chromium chromium-embedded cefsharp cefsharp.offscreen

我试图在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();
        }
    }
}

我的解决方案结构:

My solution structure

我的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 Test Explorer运行测试并得到这样的错误: test engine crashes on test run

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

1 个答案:

答案 0 :(得分:0)

使用mstest的方法是可行的,但它还不够。也许,它会帮助某人。以下方法类似于在https://github.com/cefsharp/CefSharp/issues/700#issuecomment-297209620

中描述的NUnit

测试逻辑可以封装在Console应用程序中并轻松调试。 测试应用程序可以在mstest.exe的帮助下从控制台执行

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

命令示例:

>> ChromTests\bin\x86\Release>mstest.exe /testcontainer:chromtests.dll /testsettings:..\..\..\release.testsettings /noisolation
  • / testcontainer - test dll name
  • / 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运行它。有关如何:write a visual studio unit test adapter/

的示例