angularvisual-studio-2015asp.net-coreprotractor-net

Testing ASP.NET Core app using Protractor in Visual Studio 2015


I've got a ASP.NET Core Project based on the angular2 template from the core template pack. I added a second project with xunit and protractor to run unit test against my main app.

Both work nicely alone, there is just one small thing I miss:

When I want to run the protractor tests I need to host the main project somewhere manually. If I don't host the main project manually it does not run, so all the Tests fail because my page is unreachable.

I probably need to start the main app / project somehow from the test, but I got no idea how. All examples for protractor run against some already running Homepages, none does run against some other project.

How do I start the main ASP.NET Core Project from my test Project, so it is running for testing?

Weird thing is: after I started the main app once using Strg+F5 protractor with ChromeDriver will find the homepage of the app. But I need to run it once manually for the tests to work...

public class EndToEndTests : IDisposable
{
  private const string Url = "https://localhost:44391/";
  private readonly IWebDriver _driver;
  private readonly NgWebDriver _browser;

  public EndToEndTests()
  {
// TODO: somehow get the ASP.NET Core project up and running
    _driver = new ChromeDriver(@"\test\ChromeDriver");
    _driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(10));
    _browser = new NgWebDriver(_driver);
  }

  public void Dispose()
  {
    _driver.Quit();
  }

  [Fact(DisplayName = "Has a title")]
  public void HasATitle()
  {
    _browser.Url = Url;
    Assert.Equal("My page", _browser.Title);
  }

Solution

  • You need to self-host your WebApp in your SetUp method.

    With .NET 4.5 Owin based applications, you need to use the Microsoft.Owin.Hosting package with WebApp.Start method.

    With .NET Core, you need to use WebHostBuilder