Skip to content

Commit 9c3b1d4

Browse files
committed
finished implementetion of DriverSettings.
fixed IE, Edge and Firefox issues add support for "KnownCapabilities" and options for IE/Edge remove WebDriverManager setup for Edge rename IE in BrowserName enum Resolves #4
1 parent 6dc2cd2 commit 9c3b1d4

File tree

16 files changed

+260
-56
lines changed

16 files changed

+260
-56
lines changed

Aquality.Selenium/src/Aquality.Selenium/Browsers/Browser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public enum BrowserName
295295
Chrome,
296296
Edge,
297297
Firefox,
298-
InternetExplorer,
298+
IExplorer,
299299
Safari
300300
}
301301

Aquality.Selenium/src/Aquality.Selenium/Browsers/LocalBrowserFactory.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ private Browser CreateBrowser()
3838
driverManager.SetUpDriver(new FirefoxConfig(), driverSettings.WebDriverVersion, driverSettings.SystemArchitecture);
3939
driver = new FirefoxDriver((FirefoxOptions)driverSettings.DriverOptions);
4040
break;
41-
case BrowserName.InternetExplorer:
41+
case BrowserName.IExplorer:
4242
driverManager.SetUpDriver(new InternetExplorerConfig(), driverSettings.WebDriverVersion, driverSettings.SystemArchitecture);
4343
driver = new InternetExplorerDriver((InternetExplorerOptions)driverSettings.DriverOptions);
4444
break;
4545
case BrowserName.Edge:
46-
driverManager.SetUpDriver(new EdgeConfig(), driverSettings.WebDriverVersion, driverSettings.SystemArchitecture);
4746
driver = new EdgeDriver((EdgeOptions)driverSettings.DriverOptions);
4847
break;
4948
case BrowserName.Safari:

Aquality.Selenium/src/Aquality.Selenium/Configurations/BrowserProfile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public IDriverSettings DriverSettings
4141
return new EdgeSettings(settingsFile);
4242
case BrowserName.Firefox:
4343
return new FirefoxSettings(settingsFile);
44-
case BrowserName.InternetExplorer:
44+
case BrowserName.IExplorer:
4545
return new InternetExplorerSettings(settingsFile);
4646
case BrowserName.Safari:
4747
return new SafariSettings(settingsFile);

Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/ChromeSettings.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Aquality.Selenium.Utilities;
33
using OpenQA.Selenium;
44
using OpenQA.Selenium.Chrome;
5-
using System.Linq;
65

76
namespace Aquality.Selenium.Configurations.WebDriverSettings
87
{
@@ -19,17 +18,17 @@ public ChromeSettings(JsonFile settingsFile) : base(settingsFile)
1918
{
2019
}
2120

22-
public override string DownloadDirCapabilityKey => "download.default_directory";
23-
2421
protected override BrowserName BrowserName => BrowserName.Chrome;
2522

23+
public override string DownloadDirCapabilityKey => "download.default_directory";
24+
2625
public override DriverOptions DriverOptions
2726
{
2827
get
2928
{
3029
var options = new ChromeOptions();
3130
SetChromePrefs(options);
32-
SetGlobalCapabilities(options);
31+
SetCapabilities(options, (name, value) => options.AddAdditionalCapability(name, value, isGlobalCapability: true));
3332
SetChromeArguments(options);
3433
return options;
3534
}
@@ -48,10 +47,5 @@ private void SetChromeArguments(ChromeOptions options)
4847
{
4948
options.AddArguments(BrowserStartArguments);
5049
}
51-
52-
private void SetGlobalCapabilities(ChromeOptions options)
53-
{
54-
BrowserCapabilities.ToList().ForEach(capability => options.AddAdditionalCapability(capability.Key, capability.Value, isGlobalCapability: true));
55-
}
5650
}
5751
}

Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/DriverSettings.cs

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,30 @@ protected IList<string> BrowserStartArguments
5959

