3

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);
  }
cnishina
  • 5,016
  • 1
  • 23
  • 40
Sam
  • 28,421
  • 49
  • 167
  • 247

2 Answers2

2

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

bbaia
  • 1,193
  • 7
  • 8
  • Thanks, this sounds promising! You don't happen to have any hints or links how to do this? Otherwise I'll start googling the webhostbuilder. – Sam Jan 17 '17 at 15:09
  • I use .NET Core, but WebHostBuilder does not really host a web Server, it only seems to emulate one so I can run _testserver.CreateClient() and use this. Works nicely to test the API and MVC, but seems not to be usable to test Angular2 SPAs with Protractor :( – Sam Jan 18 '17 at 10:28
  • After some banging my head against a wall I got it to work using WebHostBuilder (copying and adapting the Code from my main program.cs), thanks!! – Sam Jan 25 '17 at 10:23
  • @Sam Can you edit your question and post your solution? – Jon Apr 04 '17 at 16:40
  • 1
    @JonathanFreeland, I just copied program.cs content from my main project to run the server - but I've switched to VS2017 now and do not have the old code at Hand (I was not able to run protractor tests on VS2017 yet). – Sam Apr 05 '17 at 13:00
-3

Trying again. When im developing my webserver is running all the time. So the e2e tests should not need to start the webserver? When it comes to nightly builds the server might need to be started before tests runs. Check TeamServer for niglty build setup. Note: If you look at angular2 homepage you see that they work in an node enviroment. Mayby Protractor.NET is not the way to go? If you want to work with C# i suggest ASP.NET with JQuery. Where you let C# render the html.

Jens Alenius
  • 1,931
  • 2
  • 16
  • 20