10
10
using System ;
11
11
using System . IO ;
12
12
using WebDriverManager ;
13
- using WebDriverManager . DriverConfigs ;
14
13
using WebDriverManager . DriverConfigs . Impl ;
15
- using WebDriverManager . Helpers ;
16
14
using Aquality . Selenium . Core . Localization ;
15
+ using System . Reflection ;
16
+ using System . Text . RegularExpressions ;
17
+ using Aquality . Selenium . Core . Logging ;
17
18
18
19
namespace Aquality . Selenium . Browsers
19
20
{
@@ -22,8 +23,9 @@ namespace Aquality.Selenium.Browsers
22
23
/// </summary>
23
24
public class LocalBrowserFactory : BrowserFactory
24
25
{
25
- private static readonly object WebDriverDownloadingLock = new object ( ) ;
26
26
private const string HostAddressDefault = "::1" ;
27
+ private const string DriverVersionVariableName = "SE_DRIVER_VERSION" ;
28
+ private const string CurrentBrowserVersionPattern = "Current browser version is ([\\ d,\\ .]+)" ;
27
29
28
30
public LocalBrowserFactory ( IActionRetrier actionRetrier , IBrowserProfile browserProfile , ITimeoutConfiguration timeoutConfiguration , ILocalizedLogger localizedLogger )
29
31
: base ( actionRetrier , browserProfile , timeoutConfiguration , localizedLogger )
@@ -42,34 +44,36 @@ protected override WebDriver Driver
42
44
{
43
45
case BrowserName . Chrome :
44
46
case BrowserName . Yandex :
45
- SetUpDriver ( new ChromeConfig ( ) , driverSettings ) ;
46
- driver = GetDriver < ChromeDriver > ( ChromeDriverService . CreateDefaultService ( ) ,
47
+ driver = GetDriver < ChromeDriver > ( ( ) => ChromeDriverService . CreateDefaultService ( ) ,
47
48
( ChromeOptions ) driverSettings . DriverOptions , commandTimeout ) ;
48
49
break ;
49
50
case BrowserName . Firefox :
50
- SetUpDriver ( new FirefoxConfig ( ) , driverSettings ) ;
51
- var geckoService = FirefoxDriverService . CreateDefaultService ( ) ;
52
- geckoService . Host = ( ( FirefoxSettings ) driverSettings ) . IsGeckoServiceHostDefaultEnabled ? HostAddressDefault : geckoService . Host ;
53
- driver = GetDriver < FirefoxDriver > ( geckoService , ( FirefoxOptions ) driverSettings . DriverOptions , commandTimeout ) ;
51
+ Func < DriverService > geckoServiceProvider = ( ) =>
52
+ {
53
+ var geckoService = FirefoxDriverService . CreateDefaultService ( ) ;
54
+ geckoService . Host = ( ( FirefoxSettings ) driverSettings ) . IsGeckoServiceHostDefaultEnabled ? HostAddressDefault : geckoService . Host ;
55
+ return geckoService ;
56
+ } ;
57
+
58
+ driver = GetDriver < FirefoxDriver > ( geckoServiceProvider , ( FirefoxOptions ) driverSettings . DriverOptions , commandTimeout ) ;
54
59
break ;
55
60
case BrowserName . IExplorer :
56
- SetUpDriver ( new InternetExplorerConfig ( ) , driverSettings ) ;
57
- driver = GetDriver < InternetExplorerDriver > ( InternetExplorerDriverService . CreateDefaultService ( ) ,
61
+ driver = GetDriver < InternetExplorerDriver > ( ( ) => InternetExplorerDriverService . CreateDefaultService ( ) ,
58
62
( InternetExplorerOptions ) driverSettings . DriverOptions , commandTimeout ) ;
59
63
break ;
60
64
case BrowserName . Edge :
61
- SetUpDriver ( new EdgeConfig ( ) , driverSettings ) ;
62
- driver = GetDriver < EdgeDriver > ( EdgeDriverService . CreateDefaultService ( ) ,
65
+ driver = GetDriver < EdgeDriver > ( ( ) => EdgeDriverService . CreateDefaultService ( ) ,
63
66
( EdgeOptions ) driverSettings . DriverOptions , commandTimeout ) ;
64
67
break ;
65
68
case BrowserName . Opera :
66
69
var config = new OperaConfig ( ) ;
67
- var driverPath = SetUpDriver ( config , driverSettings ) ;
68
- driver = GetDriver < ChromeDriver > ( ChromeDriverService . CreateDefaultService ( Path . GetDirectoryName ( driverPath ) , config . GetBinaryName ( ) ) ,
70
+ var operaSettings = ( OperaSettings ) driverSettings ;
71
+ var driverPath = new DriverManager ( ) . SetUpDriver ( config , operaSettings . WebDriverVersion , operaSettings . SystemArchitecture ) ;
72
+ driver = GetDriver < ChromeDriver > ( ( ) => ChromeDriverService . CreateDefaultService ( Path . GetDirectoryName ( driverPath ) , config . GetBinaryName ( ) ) ,
69
73
( ChromeOptions ) driverSettings . DriverOptions , commandTimeout ) ;
70
74
break ;
71
75
case BrowserName . Safari :
72
- driver = GetDriver < SafariDriver > ( SafariDriverService . CreateDefaultService ( ) ,
76
+ driver = GetDriver < SafariDriver > ( ( ) => SafariDriverService . CreateDefaultService ( ) ,
73
77
( SafariOptions ) driverSettings . DriverOptions , commandTimeout ) ;
74
78
break ;
75
79
default :
@@ -79,26 +83,21 @@ protected override WebDriver Driver
79
83
}
80
84
}
81
85
82
- private WebDriver GetDriver < T > ( DriverService driverService , DriverOptions driverOptions , TimeSpan commandTimeout ) where T : WebDriver
83
- {
84
- return ( T ) Activator . CreateInstance ( typeof ( T ) , driverService , driverOptions , commandTimeout ) ;
85
- }
86
-
87
- private static string SetUpDriver ( IDriverConfig driverConfig , IDriverSettings driverSettings )
86
+ private WebDriver GetDriver < T > ( Func < DriverService > driverServiceProvider , DriverOptions driverOptions , TimeSpan commandTimeout ) where T : WebDriver
88
87
{
89
- var architecture = driverSettings . SystemArchitecture . Equals ( Architecture . Auto ) ? ArchitectureHelper . GetArchitecture ( ) : driverSettings . SystemArchitecture ;
90
- var version = driverSettings . WebDriverVersion . Equals ( VersionResolveStrategy . Latest ) ? driverConfig . GetLatestVersion ( ) : driverSettings . WebDriverVersion ;
91
- version = version . Equals ( VersionResolveStrategy . MatchingBrowser ) ? driverConfig . GetMatchingBrowserVersion ( ) : version ;
92
- var url = UrlHelper . BuildUrl ( architecture . Equals ( Architecture . X32 ) ? driverConfig . GetUrl32 ( ) : driverConfig . GetUrl64 ( ) , version ) ;
93
- var binaryPath = FileHelper . GetBinDestination ( driverConfig . GetName ( ) , version , architecture , driverConfig . GetBinaryName ( ) ) ;
94
- if ( ! File . Exists ( binaryPath ) || ! Environment . GetEnvironmentVariable ( "PATH" ) . Contains ( binaryPath ) )
88
+ var currentBrowserVersionRegex = new Regex ( CurrentBrowserVersionPattern , RegexOptions . None , TimeoutConfiguration . Condition ) ;
89
+ try
95
90
{
96
- lock ( WebDriverDownloadingLock )
97
- {
98
- return new DriverManager ( ) . SetUpDriver ( url , binaryPath ) ;
99
- }
91
+ return ( T ) Activator . CreateInstance ( typeof ( T ) , driverServiceProvider . Invoke ( ) , driverOptions , commandTimeout ) ;
92
+ }
93
+ catch ( TargetInvocationException exception )
94
+ when ( exception . InnerException != null && currentBrowserVersionRegex . IsMatch ( exception . InnerException . Message ) )
95
+ {
96
+ Logger . Instance . Debug ( exception . InnerException . Message , exception ) ;
97
+ var currentVersion = currentBrowserVersionRegex . Match ( exception . InnerException . Message ) . Groups [ 1 ] . Value ;
98
+ Environment . SetEnvironmentVariable ( DriverVersionVariableName , currentVersion ) ;
99
+ return ( T ) Activator . CreateInstance ( typeof ( T ) , driverServiceProvider . Invoke ( ) , driverOptions , commandTimeout ) ;
100
100
}
101
- return binaryPath ;
102
101
}
103
102
}
104
103
}
0 commit comments