Skip to content

Commit f14fb59

Browse files
committed
Cleanups
1 parent 5f602c2 commit f14fb59

File tree

5 files changed

+46
-41
lines changed

5 files changed

+46
-41
lines changed

src/ProjectTemplates/test/MvcTemplateTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public MvcTemplateTest(ProjectFactoryFixture projectFactory, ITestOutputHelper o
1818

1919
[Theory]
2020
[InlineData(null)]
21-
//[InlineData("F#", Skip = "https://github.com/aspnet/Templating/issues/673")]
21+
[InlineData("F#", Skip = "https://github.com/aspnet/Templating/issues/673")]
2222
private void MvcTemplate_NoAuthImpl(string languageOverride)
2323
{
2424
Project.RunDotNetNew("mvc", language: languageOverride);

src/ProjectTemplates/test/RazorComponentsTemplateTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private void TestApplication(bool publish)
3737
{
3838
aspNetProcess.AssertStatusCode("/", HttpStatusCode.OK, "text/html");
3939

40-
if (BrowserFixture.HostSupportsBrowserAutomation)
40+
if (BrowserFixture.IsHostAutomationSupported())
4141
{
4242
aspNetProcess.VisitInBrowser(Browser);
4343
TestBasicNavigation();

src/ProjectTemplates/test/SpaTemplateTest/SpaTemplateTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private void TestApplication(bool publish)
5656
{
5757
aspNetProcess.AssertStatusCode("/", HttpStatusCode.OK, "text/html");
5858

59-
if (BrowserFixture.HostSupportsBrowserAutomation)
59+
if (BrowserFixture.IsHostAutomationSupported())
6060
{
6161
aspNetProcess.VisitInBrowser(Browser);
6262
TestBasicNavigation();

src/Shared/E2ETesting/BrowserFixture.cs

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Runtime.InteropServices;
6-
using System.Text.RegularExpressions;
5+
using System.Linq;
6+
using System.Reflection;
77
using OpenQA.Selenium;
88
using OpenQA.Selenium.Chrome;
99
using OpenQA.Selenium.Remote;
@@ -18,7 +18,7 @@ public BrowserFixture(IMessageSink diagnosticsMessageSink)
1818
{
1919
DiagnosticsMessageSink = diagnosticsMessageSink;
2020

21-
if (!HostSupportsBrowserAutomation)
21+
if (!IsHostAutomationSupported())
2222
{
2323
DiagnosticsMessageSink.OnMessage(new DiagnosticMessage("Host does not support browser automation."));
2424
return;
@@ -41,20 +41,10 @@ public BrowserFixture(IMessageSink diagnosticsMessageSink)
4141
DiagnosticsMessageSink.OnMessage(new DiagnosticMessage($"Set {nameof(ChromeOptions)}.{nameof(opts.BinaryLocation)} to {binaryLocation}"));
4242
}
4343

44-
try
45-
{
46-
var driver = new RemoteWebDriver(SeleniumStandaloneServer.Instance.Uri, opts);
47-
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1);
48-
Browser = driver;
49-
Logs = new RemoteLogs(driver);
50-
}
51-
catch (WebDriverException ex)
52-
{
53-
var message =
54-
"Failed to connect to the web driver. Please see the readme and follow the instructions to install selenium." +
55-
"Remember to start the web driver with `selenium-standalone start` before running the end-to-end tests.";
56-
throw new InvalidOperationException(message, ex);
57-
}
44+
var driver = new RemoteWebDriver(SeleniumStandaloneServer.Instance.Uri, opts);
45+
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1);
46+
Browser = driver;
47+
Logs = new RemoteLogs(driver);
5848
}
5949

6050
public IWebDriver Browser { get; }
@@ -63,27 +53,28 @@ public BrowserFixture(IMessageSink diagnosticsMessageSink)
6353

6454
public IMessageSink DiagnosticsMessageSink { get; }
6555

66-
public static bool HostSupportsBrowserAutomation => string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("ASPNETCORE_BROWSER_AUTOMATION_DISABLED")) &&
67-
(IsAppVeyor || (IsVSTS && RuntimeInformation.OSDescription.Contains("Microsoft Windows")) || OSSupportsEdge());
68-
69-
private static bool IsAppVeyor =>
70-
Environment.GetEnvironmentVariables().Contains("APPVEYOR");
71-
72-
private static bool IsVSTS =>
73-
Environment.GetEnvironmentVariables().Contains("TF_BUILD");
74-
75-
private static int GetWindowsVersion()
56+
public static bool IsHostAutomationSupported()
7657
{
77-
var osDescription = RuntimeInformation.OSDescription;
78-
var windowsVersion = Regex.Match(osDescription, "^Microsoft Windows (\\d+)\\..*");
79-
return windowsVersion.Success ? int.Parse(windowsVersion.Groups[1].Value) : -1;
80-
}
81-
82-
private static bool OSSupportsEdge()
83-
{
84-
var windowsVersion = GetWindowsVersion();
85-
return (windowsVersion >= 10 && windowsVersion < 2000)
86-
|| (windowsVersion >= 2016);
58+
// We emit an assemblymetadata attribute that reflects the value of SeleniumE2ETestsSupported at build
59+
// time and we use that to conditionally skip Selenium tests parts.
60+
var attribute = typeof(BrowserFixture).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
61+
.SingleOrDefault(a => a.Key == "Microsoft.AspNetCore.Testing.Selenium.Supported");
62+
var attributeValue = attribute != null ? bool.Parse(attribute.Value) : false;
63+
64+
// The environment variable below can be set up before running the tests so as to override the default
65+
// value provided in the attribute.
66+
var environmentOverride = Environment
67+
.GetEnvironmentVariable("MICROSOFT_ASPNETCORE_TESTING_SELENIUM_SUPPORTED");
68+
var environmentOverrideValue = !string.IsNullOrWhiteSpace(environmentOverride) ? bool.Parse(attribute.Value) : false;
69+
70+
if (environmentOverride != null)
71+
{
72+
return environmentOverrideValue;
73+
}
74+
else
75+
{
76+
return attributeValue;
77+
}
8778
}
8879

8980
public void Dispose()

src/Shared/E2ETesting/E2ETesting.targets

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
</ItemGroup>
6868
</Target>
6969

70-
<Target Name="_AddMetadataAttributes" BeforeTargets="BeforeCompile" DependsOnTargets="_ResolveTestProjectReferences">
70+
<Target Name="_AddTestProjectMetadataAttributes" BeforeTargets="BeforeCompile" DependsOnTargets="_ResolveTestProjectReferences">
7171
<ItemGroup>
7272
<_ContentRootMetadata
7373
Condition="'%(_ContentRootProjectReferences.Identity)' != ''"
@@ -88,4 +88,18 @@
8888
</ItemGroup>
8989
</Target>
9090

91+
<Target Name="_AddSeleniumSupportedMetadataAttribute" BeforeTargets="BeforeCompile">
92+
<PropertyGroup>
93+
<_SeleniumE2ETestsSupportedAttributeValue>false</_SeleniumE2ETestsSupportedAttributeValue>
94+
<_SeleniumE2ETestsSupportedAttributeValue Condition="'$(SeleniumE2ETestsSupported)' == 'true'">true</_SeleniumE2ETestsSupportedAttributeValue>
95+
</PropertyGroup>
96+
<ItemGroup>
97+
<AssemblyAttribute
98+
Include="System.Reflection.AssemblyMetadataAttribute">
99+
<_Parameter1>Microsoft.AspNetCore.Testing.Selenium.Supported</_Parameter1>
100+
<_Parameter2>$(SeleniumE2ETestsSupported)</_Parameter2>
101+
</AssemblyAttribute>
102+
</ItemGroup>
103+
</Target>
104+
91105
</Project>

0 commit comments

Comments
 (0)