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

泪湿孤枕 提交于 2019-12-04 14:57:13

This was fixed by switching to Kestrel as the host -- especially since Kestrel is now the only supported host in 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)

After a bit of trail and error with DotNet Core, here is what I came up with. Note that my pathing is a little different to yours as I have my test project separated from my web project.

   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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!