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

时间:2015-07-29 00:17:16

标签: asp.net selenium asp.net-core asp.net-core-mvc

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.

class Foo {
  private:
     Foo() = default;
};

The server starts and stops fine, but I get an HTTP 500 when I hit a page, with a System.InvalidOperationException:
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(); } } }

How do I specify that I want to run Startup.cs from the "web" project, not the "web.Tests" project?

2 个答案:

答案 0 :(得分:2)

通过切换到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();
        }
    }
}

(显然将Path.Combine(...)中的参数替换为您的网络应用所在的位置)

答案 1 :(得分:1)

在使用DotNet Core进行了一些跟踪和错误后,我就想到了这一点。请注意,我的路径与您的路径略有不同,因为我将测试项目与我的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();            
    }

杀死已启动的进程(例如,DotNet.exe和webproject exe)似乎是确保Kestral停止的技巧。