diff --git a/samples/MonoSanity/wwwroot/index.html b/samples/MonoSanity/wwwroot/index.html index 846b0655ec..b0ba50cf6c 100644 --- a/samples/MonoSanity/wwwroot/index.html +++ b/samples/MonoSanity/wwwroot/index.html @@ -12,7 +12,7 @@
@@ -53,7 +53,7 @@ var a = parseInt(el('addNumberA').value); var b = parseInt(el('addNumberB').value); var result = invokeMonoMethod('MonoSanityClient', 'MonoSanityClient', 'Examples', 'AddNumbers', [a, b]); - el('addNumberResult').value = result; + el('addNumbersResult').value = result; } el('repeatString').onsubmit = function (evt) { diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/AspNetServerFixture.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/AspNetServerFixture.cs index bed8a7e6ac..b5f89875de 100644 --- a/test/Microsoft.Blazor.E2ETest/Infrastructure/AspNetServerFixture.cs +++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/AspNetServerFixture.cs @@ -10,7 +10,7 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure { public class AspNetServerFixture : ServerFixture { - public string StartAndGetUrl(Type startupType) + public void Start(Type startupType) { var sampleSitePath = Path.Combine( FindSolutionDir(), @@ -23,7 +23,7 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure .UseUrls("http://127.0.0.1:0") .Build(); - return StartAndGetUrl(host); + Start(host); } } } \ No newline at end of file diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/AspNetSiteTestBase.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/AspNetSiteTestBase.cs index 7251b4b913..8295968266 100644 --- a/test/Microsoft.Blazor.E2ETest/Infrastructure/AspNetSiteTestBase.cs +++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/AspNetSiteTestBase.cs @@ -20,13 +20,27 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure { Browser = browserFixture.Browser; - var serverRootUriString = serverFixture.StartAndGetUrl(typeof(TStartup)); - _serverRootUri = new Uri(serverRootUriString); + if (!serverFixture.IsStarted) + { + serverFixture.Start(typeof(TStartup)); + } + + _serverRootUri = serverFixture.RootUri; } - public void Navigate(string relativeUrl) + public void Navigate(string relativeUrl, bool noReload = false) { var absoluteUrl = new Uri(_serverRootUri, relativeUrl); + + if (noReload) + { + var existingUrl = Browser.Url; + if (string.Equals(existingUrl, absoluteUrl.AbsoluteUri, StringComparison.Ordinal)) + { + return; + } + } + Browser.Navigate().GoToUrl(absoluteUrl); } } diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixture.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixture.cs index a51535ce19..ea1f567bc1 100644 --- a/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixture.cs +++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixture.cs @@ -12,6 +12,9 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure { public abstract class ServerFixture : IDisposable { + public bool IsStarted => RootUri != null; + public Uri RootUri { get; private set; } + private IWebHost _host; public void Dispose() @@ -19,10 +22,15 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure _host.StopAsync(); } - protected string StartAndGetUrl(IWebHost host) + protected void Start(IWebHost host) { - _host = host; - return StartWebHostInBackgroundThread(); + if (_host != null) + { + throw new InvalidOperationException("Server is already started."); + } + + _host = host ?? throw new ArgumentNullException(nameof(host)); + RootUri = new Uri(StartWebHostInBackgroundThread()); } protected static string FindSolutionDir() diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/StaticServerFixture.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/StaticServerFixture.cs index 3c5a925576..d190faa431 100644 --- a/test/Microsoft.Blazor.E2ETest/Infrastructure/StaticServerFixture.cs +++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/StaticServerFixture.cs @@ -1,6 +1,7 @@ // 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; using System.IO; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -9,7 +10,7 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure { public class StaticServerFixture : ServerFixture { - public string StartAndGetUrl(string sampleSiteName) + public void Start(string sampleSiteName) { var sampleSitePath = Path.Combine( FindSolutionDir(), @@ -24,7 +25,7 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure .UseUrls("http://127.0.0.1:0") .Build(); - return StartAndGetUrl(host); + Start(host); } private class Startup diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/StaticSiteTestBase.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/StaticSiteTestBase.cs index f006556cac..8b5bdbbd54 100644 --- a/test/Microsoft.Blazor.E2ETest/Infrastructure/StaticSiteTestBase.cs +++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/StaticSiteTestBase.cs @@ -22,9 +22,12 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure { Browser = browserFixture.Browser; - // Start a static files web server for the specified directory - var serverRootUriString = serverFixture.StartAndGetUrl(sampleSiteName); - _serverRootUri = new Uri(serverRootUriString); + if (!serverFixture.IsStarted) + { + serverFixture.Start(sampleSiteName); + } + + _serverRootUri = serverFixture.RootUri; } public void Navigate(string relativeUrl) diff --git a/test/Microsoft.Blazor.E2ETest/Tests/MonoSanityTest.cs b/test/Microsoft.Blazor.E2ETest/Tests/MonoSanityTest.cs index 11d7cbd3e1..90c7c7121e 100644 --- a/test/Microsoft.Blazor.E2ETest/Tests/MonoSanityTest.cs +++ b/test/Microsoft.Blazor.E2ETest/Tests/MonoSanityTest.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.Blazor.E2ETest.Infrastructure; +using OpenQA.Selenium; using Xunit; namespace Microsoft.Blazor.E2ETest.Tests @@ -16,8 +17,45 @@ namespace Microsoft.Blazor.E2ETest.Tests [Fact] public void HasTitle() { - Navigate("/"); + Navigate("/", noReload: true); Assert.Equal("Mono sanity check", Browser.Title); } + + [Fact] + public void CanAddNumbers() + { + Navigate("/", noReload: true); + + SetValue(Browser, "addNumberA", "1001"); + SetValue(Browser, "addNumberB", "2002"); + Browser.FindElement(By.CssSelector("#addNumbers button")).Click(); + + Assert.Equal("3003", GetValue(Browser, "addNumbersResult")); + } + + [Fact] + public void CanRepeatString() + { + Navigate("/", noReload: true); + + SetValue(Browser, "repeatStringStr", "Test"); + SetValue(Browser, "repeatStringCount", "5"); + Browser.FindElement(By.CssSelector("#repeatString button")).Click(); + + Assert.Equal("TestTestTestTestTest", GetValue(Browser, "repeatStringResult")); + } + + private static string GetValue(IWebDriver webDriver, string elementId) + { + var element = webDriver.FindElement(By.Id(elementId)); + return element.GetAttribute("value"); + } + + private static void SetValue(IWebDriver webDriver, string elementId, string value) + { + var element = webDriver.FindElement(By.Id(elementId)); + element.Clear(); + element.SendKeys(value); + } } }