2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
- using System . Runtime . InteropServices ;
6
- using System . Text . RegularExpressions ;
5
+ using System . Linq ;
6
+ using System . Reflection ;
7
7
using OpenQA . Selenium ;
8
8
using OpenQA . Selenium . Chrome ;
9
9
using OpenQA . Selenium . Remote ;
@@ -18,7 +18,7 @@ public BrowserFixture(IMessageSink diagnosticsMessageSink)
18
18
{
19
19
DiagnosticsMessageSink = diagnosticsMessageSink ;
20
20
21
- if ( ! HostSupportsBrowserAutomation )
21
+ if ( ! IsHostAutomationSupported ( ) )
22
22
{
23
23
DiagnosticsMessageSink . OnMessage ( new DiagnosticMessage ( "Host does not support browser automation." ) ) ;
24
24
return ;
@@ -41,20 +41,10 @@ public BrowserFixture(IMessageSink diagnosticsMessageSink)
41
41
DiagnosticsMessageSink . OnMessage ( new DiagnosticMessage ( $ "Set { nameof ( ChromeOptions ) } .{ nameof ( opts . BinaryLocation ) } to { binaryLocation } ") ) ;
42
42
}
43
43
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 ) ;
58
48
}
59
49
60
50
public IWebDriver Browser { get ; }
@@ -63,27 +53,28 @@ public BrowserFixture(IMessageSink diagnosticsMessageSink)
63
53
64
54
public IMessageSink DiagnosticsMessageSink { get ; }
65
55
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 ( )
76
57
{
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
+ }
87
78
}
88
79
89
80
public void Dispose ( )
0 commit comments