Refactor E2E tests fixtures/base classes for better composability

This commit is contained in:
Steve Sanderson 2017-12-07 10:39:46 +00:00
parent aedb146ea4
commit a59bfa3cb9
9 changed files with 107 additions and 115 deletions

View File

@ -0,0 +1,18 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using OpenQA.Selenium;
using Xunit;
namespace Microsoft.Blazor.E2ETest.Infrastructure
{
public class BrowserTestBase : IClassFixture<BrowserFixture>
{
public IWebDriver Browser { get; }
public BrowserTestBase(BrowserFixture browserFixture)
{
Browser = browserFixture.Browser;
}
}
}

View File

@ -1,29 +1,27 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using Microsoft.AspNetCore; using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using System;
using System.IO;
namespace Microsoft.Blazor.E2ETest.Infrastructure namespace Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures
{ {
public class AspNetServerFixture : ServerFixture public class AspNetSiteServerFixture<TStartup> : WebHostServerFixture
where TStartup: class
{ {
public void Start(Type startupType) protected override IWebHost CreateWebHost()
{ {
var sampleSitePath = Path.Combine( var sampleSitePath = Path.Combine(
FindSolutionDir(), FindSolutionDir(),
"samples", "samples",
startupType.Assembly.GetName().Name); typeof(TStartup).Assembly.GetName().Name);
var host = WebHost.CreateDefaultBuilder() return WebHost.CreateDefaultBuilder()
.UseStartup(startupType) .UseStartup<TStartup>()
.UseContentRoot(sampleSitePath) .UseContentRoot(sampleSitePath)
.UseUrls("http://127.0.0.1:0") .UseUrls("http://127.0.0.1:0")
.Build(); .Build();
Start(host);
} }
} }
} }

View File

@ -1,37 +1,27 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Threading; using System.Threading;
namespace Microsoft.Blazor.E2ETest.Infrastructure namespace Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures
{ {
public abstract class ServerFixture : IDisposable public abstract class ServerFixture : IDisposable
{ {
public bool IsStarted => RootUri != null; public Uri RootUri => _rootUriInitializer.Value;
public Uri RootUri { get; private set; }
private IWebHost _host; private readonly Lazy<Uri> _rootUriInitializer;
public void Dispose() public ServerFixture()
{ {
_host.StopAsync(); _rootUriInitializer = new Lazy<Uri>(() =>
new Uri(StartAndGetRootUri()));
} }
protected void Start(IWebHost host) public abstract void Dispose();
{
if (_host != null)
{
throw new InvalidOperationException("Server is already started.");
}
_host = host ?? throw new ArgumentNullException(nameof(host)); protected abstract string StartAndGetRootUri();
RootUri = new Uri(StartWebHostInBackgroundThread());
}
protected static string FindSolutionDir() protected static string FindSolutionDir()
{ {
@ -62,21 +52,17 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure
} }
} }
private string StartWebHostInBackgroundThread() protected static void RunInBackgroundThread(Action action)
{ {
var serverStarted = new ManualResetEvent(false); var isDone = new ManualResetEvent(false);
new Thread(() => new Thread(() =>
{ {
_host.Start(); action();
serverStarted.Set(); isDone.Set();
}).Start(); }).Start();
serverStarted.WaitOne(); isDone.WaitOne();
return _host.ServerFeatures
.Get<IServerAddressesFeature>()
.Addresses.Single();
} }
} }
} }

View File

