Running 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 to check the pages displayed, m trying to run the test automatically opens the site, so I don't need to run it separately / manually.
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 HTTP 500 when I hit the page with 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 and not the "web.Tests" project?
source to share
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 arguments in Path.Combine(...)
where your web app resides)
source to share
After a little trail and a bug with DotNet Core, here's what I came up with. Please note that my path is slightly different from yours as my test project is separate 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 started process (eg DotNet.exe and webproject exe) seems to be a trick to stop Kestral.
source to share