From dfd6c4a1c2240fc3df1c1b0a97447dc4a81796f6 Mon Sep 17 00:00:00 2001
From: Steve Sanderson
Date: Thu, 4 Jan 2018 17:35:58 +0000
Subject: [PATCH] Begin E2E testing for BasicTestApp and component rendering in
browser
---
src/Microsoft.Blazor.Browser.JS/src/Boot.ts | 1 +
.../src/GlobalExports.ts | 13 +++++
.../src/RegisteredFunction.ts | 10 +++-
.../Core/FileSystem/IndexHtmlFileProvider.cs | 17 +++++-
.../IndexHtmlFileProviderTest.cs | 4 +-
.../ServerFixtures/AspNetSiteServerFixture.cs | 4 +-
.../ServerFixtures/DevHostServerFixture.cs | 4 +-
.../ServerFixtures/ServerFixture.cs | 14 +++++
.../ServerFixtures/StaticSiteServerFixture.cs | 5 +-
.../Microsoft.Blazor.E2ETest.csproj | 1 +
.../Tests/ComponentRenderingTest.cs | 55 +++++++++++++++++++
test/testapps/BasicTestApp/Program.cs | 15 ++++-
.../BasicTestApp/TextOnlyComponent.cs | 16 ++++++
test/testapps/BasicTestApp/wwwroot/index.html | 12 ++++
14 files changed, 151 insertions(+), 20 deletions(-)
create mode 100644 src/Microsoft.Blazor.Browser.JS/src/GlobalExports.ts
create mode 100644 test/Microsoft.Blazor.E2ETest/Tests/ComponentRenderingTest.cs
create mode 100644 test/testapps/BasicTestApp/TextOnlyComponent.cs
diff --git a/src/Microsoft.Blazor.Browser.JS/src/Boot.ts b/src/Microsoft.Blazor.Browser.JS/src/Boot.ts
index 4b11f6c205..dcbe130563 100644
--- a/src/Microsoft.Blazor.Browser.JS/src/Boot.ts
+++ b/src/Microsoft.Blazor.Browser.JS/src/Boot.ts
@@ -1,6 +1,7 @@
import { platform } from './Environment';
import { getAssemblyNameFromUrl } from './Platform/DotNet';
import './Rendering/Renderer';
+import './GlobalExports';
async function boot() {
// Read startup config from the )
+ // and then let the developer manually place the tag that loads blazor.js
+ // wherever they want (adding their own async/defer if they want).
return htmlTemplate
- .Replace("
", "\n" + CreateBootMarkup(assemblyName, binFiles));
}
private static string CreateBootMarkup(string assemblyName, IEnumerable binFiles)
diff --git a/test/Microsoft.Blazor.Build.Test/IndexHtmlFileProviderTest.cs b/test/Microsoft.Blazor.Build.Test/IndexHtmlFileProviderTest.cs
index dd22d0e11a..d666e5462a 100644
--- a/test/Microsoft.Blazor.Build.Test/IndexHtmlFileProviderTest.cs
+++ b/test/Microsoft.Blazor.Build.Test/IndexHtmlFileProviderTest.cs
@@ -68,7 +68,7 @@ namespace Microsoft.Blazor.Server.Test
public void InsertsScriptTagReferencingAssemblyAndDependencies()
{
// Arrange
- var htmlTemplate = "Hello";
+ var htmlTemplate = "Hello
Some text";
var dependencies = new IFileInfo[]
{
new TestFileInfo("System.Abc.dll"),
@@ -80,7 +80,7 @@ namespace Microsoft.Blazor.Server.Test
// Act
var file = instance.GetFileInfo("/index.html");
var parsedHtml = new HtmlParser().Parse(ReadString(file));
- var scriptElem = parsedHtml.Body.LastElementChild;
+ var scriptElem = parsedHtml.Body.FirstElementChild;
// Assert
Assert.Equal("script", scriptElem.TagName.ToLowerInvariant());
diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/AspNetSiteServerFixture.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/AspNetSiteServerFixture.cs
index 1671468ce2..9d1ed52ae8 100644
--- a/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/AspNetSiteServerFixture.cs
+++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/AspNetSiteServerFixture.cs
@@ -23,9 +23,7 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures
$"No value was provided for {nameof(BuildWebHostMethod)}");
}
- var sampleSitePath = Path.Combine(
- FindSolutionDir(),
- "samples",
+ var sampleSitePath = FindSampleOrTestSitePath(
BuildWebHostMethod.Method.DeclaringType.Assembly.GetName().Name);
return BuildWebHostMethod(new[]
diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/DevHostServerFixture.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/DevHostServerFixture.cs
index 928b12c0ee..0c6b3344a2 100644
--- a/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/DevHostServerFixture.cs
+++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/DevHostServerFixture.cs
@@ -11,9 +11,7 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures
{
protected override IWebHost CreateWebHost()
{
- var sampleSitePath = Path.Combine(
- FindSolutionDir(),
- "samples",
+ var sampleSitePath = FindSampleOrTestSitePath(
typeof(TProgram).Assembly.GetName().Name);
return DevHostServerProgram.BuildWebHost(new string[]
diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/ServerFixture.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/ServerFixture.cs
index 5f8392ef46..b94d0ce0c2 100644
--- a/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/ServerFixture.cs
+++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/ServerFixture.cs
@@ -3,6 +3,7 @@
using System;
using System.IO;
+using System.Linq;
using System.Threading;
namespace Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures
@@ -30,6 +31,19 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures
Path.GetDirectoryName(typeof(ServerFixture).Assembly.Location));
}
+ protected static string FindSampleOrTestSitePath(string projectName)
+ {
+ var solutionDir = FindSolutionDir();
+ var possibleLocations = new[]
+ {
+ Path.Combine(solutionDir, "samples", projectName),
+ Path.Combine(solutionDir, "test", "testapps", projectName)
+ };
+
+ return possibleLocations.FirstOrDefault(Directory.Exists)
+ ?? throw new ArgumentException($"Cannot find a sample or test site with name '{projectName}'.");
+ }
+
private static string FindClosestDirectoryContaining(
string filename,
string startDirectory)
diff --git a/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/StaticSiteServerFixture.cs b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/StaticSiteServerFixture.cs
index 18d3ac4b68..92a1e841a2 100644
--- a/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/StaticSiteServerFixture.cs
+++ b/test/Microsoft.Blazor.E2ETest/Infrastructure/ServerFixtures/StaticSiteServerFixture.cs
@@ -23,10 +23,7 @@ namespace Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures
throw new InvalidOperationException($"No value was provided for {nameof(SampleSiteName)}");
}
- var sampleSitePath = Path.Combine(
- FindSolutionDir(),
- "samples",
- SampleSiteName);
+ var sampleSitePath = FindSampleOrTestSitePath(SampleSiteName);
return new WebHostBuilder()
.UseKestrel()
diff --git a/test/Microsoft.Blazor.E2ETest/Microsoft.Blazor.E2ETest.csproj b/test/Microsoft.Blazor.E2ETest/Microsoft.Blazor.E2ETest.csproj
index 784b08ee34..ab42131656 100644
--- a/test/Microsoft.Blazor.E2ETest/Microsoft.Blazor.E2ETest.csproj
+++ b/test/Microsoft.Blazor.E2ETest/Microsoft.Blazor.E2ETest.csproj
@@ -22,6 +22,7 @@
+
diff --git a/test/Microsoft.Blazor.E2ETest/Tests/ComponentRenderingTest.cs b/test/Microsoft.Blazor.E2ETest/Tests/ComponentRenderingTest.cs
new file mode 100644
index 0000000000..e5db024972
--- /dev/null
+++ b/test/Microsoft.Blazor.E2ETest/Tests/ComponentRenderingTest.cs
@@ -0,0 +1,55 @@
+// 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 BasicTestApp;
+using Microsoft.Blazor.E2ETest.Infrastructure;
+using Microsoft.Blazor.E2ETest.Infrastructure.ServerFixtures;
+using OpenQA.Selenium;
+using OpenQA.Selenium.Support.UI;
+using Xunit;
+
+namespace Microsoft.Blazor.E2ETest.Tests
+{
+ public class ComponentRenderingTest
+ : ServerTestBase>
+ {
+ public ComponentRenderingTest(BrowserFixture browserFixture, DevHostServerFixture serverFixture)
+ : base(browserFixture, serverFixture)
+ {
+ }
+
+ [Fact]
+ public void BasicTestAppCanBeServed()
+ {
+ Navigate("/", noReload: true);
+ Assert.Equal("Basic test app", Browser.Title);
+ }
+
+ [Fact]
+ public void CanRenderTextOnlyComponent()
+ {
+ Navigate("/", noReload: true);
+ MountTestComponent("BasicTestApp.TextOnlyComponent");
+
+ var appElement = Browser.FindElement(By.TagName("app"));
+ Assert.Equal("Hello from TextOnlyComponent", appElement.Text);
+ }
+
+ private void MountTestComponent(string componentTypeName)
+ {
+ WaitUntilDotNetRunningInBrowser();
+ ((IJavaScriptExecutor)Browser).ExecuteScript(
+ $"mountTestComponent('{componentTypeName}')");
+ }
+
+ private void WaitUntilDotNetRunningInBrowser()
+ {
+ new WebDriverWait(Browser, TimeSpan.FromSeconds(30)).Until(driver =>
+ {
+ return ((IJavaScriptExecutor)driver)
+ .ExecuteScript("return window.isTestReady;");
+ });
+ }
+ }
+}
diff --git a/test/testapps/BasicTestApp/Program.cs b/test/testapps/BasicTestApp/Program.cs
index 37699ba7e1..19a4ea56df 100644
--- a/test/testapps/BasicTestApp/Program.cs
+++ b/test/testapps/BasicTestApp/Program.cs
@@ -1,15 +1,26 @@
// 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.Blazor.Browser;
+using Microsoft.Blazor.Browser.Interop;
+using Microsoft.Blazor.Components;
using System;
namespace BasicTestApp
{
- class Program
+ public class Program
{
static void Main(string[] args)
{
- Console.WriteLine("BasicTestApp entrypoint invoked");
+ // Signal to tests that we're ready
+ RegisteredFunction.Invoke