6060
protected abstract BrowserName BrowserName { get; }
6161

62+
protected virtual IDictionary<string, Action<DriverOptions, object>> KnownCapabilitySetters => new Dictionary<string, Action<DriverOptions, object>>();
63+
6264
public abstract string DownloadDirCapabilityKey { get; }
6365

6466
public abstract DriverOptions DriverOptions { get; }
6567

66-
public string WebDriverVersion => SettingsFile.GetObject<string>($"{DriverSettingsPath}.webDriverVersion");
68+
public string WebDriverVersion
69+
{
70+
get
71+
{
72+
var jsonPath = $"{DriverSettingsPath}.webDriverVersion";
73+
return SettingsFile.IsValuePresent(jsonPath)
74+
? SettingsFile.GetObject<string>(jsonPath)
75+
: "Latest";
76+
}
77+
}
6778

6879
public Architecture SystemArchitecture
6980
{
7081
get
7182
{
7283
var jsonPath = $"{DriverSettingsPath}.systemArchitecture";
7384
return SettingsFile.IsValuePresent(jsonPath)
74-
? (Architecture) Enum.Parse(typeof(Architecture), SettingsFile.GetObject<string>(jsonPath))
85+
? SettingsFile.GetObject<string>(jsonPath).ToEnum<Architecture>()
7586
: Architecture.Auto;
7687
}
7788
}
@@ -90,9 +101,63 @@ public virtual string DownloadDir
90101
}
91102
}
92103

93-
protected void SetCapabilities(DriverOptions options)
104+
protected void SetCapabilities(DriverOptions options, Action<string, object> addCapabilityMethod = null)
105+
{
106+
foreach(var capability in BrowserCapabilities)
107+
{
108+
try
109+
{
110+
var defaultAddCapabilityMethod = addCapabilityMethod ?? options.AddAdditionalCapability;
111+
defaultAddCapabilityMethod(capability.Key, capability.Value);
112+
}
113+
catch(ArgumentException exception)
114+
{
115+
if(exception.Message.StartsWith("There is already an option"))
116+
{
117+
SetKnownProperty(options, capability, exception);
118+
}
119+
else
120+
{
121+
throw exception;
122+
}
123+
}
124+
}
125+
}
126+
127+
private void SetKnownProperty(DriverOptions options, KeyValuePair<string, object> capability, ArgumentException exception)
128+
{
129+
if (KnownCapabilitySetters.ContainsKey(capability.Key))
130+
{
131+
KnownCapabilitySetters[capability.Key](options, capability.Value);
132+
}
133+
else
134+
{
135+
SetOptionByPropertyName(options, capability, exception);
136+
}
137+
}
138+
139+
protected void SetOptionsByPropertyNames(DriverOptions options)
140+
{
141+
foreach (var option in BrowserOptions)
142+
{
143+
SetOptionByPropertyName(options, option, new NotSupportedException($"Property for option {option.Key} is not supported"));
144+
}
145+
}
146+
147+
private void SetOptionByPropertyName(DriverOptions options, KeyValuePair<string, object> option, Exception exception)
148+
{
149+
var optionProperty = options
150+
.GetType()
151+
.GetProperties()
152+
.FirstOrDefault(property => IsPropertyNameMatchOption(property.Name, option.Key) && property.CanWrite)
153+
?? throw exception;
154+
optionProperty.SetValue(options, option.Value);
155+
}
156+
157+
private bool IsPropertyNameMatchOption(string propertyName, string optionKey)
94158
{
95-
BrowserCapabilities.ToList().ForEach(capability => options.AddAdditionalCapability(capability.Key, capability.Value));
159+
return propertyName.Equals(optionKey, StringComparison.InvariantCultureIgnoreCase)
160+
|| optionKey.ToLowerInvariant().Contains(propertyName.ToLowerInvariant());
96161
}
97162
}
98163
}

Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/EdgeSettings.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@ public EdgeSettings(JsonFile settingsFile) : base(settingsFile)
1919
{
2020
}
2121

