简体   繁体   English

在ASP.NET 5 / MVC 6上运行Selenium测试之前启动IIS Express

[英]Starting IIS Express before running Selenium tests on ASP.NET 5 / MVC 6

I have a VS solution with a "web" project (ASP.NET v5) and a "web.Tests" project (xunit.net 2.1beta) -- one of the tests is checking the rendered pages, and I'm trying to have the test bring up the site automatically, so I don't need to have it running separately/manually. 我有一个带有“web”项目的VS解决方案(ASP.NET v5)和一个“web.Tests”项目(xunit.net 2.1beta) - 其中一个测试是检查渲染的页面,我正在尝试让测试自动调出网站,所以我不需要单独/手动运行。

namespace web.Tests
{
  public abstract class BrowserTest : IDisposable
  {
    protected readonly IisExpress server;
    protected readonly IWebDriver driver;

    protected BrowserTest()
    {
      var project = ProjectLocation.FromPath(Path.Combine(SolutionRoot, "src", "web", "wwwroot"));
      var app = new WebApplication(project, 8080);
      server = new IisExpress(app);
      server.Start();
      driver = new PhantomJSDriver();
    }

    public void Dispose()
    {
      server.Stop();
    }
  }
}

The server starts and stops fine, but I get an HTTP 500 when I hit a page, with a System.InvalidOperationException: 服务器启动和停止正常,但当我点击页面时,我得到一个HTTP 500,带有System.InvalidOperationException:
A type named 'StartupProduction' or 'Startup' could not be found in assembly 'web.Tests'.

How do I specify that I want to run Startup.cs from the "web" project, not the "web.Tests" project? 如何指定我想从“web”项目运行Startup.cs,而不是“web.Tests”项目?

This was fixed by switching to Kestrel as the host -- especially since Kestrel is now the only supported host in ASP.NET 5 通过切换到Kestrel作为主机来解决这个问题 - 特别是因为Kestrel现在是ASP.NET 5中唯一受支持的主机

using System;
using System.Diagnostics;
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;

namespace Test
{
    public abstract class PhantomFixture : IDisposable
    {
        public readonly IWebDriver driver;
        private readonly Process server;

        protected PhantomFixture()
        {
            server = Process.Start(new ProcessStartInfo
            {
                FileName = "dnx.exe",
                Arguments = "web",
                WorkingDirectory = Path.Combine(Directory.GetCurrentDirectory(), "..", "Web")
            });
            driver = new PhantomJSDriver();
        }

        public void Dispose()
        {
            server.Kill();
            driver.Dispose();
        }
    }
}

(obviously replacing the arguments in Path.Combine(...) with where your web app is located) (显然将Path.Combine(...)的参数替换为您的Web应用程序所在的位置)

After a bit of trail and error with DotNet Core, here is what I came up with. 在使用DotNet Core进行了一些跟踪和错误之后,我就想到了这一点。 Note that my pathing is a little different to yours as I have my test project separated from my web project. 请注意,我的路径与您的路径略有不同,因为我将测试项目与Web项目分开。

   private System.Diagnostics.Process _WebServerProcess;

   [OneTimeSetUp]
    public void SetupTest()
    {

       _WebServerProcess = new System.Diagnostics.Process
       {
           EnableRaisingEvents = false,
           StartInfo = {
               WorkingDirectory = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "MyWebProjectName"),
               FileName = $"dotnet.exe",
               Arguments = " run"
           }
       };
    }
    private void KillWebServer()
    {            
        IEnumerable<Process> processes =  Process.GetProcesses()
            .Where(p => p.ProcessName == "MyWebProjectName.exe" && p.HasExited == false)
            .AsEnumerable();

        foreach (Process process in processes)         
            process.Kill();

        if (_WebServerProcess != null)
        {
            if (!_WebServerProcess.HasExited)
                _WebServerProcess.Kill();
            _WebServerProcess = null;
        }
    }

    public void Dispose()
    {
        KillWebServer();            
    }

Killing both the process that was started (eg, DotNet.exe & the webproject exe) seems be be the trick to ensuring that Kestral stopped. 杀死已启动的进程(例如,DotNet.exe和webproject exe)似乎是确保Kestral停止的技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM