Add E2E tests for MonoSanity sample

This commit is contained in:
Steve Sanderson 2017-12-06 15:57:06 +00:00
parent 32dae87b00
commit 5aaed3d75a
7 changed files with 80 additions and 16 deletions

View File

@ -12,7 +12,7 @@
<form id="addNumbers"> <form id="addNumbers">
<input id="addNumberA" value="123" /> + <input id="addNumberA" value="123" /> +
<input id="addNumberB" value="456" /> = <input id="addNumberB" value="456" /> =
<input id="addNumberResult" readonly /> <input id="addNumbersResult" readonly />
<button type="submit" disabled>Go</button> <button type="submit" disabled>Go</button>
</form> </form>
</fieldset> </fieldset>
@ -53,7 +53,7 @@
var a = parseInt(el('addNumberA').value); var a = parseInt(el('addNumberA').value);
var b = parseInt(el('addNumberB').value); var b = parseInt(el('addNumberB').value);
var result = invokeMonoMethod('MonoSanityClient', 'MonoSanityClient', 'Examples', 'AddNumbers', [a, b]); var result = invokeMonoMethod('MonoSanityClient', 'MonoSanityClient', 'Examples', 'AddNumbers', [a, b]);
el('addNumberResult').value = result; el('addNumbersResult').value = result;
} }
el('repeatString').onsubmit = function (evt) { el('repeatString').onsubmit = function (evt) {

View File

@ -10,7 +10,7 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure
{ {
public class AspNetServerFixture : ServerFixture public class AspNetServerFixture : ServerFixture
{ {
public string StartAndGetUrl(Type startupType) public void Start(Type startupType)
{ {
var sampleSitePath = Path.Combine( var sampleSitePath = Path.Combine(
FindSolutionDir(), FindSolutionDir(),
@ -23,7 +23,7 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure
.UseUrls("http://127.0.0.1:0") .UseUrls("http://127.0.0.1:0")
.Build(); .Build();
return StartAndGetUrl(host); Start(host);
} }
} }
} }

View File

@ -20,13 +20,27 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure
{ {
Browser = browserFixture.Browser; Browser = browserFixture.Browser;
var serverRootUriString = serverFixture.StartAndGetUrl(typeof(TStartup)); if (!serverFixture.IsStarted)
_serverRootUri = new Uri(serverRootUriString); {
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); 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); Browser.Navigate().GoToUrl(absoluteUrl);
} }
} }

View File

@ -12,6 +12,9 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure
{ {
public abstract class ServerFixture : IDisposable public abstract class ServerFixture : IDisposable
{ {
public bool IsStarted => RootUri != null;
public Uri RootUri { get; private set; }
private IWebHost _host; private IWebHost _host;
public void Dispose() public void Dispose()
@ -19,10 +22,15 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure
_host.StopAsync(); _host.StopAsync();
} }
protected string StartAndGetUrl(IWebHost host) protected void Start(IWebHost host)
{ {
_host = host; if (_host != null)
return StartWebHostInBackgroundThread(); {
throw new InvalidOperationException("Server is already started.");
}
_host = host ?? throw new ArgumentNullException(nameof(host));
RootUri = new Uri(StartWebHostInBackgroundThread());
} }
protected static string FindSolutionDir() protected static string FindSolutionDir()

View File

@ -1,6 +1,7 @@
// 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;
using System.IO; using System.IO;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
@ -9,7 +10,7 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure
{ {
public class StaticServerFixture : ServerFixture public class StaticServerFixture : ServerFixture
{ {
public string StartAndGetUrl(string sampleSiteName) public void Start(string sampleSiteName)
{ {
var sampleSitePath = Path.Combine( var sampleSitePath = Path.Combine(
FindSolutionDir(), FindSolutionDir(),
@ -24,7 +25,7 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure
.UseUrls("http://127.0.0.1:0") .UseUrls("http://127.0.0.1:0")
.Build(); .Build();
return StartAndGetUrl(host); Start(host);
} }
private class Startup private class Startup

View File

@ -22,9 +22,12 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure
{ {
Browser = browserFixture.Browser; Browser = browserFixture.Browser;
// Start a static files web server for the specified directory if (!serverFixture.IsStarted)
var serverRootUriString = serverFixture.StartAndGetUrl(sampleSiteName); {
_serverRootUri = new Uri(serverRootUriString); serverFixture.Start(sampleSiteName);
}
_serverRootUri = serverFixture.RootUri;
} }
public void Navigate(string relativeUrl) public void Navigate(string relativeUrl)

View File

@ -2,6 +2,7 @@
// 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 OpenQA.Selenium;
using Xunit; using Xunit;
namespace Microsoft.Blazor.E2ETest.Tests namespace Microsoft.Blazor.E2ETest.Tests
@ -16,8 +17,45 @@ namespace Microsoft.Blazor.E2ETest.Tests
[Fact] [Fact]
public void HasTitle() public void HasTitle()
{ {
Navigate("/"); Navigate("/", noReload: true);
Assert.Equal("Mono sanity check", Browser.Title); 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);
}
} }
} }