22+
protected override BrowserName BrowserName => BrowserName.Edge;
23+
2224
public override DriverOptions DriverOptions
2325
{
2426
get
2527
{
2628
var options = new EdgeOptions();
2729
SetCapabilities(options);
30+
SetOptionsByPropertyNames(options);
2831
return options;
2932
}
3033
}
3134

3235
public override string DownloadDirCapabilityKey => throw new NotSupportedException("Download directory key for Edge profiles is not supported");
33-
34-
protected override BrowserName BrowserName => BrowserName.Edge;
3536
}
3637
}

Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/FirefoxSettings.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
using Aquality.Selenium.Utilities;
33
using OpenQA.Selenium;
44
using OpenQA.Selenium.Firefox;
5-
using System.Linq;
5+
using System;
6+
using System.Collections.Generic;
67

78
namespace Aquality.Selenium.Configurations.WebDriverSettings
89
{
@@ -19,17 +20,29 @@ public FirefoxSettings(JsonFile settingsFile) : base(settingsFile)
1920
{
2021
}
2122

23+
protected override BrowserName BrowserName => BrowserName.Firefox;
24+
25+
protected override IDictionary<string, Action<DriverOptions, object>> KnownCapabilitySetters => new Dictionary<string, Action<DriverOptions, object>>
26+
{
27+
{ "binary", (options, value) => ((FirefoxOptions) options).BrowserExecutableLocation = value.ToString() },
28+
{ "firefox_binary", (options, value) => ((FirefoxOptions) options).BrowserExecutableLocation = value.ToString() },
29+
{ "firefox_profile", (options, value) => ((FirefoxOptions) options).Profile = new FirefoxProfileManager().GetProfile(value.ToString()) },
30+
{ "log", (options, value) => ((FirefoxOptions) options).LogLevel = value.ToEnum<FirefoxDriverLogLevel>() },
31+
{ "marionette", (options, value) => ((FirefoxOptions) options).UseLegacyImplementation = (bool) value }
32+
};
33+
2234
public override DriverOptions DriverOptions
2335
{
2436
get
2537
{
2638
var options = new FirefoxOptions();
27-
SetGlobalCapabilities(options);
39+
SetCapabilities(options, (name, value) => options.AddAdditionalCapability(name, value, isGlobalCapability: true));
2840
SetFirefoxPrefs(options);
2941
SetFirefoxArguments(options);
3042
return options;
3143
}
3244
}
45+
3346
private void SetFirefoxPrefs(FirefoxOptions options)
3447
{
3548
foreach (var option in BrowserOptions)
@@ -63,13 +76,6 @@ private void SetFirefoxArguments(FirefoxOptions options)
6376
options.AddArguments(BrowserStartArguments);
6477
}
6578

66-
private void SetGlobalCapabilities(FirefoxOptions options)
67-
{
68-
BrowserCapabilities.ToList().ForEach(capability => options.AddAdditionalCapability(capability.Key, capability.Value, isGlobalCapability: true));
69-
}
70-
7179
public override string DownloadDirCapabilityKey => "browser.download.dir";
72-
73-
protected override BrowserName BrowserName => BrowserName.Firefox;
7480
}
7581
}

Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/InternetExplorerSettings.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2+
using System.Collections.Generic;
23
using Aquality.Selenium.Browsers;
34
using Aquality.Selenium.Utilities;
45
using OpenQA.Selenium;
56
using OpenQA.Selenium.IE;
7+
using OpenQA.Selenium.Remote;
68

