From a59bfa3cb9fe02951586d09e9de2c778587c5778 Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Thu, 7 Dec 2017 10:39:46 +0000 Subject: [PATCH] Refactor E2E tests fixtures/base classes for better composability --- .../Infrastructure/BrowserTestBase.cs | 18 +++++++++ .../AspNetSiteServerFixture.cs} | 24 +++++------ .../{ => ServerFixtures}/ServerFixture.cs | 40 ++++++------------- .../StaticSiteServerFixture.cs} | 31 ++++++++------ .../ServerFixtures/WebHostServerFixture.cs | 30 ++++++++++++++ ...spNetSiteTestBase.cs => ServerTestBase.cs} | 27 +++++-------- .../Infrastructure/StaticSiteTestBase.cs | 39 ------------------ .../Tests/HelloWorldTest.cs | 8 ++-- .../Tests/MonoSanityTest.cs | 5 ++- 9 files changed, 107 insertions(+), 115 deletions(-) create mode 100644 test/Microsoft.Blazor.E2ETest/Infrastructure/BrowserTestBase.cs rename test/Microsoft.Blazor.E2ETest/Infrastructure/{AspNetServerFixture.cs => ServerFixtures/AspNetSiteServerFixture.cs} (50%) rename test/Microsoft.Blazor.E2ETest/Infrastructure/{ => ServerFixtures}/ServerFixture.cs (56%) rename test/Microsoft.Blazor.E2ETest/Infrastructure/{StaticServerFixture.cs => ServerFixtures/StaticSiteServerFixture.cs} (51%) create mode 100644 test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/WebHostServerFixture.cs rename test/Microsoft.Blazor.E2ETest/Infrastructure/{AspNetSiteTestBase.cs => ServerTestBase.cs} (52%) delete mode 100644 test/Microsoft.Blazor.E2ETest/Infrastructure/StaticSiteTestBase.cs diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/BrowserTestBase.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/BrowserTestBase.cs new file mode 100644 index 0000000000..34dbec336a --- /dev/null +++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/BrowserTestBase.cs @@ -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 + { + public IWebDriver Browser { get; } + + public BrowserTestBase(BrowserFixture browserFixture) + { + Browser = browserFixture.Browser; + } + } +} diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/AspNetServerFixture.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/AspNetSiteServerFixture.cs similarity index 50% rename from test/Microsoft.Blazor.E2ETest/Infrastructure/AspNetServerFixture.cs rename to test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/AspNetSiteServerFixture.cs index b5f89875de..ae9bcd73b6 100644 --- a/test/Microsoft.Blazor.E2ETest/Infrastructure/AspNetServerFixture.cs +++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/AspNetSiteServerFixture.cs @@ -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 : 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() .UseContentRoot(sampleSitePath) .UseUrls("http://127.0.0.1:0") .Build(); - - Start(host); } } -} \ No newline at end of file +} diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixture.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/ServerFixture.cs similarity index 56% rename from test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixture.cs rename to test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/ServerFixture.cs index ea1f567bc1..5f8392ef46 100644 --- a/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixture.cs +++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/ServerFixture.cs @@ -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 _rootUriInitializer; - public void Dispose() + public ServerFixture() { - _host.StopAsync(); + _rootUriInitializer = new Lazy(() => + 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() - .Addresses.Single(); + isDone.WaitOne(); } } } diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/StaticServerFixture.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/StaticSiteServerFixture.cs similarity index 51% rename from test/Microsoft.Blazor.E2ETest/Infrastructure/StaticServerFixture.cs rename to test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/StaticSiteServerFixture.cs index d190faa431..321c8159c5 100644 --- a/test/Microsoft.Blazor.E2ETest/Infrastructure/StaticServerFixture.cs +++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/StaticSiteServerFixture.cs @@ -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() + .UseStartup() .UseUrls("http://127.0.0.1:0") .Build(); - - Start(host); } - private class Startup + private class StaticSiteStartup { public void Configure(IApplicationBuilder app) { diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/WebHostServerFixture.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/WebHostServerFixture.cs new file mode 100644 index 0000000000..515108f6d3 --- /dev/null +++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/WebHostServerFixture.cs @@ -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() + .Addresses.Single(); + } + + public override void Dispose() + { + _host.StopAsync(); + } + + protected abstract IWebHost CreateWebHost(); + } +} diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/AspNetSiteTestBase.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerTestBase.cs similarity index 52% rename from test/Microsoft.Blazor.E2ETest/Infrastructure/AspNetSiteTestBase.cs rename to test/Microsoft.Blazor.E2ETest/Infrastructure/ServerTestBase.cs index 8295968266..6ee56bc9c6 100644 --- a/test/Microsoft.Blazor.E2ETest/Infrastructure/AspNetSiteTestBase.cs +++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerTestBase.cs @@ -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 - : IClassFixture, IClassFixture + public abstract class ServerTestBase + : BrowserTestBase, IClassFixture + 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) { diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/StaticSiteTestBase.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/StaticSiteTestBase.cs deleted file mode 100644 index 8b5bdbbd54..0000000000 --- a/test/Microsoft.Blazor.E2ETest/Infrastructure/StaticSiteTestBase.cs +++ /dev/null @@ -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, IClassFixture - { - 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); - } - } -} diff --git a/test/Microsoft.Blazor.E2ETest/Tests/HelloWorldTest.cs b/test/Microsoft.Blazor.E2ETest/Tests/HelloWorldTest.cs index 422cbbf57e..26ff3d36c2 100644 --- a/test/Microsoft.Blazor.E2ETest/Tests/HelloWorldTest.cs +++ b/test/Microsoft.Blazor.E2ETest/Tests/HelloWorldTest.cs @@ -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 { - public HelloWorldTest(BrowserFixture browserFixture, StaticServerFixture serverFixture) - : base(browserFixture, serverFixture, "HelloWorld") + public HelloWorldTest(BrowserFixture browserFixture, StaticSiteServerFixture serverFixture) + : base(browserFixture, serverFixture) { + serverFixture.SampleSiteName = "HelloWorld"; } [Fact] diff --git a/test/Microsoft.Blazor.E2ETest/Tests/MonoSanityTest.cs b/test/Microsoft.Blazor.E2ETest/Tests/MonoSanityTest.cs index 7b52c973fa..f2f53c0418 100644 --- a/test/Microsoft.Blazor.E2ETest/Tests/MonoSanityTest.cs +++ b/test/Microsoft.Blazor.E2ETest/Tests/MonoSanityTest.cs @@ -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 + public class MonoSanityTest : ServerTestBase> { - public MonoSanityTest(BrowserFixture browserFixture, AspNetServerFixture serverFixture) + public MonoSanityTest(BrowserFixture browserFixture, AspNetSiteServerFixture serverFixture) : base(browserFixture, serverFixture) { }