@ -6,29 +6,34 @@ using System.IO;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
namespace Microsoft.Blazor.E2ETest.Infrastructure namespace Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures
{ {
public class StaticServerFixture : ServerFixture public class StaticSiteServerFixture : WebHostServerFixture
{ {
public void Start(string sampleSiteName) public string SampleSiteName { get; set; }
{
var sampleSitePath = Path.Combine(
FindSolutionDir(),
"samples",
sampleSiteName);
var host = new WebHostBuilder() protected override IWebHost CreateWebHost()
{
if (string.IsNullOrEmpty(SampleSiteName))
{
throw new InvalidOperationException($"No value was provided for {nameof(SampleSiteName)}");
}
var sampleSitePath = Path.Combine(
FindSolutionDir(),
"samples",
SampleSiteName);
return new WebHostBuilder()
.UseKestrel() .UseKestrel()
.UseContentRoot(sampleSitePath) .UseContentRoot(sampleSitePath)
.UseWebRoot(string.Empty) .UseWebRoot(string.Empty)
.UseStartup<Startup>() .UseStartup<StaticSiteStartup>()
.UseUrls("http://127.0.0.1:0") .UseUrls("http://127.0.0.1:0")
.Build(); .Build();
Start(host);
} }
private class Startup private class StaticSiteStartup
{ {
public void Configure(IApplicationBuilder app) public void Configure(IApplicationBuilder app)
{ {

View File

@ -0,0 +1,30 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using System.Linq;
namespace Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures
{
public abstract class WebHostServerFixture : ServerFixture
{
private IWebHost _host;
protected override string StartAndGetRootUri()
{
_host = CreateWebHost();
RunInBackgroundThread(_host.Start);
return _host.ServerFeatures
.Get<IServerAddressesFeature>()
.Addresses.Single();
}
public override void Dispose()
{
_host.StopAsync();
}
protected abstract IWebHost CreateWebHost();
}
}

View File

@ -1,36 +1,27 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using OpenQA.Selenium; using Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures;
using System; using System;
using Xunit; using Xunit;
namespace Microsoft.Blazor.E2ETest.Infrastructure namespace Microsoft.Blazor.E2ETest.Infrastructure
{ {
public class AspNetSiteTestBase<TStartup> public abstract class ServerTestBase<TServerFixture>
: IClassFixture<BrowserFixture>, IClassFixture<AspNetServerFixture> : BrowserTestBase, IClassFixture<TServerFixture>
where TServerFixture: ServerFixture
{ {
public IWebDriver Browser { get; } private readonly TServerFixture _serverFixture;
private Uri _serverRootUri; public ServerTestBase(BrowserFixture browserFixture, TServerFixture serverFixture)
: base(browserFixture)
public AspNetSiteTestBase(
BrowserFixture browserFixture,
AspNetServerFixture serverFixture)
{ {
Browser = browserFixture.Browser; _serverFixture = serverFixture;
if (!serverFixture.IsStarted)
{
serverFixture.Start(typeof(TStartup));
}
_serverRootUri = serverFixture.RootUri;
} }
public void Navigate(string relativeUrl, bool noReload = false) public void Navigate(string relativeUrl, bool noReload = false)
{ {
var absoluteUrl = new Uri(_serverRootUri, relativeUrl); var absoluteUrl = new Uri(_serverFixture.RootUri, relativeUrl);
if (noReload) if (noReload)
{ {

View File

@ -1,39 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using OpenQA.Selenium;
using System;
using System.IO;
using Xunit;
namespace Microsoft.Blazor.E2ETest.Infrastructure
{
public class StaticSiteTestBase
: IClassFixture<BrowserFixture>, IClassFixture<StaticServerFixture>
{
public IWebDriver Browser { get; }
private Uri _serverRootUri;
public StaticSiteTestBase(
BrowserFixture browserFixture,
StaticServerFixture serverFixture,
string sampleSiteName)
{
Browser = browserFixture.Browser;
if (!serverFixture.IsStarted)
{
serverFixture.Start(sampleSiteName);
}
_serverRootUri = serverFixture.RootUri;
}
public void Navigate(string relativeUrl)
{
var absoluteUrl = new Uri(_serverRootUri, relativeUrl);
Browser.Navigate().GoToUrl(absoluteUrl);
}
}
}

View File

@ -2,16 +2,18 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Blazor.E2ETest.Infrastructure; using Microsoft.Blazor.E2ETest.Infrastructure;
using Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures;
using OpenQA.Selenium; using OpenQA.Selenium;
using Xunit; using Xunit;
namespace Microsoft.Blazor.E2ETest.Tests namespace Microsoft.Blazor.E2ETest.Tests
{ {
public class HelloWorldTest : StaticSiteTestBase public class HelloWorldTest : ServerTestBase<StaticSiteServerFixture>
{ {
public HelloWorldTest(BrowserFixture browserFixture, StaticServerFixture serverFixture) public HelloWorldTest(BrowserFixture browserFixture, StaticSiteServerFixture serverFixture)
: base(browserFixture, serverFixture, "HelloWorld") : base(browserFixture, serverFixture)
{ {
serverFixture.SampleSiteName = "HelloWorld";
} }
[Fact] [Fact]

View File

@ -2,14 +2,15 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Blazor.E2ETest.Infrastructure; using Microsoft.Blazor.E2ETest.Infrastructure;
using Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures;
using OpenQA.Selenium; using OpenQA.Selenium;
using Xunit; using Xunit;
namespace Microsoft.Blazor.E2ETest.Tests namespace Microsoft.Blazor.E2ETest.Tests
{ {
public class MonoSanityTest : AspNetSiteTestBase<MonoSanity.Startup> public class MonoSanityTest : ServerTestBase<AspNetSiteServerFixture<MonoSanity.Startup>>
{ {
public MonoSanityTest(BrowserFixture browserFixture, AspNetServerFixture serverFixture) public MonoSanityTest(BrowserFixture browserFixture, AspNetSiteServerFixture<MonoSanity.Startup> serverFixture)
: base(browserFixture, serverFixture) : base(browserFixture, serverFixture)
{ {
} }