79
namespace Aquality.Selenium.Configurations.WebDriverSettings
810
{
@@ -19,18 +21,30 @@ public InternetExplorerSettings(JsonFile settingsFile) : base(settingsFile)
1921
{
2022
}
2123

24+
protected override BrowserName BrowserName => BrowserName.IExplorer;
25+
26+
protected override IDictionary<string, Action<DriverOptions, object>> KnownCapabilitySetters => new Dictionary<string, Action<DriverOptions, object>>
27+
{
28+
{ "ignoreProtectedModeSettings", (options, value) => ((InternetExplorerOptions) options).IntroduceInstabilityByIgnoringProtectedModeSettings = (bool) value },
29+
{ "ignoreZoomSetting", (options, value) => ((InternetExplorerOptions) options).IgnoreZoomLevel = (bool) value },
30+
{ CapabilityType.HasNativeEvents, (options, value) => ((InternetExplorerOptions) options).EnableNativeEvents = (bool) value },
31+
{ CapabilityType.UnexpectedAlertBehavior, (options, value) => ((InternetExplorerOptions) options).UnhandledPromptBehavior = value.ToEnum<UnhandledPromptBehavior>() },
32+
{ "ie.browserCommandLineSwitches", (options, value) => ((InternetExplorerOptions) options).BrowserCommandLineArguments = value.ToString() },
33+
{ "elementScrollBehavior", (options, value) => ((InternetExplorerOptions) options).ElementScrollBehavior = value.ToEnum<InternetExplorerElementScrollBehavior>() }
34+
};
35+
2236
public override DriverOptions DriverOptions
2337
{
2438
get
2539
{
2640
var options = new InternetExplorerOptions();
2741
SetCapabilities(options);
42+
SetOptionsByPropertyNames(options);
43+
options.BrowserCommandLineArguments = string.Join(" ", BrowserStartArguments);
2844
return options;
2945
}
3046
}
3147

3248
public override string DownloadDirCapabilityKey => throw new NotSupportedException("Download directory key for Internet Explorer profiles is not supported");
33-
34-
protected override BrowserName BrowserName => BrowserName.InternetExplorer;
3549
}
3650
}

Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/SafariSettings.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ public SafariSettings(JsonFile settingsFile) : base(settingsFile)
1818
{
1919
}
2020

21+
protected override BrowserName BrowserName => BrowserName.Safari;
22+
2123
public override DriverOptions DriverOptions
2224
{
2325
get
2426
{
2527
var options = new SafariOptions();
2628
SetCapabilities(options);
29+
SetOptionsByPropertyNames(options);
2730
return options;
2831
}
2932
}
3033
public override string DownloadDirCapabilityKey => "safari.options.dataDir";
31-
32-
protected override BrowserName BrowserName => BrowserName.Safari;
3334
}
3435
}

Aquality.Selenium/src/Aquality.Selenium/Resources/settings.json

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"browserName" : "chrome",
2+
"browserName": "chrome",
33
"isRemote": true,
44
"remoteConnectionUrl": "http://qa-auto-nexus:4444/wd/hub",
5-
"isElementHighlightEnabled" : true,
5+
"isElementHighlightEnabled": true,
66

77
"driverSettings": {
88
"chrome": {
@@ -48,7 +48,17 @@
4848
"capabilities": {
4949
"ignoreProtectedModeSettings": true
5050
},
51-
"options": {},
51+
"options": {
52+
},
53+
"startArguments": []
54+
},
55+
"edge": {
56+
"webDriverVersion": "Latest",
57+
"systemArchitecture": "X32",
58+
"capabilities": {
59+
},
60+
"options": {
61+
},
5262
"startArguments": []
5363
},
5464
"safari": {
@@ -59,10 +69,10 @@
5969
}
6070
},
6171
"timeouts": {
62-
"timeoutImplicit" : 0,
63-
"timeoutCondition" : 30,
64-
"timeoutScript" : 10,
65-
"timeoutPageLoad" : 15,
72+
"timeoutImplicit": 0,
73+
"timeoutCondition": 30,
74+
"timeoutScript": 10,
75+
"timeoutPageLoad": 15,
6676
"timeoutPollingInterval": 300
6777
}
6878
}

0 commit comments

Comments
 (0)