Refactor E2E tests fixtures/base classes for better composability
This commit is contained in:
parent
aedb146ea4
commit
a59bfa3cb9
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,29 +1,27 @@
|
|||
// 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 System.IO;
|
||||
using Microsoft.AspNetCore;
|
||||
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(
|
||||
FindSolutionDir(),
|
||||
"samples",
|
||||
startupType.Assembly.GetName().Name);
|
||||
FindSolutionDir(),
|
||||
"samples",
|
||||
typeof(TStartup).Assembly.GetName().Name);
|
||||
|
||||
var host = WebHost.CreateDefaultBuilder()
|
||||
.UseStartup(startupType)
|
||||
return WebHost.CreateDefaultBuilder()
|
||||
.UseStartup<TStartup>()
|
||||
.UseContentRoot(sampleSitePath)
|
||||
.UseUrls("http://127.0.0.1:0")
|
||||
.Build();
|
||||
|
||||
Start(host);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,37 +1,27 @@
|
|||
// 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;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
namespace Microsoft.Blazor.E2ETest.Infrastructure
|
||||
namespace Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures
|
||||
{
|
||||
public abstract class ServerFixture : IDisposable
|
||||
{
|
||||
public bool IsStarted => RootUri != null;
|
||||
public Uri RootUri { get; private set; }
|
||||
public Uri RootUri => _rootUriInitializer.Value;
|
||||
|
||||
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)
|
||||
{
|
||||
if (_host != null)
|
||||
{
|
||||
throw new InvalidOperationException("Server is already started.");
|
||||
}
|
||||
public abstract void Dispose();
|
||||
|
||||
_host = host ?? throw new ArgumentNullException(nameof(host));
|
||||
RootUri = new Uri(StartWebHostInBackgroundThread());
|
||||
}
|
||||
protected abstract string StartAndGetRootUri();
|
||||
|
||||
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(() =>
|
||||
{
|
||||
_host.Start();
|
||||
serverStarted.Set();
|
||||
action();
|
||||
isDone.Set();
|
||||
}).Start();
|
||||
|
||||
serverStarted.WaitOne();
|
||||
|
||||
return _host.ServerFeatures
|
||||
.Get<IServerAddressesFeature>()
|
||||
.Addresses.Single();
|
||||
isDone.WaitOne();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,29 +6,34 @@ using System.IO;
|
|||
using Microsoft.AspNetCore.Builder;
|
||||
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)
|
||||
{
|
||||
var sampleSitePath = Path.Combine(
|
||||
FindSolutionDir(),
|
||||
"samples",
|
||||
sampleSiteName);
|
||||
public string SampleSiteName { get; set; }
|
||||
|
||||
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()
|
||||
.UseContentRoot(sampleSitePath)
|
||||
.UseWebRoot(string.Empty)
|
||||
.UseStartup<Startup>()
|
||||
.UseStartup<StaticSiteStartup>()
|
||||
.UseUrls("http://127.0.0.1:0")
|
||||
.Build();
|
||||
|
||||
Start(host);
|
||||
}
|
||||
|
||||
private class Startup
|
||||
private class StaticSiteStartup
|
||||
{
|
||||
public void Configure(IApplicationBuilder app)
|
||||
{
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,36 +1,27 @@
|
|||
// 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 Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures;
|
||||
using System;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Blazor.E2ETest.Infrastructure
|
||||
{
|
||||
public class AspNetSiteTestBase<TStartup>
|
||||
: IClassFixture<BrowserFixture>, IClassFixture<AspNetServerFixture>
|
||||
public abstract class ServerTestBase<TServerFixture>
|
||||
: BrowserTestBase, IClassFixture<TServerFixture>
|
||||
where TServerFixture: ServerFixture
|
||||
{
|
||||
public IWebDriver Browser { get; }
|
||||
private readonly TServerFixture _serverFixture;
|
||||
|
||||
private Uri _serverRootUri;
|
||||
|
||||
public AspNetSiteTestBase(
|
||||
BrowserFixture browserFixture,
|
||||
AspNetServerFixture serverFixture)
|
||||
public ServerTestBase(BrowserFixture browserFixture, TServerFixture serverFixture)
|
||||
: base(browserFixture)
|
||||
{
|
||||
Browser = browserFixture.Browser;
|
||||
|
||||
if (!serverFixture.IsStarted)
|
||||
{
|
||||
serverFixture.Start(typeof(TStartup));
|
||||
}
|
||||
|
||||
_serverRootUri = serverFixture.RootUri;
|
||||
_serverFixture = serverFixture;
|
||||
}
|
||||
|
||||
public void Navigate(string relativeUrl, bool noReload = false)
|
||||
{
|
||||
var absoluteUrl = new Uri(_serverRootUri, relativeUrl);
|
||||
var absoluteUrl = new Uri(_serverFixture.RootUri, relativeUrl);
|
||||
|
||||
if (noReload)
|
||||
{
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,16 +2,18 @@
|
|||
// 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.ServerFixtures;
|
||||
using OpenQA.Selenium;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Blazor.E2ETest.Tests
|
||||
{
|
||||
public class HelloWorldTest : StaticSiteTestBase
|
||||
public class HelloWorldTest : ServerTestBase<StaticSiteServerFixture>
|
||||
{
|
||||
public HelloWorldTest(BrowserFixture browserFixture, StaticServerFixture serverFixture)
|
||||
: base(browserFixture, serverFixture, "HelloWorld")
|
||||
public HelloWorldTest(BrowserFixture browserFixture, StaticSiteServerFixture serverFixture)
|
||||
: base(browserFixture, serverFixture)
|
||||
{
|
||||
serverFixture.SampleSiteName = "HelloWorld";
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
|
|
@ -2,14 +2,15 @@
|
|||
// 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.ServerFixtures;
|
||||
using OpenQA.Selenium;
|
||||
using Xunit;
|
||||
|
||||
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)
|
||||
{
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue