Skip to content

Commit 6dc2cd2

Browse files
committed
implement DriverSettings to resolve #4
1 parent ae8341f commit 6dc2cd2

File tree

9 files changed

+150
-29
lines changed

9 files changed

+150
-29
lines changed

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System;
1+
using Aquality.Selenium.Browsers;
22
using Aquality.Selenium.Utilities;
33
using OpenQA.Selenium;
4+
using OpenQA.Selenium.Chrome;
5+
using System.Linq;
46

57
namespace Aquality.Selenium.Configurations.WebDriverSettings
68
{
@@ -17,10 +19,39 @@ public ChromeSettings(JsonFile settingsFile) : base(settingsFile)
1719
{
1820
}
1921

20-
public override DriverOptions DriverOptions => throw new NotImplementedException();
22+
public override string DownloadDirCapabilityKey => "download.default_directory";
2123

22-
public override string DownloadDir => throw new NotImplementedException();
24+
protected override BrowserName BrowserName => BrowserName.Chrome;
2325

24-
public override string DownloadDirCapabilityKey => throw new NotImplementedException();
26+
public override DriverOptions DriverOptions
27+
{
28+
get
29+
{
30+
var options = new ChromeOptions();
31+
SetChromePrefs(options);
32+
SetGlobalCapabilities(options);
33+
SetChromeArguments(options);
34+
return options;
35+
}
36+
}
37+
38+
private void SetChromePrefs(ChromeOptions options)
39+
{
40+
foreach(var option in BrowserOptions)
41+
{
42+
var value = option.Key == DownloadDirCapabilityKey ? DownloadDir : option.Value;
43+
options.AddUserProfilePreference(option.Key, value);
44+
}
45+
}
46+
47+
private void SetChromeArguments(ChromeOptions options)
48+
{
49+
options.AddArguments(BrowserStartArguments);
50+
}
51+
52+
private void SetGlobalCapabilities(ChromeOptions options)
53+
{
54+
BrowserCapabilities.ToList().ForEach(capability => options.AddAdditionalCapability(capability.Key, capability.Value, isGlobalCapability: true));
55+
}
2556
}
2657
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ private string DriverSettingsPath
3131
}
3232
}
3333

34-
private IDictionary<string, object> BrowserCapabilities
34+
protected IDictionary<string, object> BrowserCapabilities
3535
{
3636
get
3737
{
38-
return SettingsFile.GetObject<IDictionary<string, object>>($"{DriverSettingsPath}.driversettings");
38+
return SettingsFile.GetObject<IDictionary<string, object>>($"{DriverSettingsPath}.capabilities");
3939
}
4040
}
4141

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using Aquality.Selenium.Browsers;
23
using Aquality.Selenium.Utilities;
34
using OpenQA.Selenium;
5+
using OpenQA.Selenium.Edge;
46

57
namespace Aquality.Selenium.Configurations.WebDriverSettings
68
{
@@ -17,10 +19,18 @@ public EdgeSettings(JsonFile settingsFile) : base(settingsFile)
1719
{
1820
}
1921

20-
public override DriverOptions DriverOptions => throw new NotImplementedException();
21-
22-
public override string DownloadDir => throw new NotImplementedException();
22+
public override DriverOptions DriverOptions
23+
{
24+
get
25+
{
26+
var options = new EdgeOptions();
27+
SetCapabilities(options);
28+
return options;
29+
}
30+
}
31+
32+
public override string DownloadDirCapabilityKey => throw new NotSupportedException("Download directory key for Edge profiles is not supported");
2333

24-
public override string DownloadDirCapabilityKey => throw new NotImplementedException();
34+
protected override BrowserName BrowserName => BrowserName.Edge;
2535
}
2636
}

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

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System;
1+
using Aquality.Selenium.Browsers;
22
using Aquality.Selenium.Utilities;
33
using OpenQA.Selenium;
4+
using OpenQA.Selenium.Firefox;
5+
using System.Linq;
46

57
namespace Aquality.Selenium.Configurations.WebDriverSettings
68
{
@@ -17,10 +19,57 @@ public FirefoxSettings(JsonFile settingsFile) : base(settingsFile)
1719
{
1820
}
1921

20-
public override DriverOptions DriverOptions => throw new NotImplementedException();
22+
public override DriverOptions DriverOptions
23+
{
24+
get
25+
{
26+
var options = new FirefoxOptions();
27+
SetGlobalCapabilities(options);
28+
SetFirefoxPrefs(options);
29+
SetFirefoxArguments(options);
30+
return options;
31+
}
32+
}
33+
private void SetFirefoxPrefs(FirefoxOptions options)
34+
{
35+
foreach (var option in BrowserOptions)
36+
{
37+
var value = option.Key == DownloadDirCapabilityKey ? DownloadDir : option.Value;
38+
if(option.Key == DownloadDirCapabilityKey)
39+
{
40+
options.SetPreference(option.Key, DownloadDir);
41+
}
42+
else if(value is bool)
43+
{
44+
options.SetPreference(option.Key, (bool) value);
45+
}
46+
else if(value is int)
47+
{
48+
options.SetPreference(option.Key, (int) value);
49+
}
50+
else if(value is float)
51+
{
52+
options.SetPreference(option.Key, (float) value);
53+
}
54+
else if (value is string)
55+
{
56+
options.SetPreference(option.Key, value as string);
57+
}
58+
}
59+
}
60+
61+
private void SetFirefoxArguments(FirefoxOptions options)
62+
{
63+
options.AddArguments(BrowserStartArguments);
64+
}
65+
66+
private void SetGlobalCapabilities(FirefoxOptions options)
67+
{
68+
BrowserCapabilities.ToList().ForEach(capability => options.AddAdditionalCapability(capability.Key, capability.Value, isGlobalCapability: true));
69+
}
2170

22-
public override string DownloadDir => throw new NotImplementedException();
71+
public override string DownloadDirCapabilityKey => "browser.download.dir";
2372

24-
public override string DownloadDirCapabilityKey => throw new NotImplementedException();
73+
protected override BrowserName BrowserName => BrowserName.Firefox;
2574
}
2675
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using Aquality.Selenium.Browsers;
23
using Aquality.Selenium.Utilities;
34
using OpenQA.Selenium;
5+
using OpenQA.Selenium.IE;
46

57
namespace Aquality.Selenium.Configurations.WebDriverSettings
68
{
@@ -17,10 +19,18 @@ public InternetExplorerSettings(JsonFile settingsFile) : base(settingsFile)
1719
{
1820
}
1921

20-
public override DriverOptions DriverOptions => throw new NotImplementedException();
22+
public override DriverOptions DriverOptions
23+
{
24+
get
25+
{
26+
var options = new InternetExplorerOptions();
27+
SetCapabilities(options);
28+
return options;
29+
}
30+
}
2131

22-
public override string DownloadDir => throw new NotImplementedException();
32+
public override string DownloadDirCapabilityKey => throw new NotSupportedException("Download directory key for Internet Explorer profiles is not supported");
2333

24-
public override string DownloadDirCapabilityKey => throw new NotImplementedException();
34+
protected override BrowserName BrowserName => BrowserName.InternetExplorer;
2535
}
2636
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System;
1+
using Aquality.Selenium.Browsers;
22
using Aquality.Selenium.Utilities;
33
using OpenQA.Selenium;
4+
using OpenQA.Selenium.Safari;
45

56
namespace Aquality.Selenium.Configurations.WebDriverSettings
67
{
@@ -17,10 +18,17 @@ public SafariSettings(JsonFile settingsFile) : base(settingsFile)
1718
{
1819
}
1920

20-
public override DriverOptions DriverOptions => throw new NotImplementedException();
21-
22-
public override string DownloadDir => throw new NotImplementedException();
21+
public override DriverOptions DriverOptions
22+
{
23+
get
24+
{
25+
var options = new SafariOptions();
26+
SetCapabilities(options);
27+
return options;
28+
}
29+
}
30+
public override string DownloadDirCapabilityKey => "safari.options.dataDir";
2331

24-
public override string DownloadDirCapabilityKey => throw new NotImplementedException();
32+
protected override BrowserName BrowserName => BrowserName.Safari;
2533
}
2634
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
"driverSettings": {
88
"chrome": {
9-
"webDriverVersion": "latest",
9+
"webDriverVersion": "Latest",
1010
"capabilities": {
1111
"enableVNC": true
1212
},
@@ -21,7 +21,7 @@
2121
"startArguments": []
2222
},
2323
"firefox": {
24-
"webDriverVersion": "latest",
24+
"webDriverVersion": "Latest",
2525
"capabilities": {
2626
"enableVNC": true
2727
},
@@ -43,7 +43,7 @@
4343
"startArguments": []
4444
},
4545
"iexplorer": {
46-
"webDriverVersion": "latest",
46+
"webDriverVersion": "Latest",
4747
"systemArchitecture": "X32",
4848
"capabilities": {
4949
"ignoreProtectedModeSettings": true

Aquality.Selenium/src/Aquality.Selenium/Utilities/JsonFile.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Aquality.Selenium.Logging;
22
using Newtonsoft.Json;
33
using Newtonsoft.Json.Linq;
4+
using System.ComponentModel;
45
using System.IO;
56

67
namespace Aquality.Selenium.Utilities
@@ -43,7 +44,13 @@ public JsonFile(string resourceFileName)
4344
/// <returns>Object from JSON by JsonPath.</returns>
4445
public T GetObject<T>(string jsonPath)
4546
{
46-
return (T) GetValue(jsonPath);
47+
var envValue = GetEnvironmentValue(jsonPath);
48+
if (envValue != null)
49+
{
50+
return (T) TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(envValue);
51+
}
52+
53+
return GetJsonNode(jsonPath).ToObject<T>();
4754
}
4855

4956
/// <summary>
@@ -84,10 +91,16 @@ private object GetEnvironmentValueOrDefault(string jsonPath)
8491
{
8592
return node.ToObject<int>();
8693
}
87-
else
94+
else if (node.Type == JTokenType.Float)
95+
{
96+
return node.ToObject<float>();
97+
}
98+
else if (node.Type == JTokenType.String)
8899
{
89-
return node.ToString();
100+
return node.ToObject<string>();
90101
}
102+
103+
return node.ToObject<object>();
91104
}
92105
else
93106
{

Aquality.Selenium/tests/Aquality.Selenium.Tests/Unit/BrowserManager/BrowserManagerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class BrowserManagerTests
1010
[Test]
1111
public void Should_BeAbleGetBrowser()
1212
{
13-
Assert.Throws<NotImplementedException>(() => BrowserManager.Browser.WaitForPageToLoad());
13+
Assert.DoesNotThrow(() => BrowserManager.Browser.WaitForPageToLoad());
1414
}
1515
}
1616
}

0 commit comments

Comments
 (0)