Skip to content

Commit e734cd5

Browse files
authored
Rework known capabilities setting logic for Enum types to handle "unhandledPromptBehavior" capability (#196)
resolves #195
1 parent 2847889 commit e734cd5

File tree

11 files changed

+99
-36
lines changed

11 files changed

+99
-36
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public override DriverOptions DriverOptions
3030
SetChromePrefs(options);
3131
SetCapabilities(options, (name, value) => options.AddAdditionalCapability(name, value, isGlobalCapability: true));
3232
SetChromeArguments(options);
33-
SetPageLoadStratergy(options);
33+
SetPageLoadStrategy(options);
3434
return options;
3535
}
3636
}

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

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected IReadOnlyDictionary<string, object> BrowserCapabilities
6767
args: string.Join(",", capabilities.Select(cap => $"{Environment.NewLine}{cap.Key}: {cap.Value}")));
6868
}
6969
}
70-
70+
7171
return capabilities;
7272
}
7373
}
@@ -78,14 +78,14 @@ protected IReadOnlyDictionary<string, object> BrowserOptions
7878
{
7979
if (options == null)
8080
{
81-
options = SettingsFile.GetValueDictionaryOrEmpty<object>($"{DriverSettingsPath}.{nameof(options)}");
81+
options = SettingsFile.GetValueDictionaryOrEmpty<object>($"{DriverSettingsPath}.{nameof(options)}");
8282
if (options.Any())
8383
{
8484
AqualityServices.LocalizedLogger.Debug("loc.browser.options",
8585
args: string.Join(",", options.Select(opt => $"{Environment.NewLine}{opt.Key}: {opt.Value}")));
8686
}
87-
}
88-
87+
}
88+
8989
return options;
9090
}
9191
}
@@ -107,13 +107,13 @@ protected IReadOnlyList<string> BrowserStartArguments
107107
}
108108
}
109109

110-
private string DriverSettingsPath => $".driverSettings.{BrowserName.ToString().ToLowerInvariant()}";
110+
private string DriverSettingsPath => $".driverSettings.{BrowserName.ToString().ToLowerInvariant()}";
111111

112112
protected abstract BrowserName BrowserName { get; }
113113

114114
protected virtual IDictionary<string, Action<DriverOptions, object>> KnownCapabilitySetters => new Dictionary<string, Action<DriverOptions, object>>();
115115

116-
protected void SetPageLoadStratergy(DriverOptions options)
116+
protected void SetPageLoadStrategy(DriverOptions options)
117117
{
118118
options.PageLoadStrategy = PageLoadStrategy;
119119
}
@@ -126,10 +126,10 @@ protected void SetCapabilities(DriverOptions options, Action<string, object> add
126126
{
127127
var defaultAddCapabilityMethod = addCapabilityMethod ?? options.AddAdditionalCapability;
128128
defaultAddCapabilityMethod(capability.Key, capability.Value);
129-
}
129+
}
130130
catch (ArgumentException exception)
131131
{
132-
if(exception.Message.StartsWith("There is already an option"))
132+
if (exception.Message.StartsWith("There is already an option"))
133133
{
134134
SetKnownProperty(options, capability, exception);
135135
}
@@ -150,7 +150,7 @@ private void SetKnownProperty(DriverOptions options, KeyValuePair<string, object
150150
else
151151
{
152152
SetOptionByPropertyName(options, capability, exception);
153-
}
153+
}
154154
}
155155

156156
protected void SetOptionsByPropertyNames(DriverOptions options)
@@ -168,7 +168,41 @@ private void SetOptionByPropertyName(DriverOptions options, KeyValuePair<string,
168168
.GetProperties()
169169
.FirstOrDefault(property => IsPropertyNameMatchOption(property.Name, option.Key) && property.CanWrite)
170170
?? throw exception;
171-
optionProperty.SetValue(options, option.Value);
171+
var propertyType = optionProperty.PropertyType;
172+
var valueToSet = IsEnumValue(propertyType, option.Value)
173+
? ParseEnumValue(propertyType, option.Value)
174+
: option.Value;
175+
optionProperty.SetValue(options, valueToSet);
176+
}
177+
178+
private object ParseEnumValue(Type propertyType, object optionValue)
179+
{
180+
return optionValue is string
181+
? Enum.Parse(propertyType, optionValue.ToString(), ignoreCase: true)
182+
: Enum.ToObject(propertyType, Convert.ChangeType(optionValue, Enum.GetUnderlyingType(propertyType)));
183+
}
184+
185+
private bool IsEnumValue(Type propertyType, object optionValue)
186+
{
187+
var valueAsString = optionValue.ToString();
188+
if (!propertyType.IsEnum || string.IsNullOrEmpty(valueAsString))
189+
{
190+
return false;
191+
}
192+
var normalizedValue = char.ToUpper(valueAsString[0]) +
193+
(valueAsString.Length > 1 ? valueAsString.Substring(1) : string.Empty);
194+
return propertyType.IsEnumDefined(normalizedValue)
195+
|| propertyType.IsEnumDefined(valueAsString)
196+
|| (IsValueOfIntegralNumericType(optionValue)
197+
&& propertyType.IsEnumDefined(Convert.ChangeType(optionValue, Enum.GetUnderlyingType(propertyType))));
198+
}
199+
200+
private bool IsValueOfIntegralNumericType(object value)
201+
{
202+
return value is byte || value is sbyte
203+
|| value is ushort || value is short
204+
|| value is uint || value is int
205+
|| value is ulong || value is long;
172206
}
173207

174208
private bool IsPropertyNameMatchOption(string propertyName, string optionKey)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public override DriverOptions DriverOptions
3333
SetEdgeChromiumPrefs(options);
3434
SetCapabilities(options, (name, value) => options.AddAdditionalCapability(name, value, isGlobalCapability: true));
3535
SetEdgeChromiumArguments(options);
36-
SetPageLoadStratergy(options);
36+
SetPageLoadStrategy(options);
3737
return options;
3838
}
3939
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public override DriverOptions DriverOptions
2828
var options = new EdgeOptions();
2929
SetCapabilities(options);
3030
SetOptionsByPropertyNames(options);
31-
SetPageLoadStratergy(options);
31+
SetPageLoadStrategy(options);
3232
return options;
3333
}
3434
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public override DriverOptions DriverOptions
4040
SetCapabilities(options, (name, value) => options.AddAdditionalCapability(name, value, isGlobalCapability: true));
4141
SetFirefoxPrefs(options);
4242
SetFirefoxArguments(options);
43-
SetPageLoadStratergy(options);
43+
SetPageLoadStrategy(options);
4444
return options;
4545
}
4646
}
@@ -54,21 +54,21 @@ private void SetFirefoxPrefs(FirefoxOptions options)
5454
{
5555
options.SetPreference(option.Key, DownloadDir);
5656
}
57-
else if (value is bool)
57+
else if (value is bool boolean)
5858
{
59-
options.SetPreference(option.Key, (bool) value);
59+
options.SetPreference(option.Key, boolean);
6060
}
61-
else if (value is int)
61+
else if (value is int @int)
6262
{
63-
options.SetPreference(option.Key, (int) value);
63+
options.SetPreference(option.Key, @int);
6464
}
65-
else if (value is long)
65+
else if (value is long @long)
6666
{
67-
options.SetPreference(option.Key, (long)value);
67+
options.SetPreference(option.Key, @long);
6868
}
69-
else if (value is float)
69+
else if (value is float @float)
7070
{
71-
options.SetPreference(option.Key, (float) value);
71+
options.SetPreference(option.Key, @float);
7272
}
7373
else if (value is string)
7474
{

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using Aquality.Selenium.Browsers;
44
using Aquality.Selenium.Core.Configurations;
5-
using Aquality.Selenium.Core.Utilities;
65
using OpenQA.Selenium;
76
using OpenQA.Selenium.IE;
87
using OpenQA.Selenium.Remote;
@@ -29,9 +28,7 @@ public InternetExplorerSettings(ISettingsFile settingsFile) : base(settingsFile)
2928
{ "ignoreProtectedModeSettings", (options, value) => ((InternetExplorerOptions) options).IntroduceInstabilityByIgnoringProtectedModeSettings = (bool) value },
3029
{ "ignoreZoomSetting", (options, value) => ((InternetExplorerOptions) options).IgnoreZoomLevel = (bool) value },
3130
{ CapabilityType.HasNativeEvents, (options, value) => ((InternetExplorerOptions) options).EnableNativeEvents = (bool) value },
32-
{ CapabilityType.UnexpectedAlertBehavior, (options, value) => ((InternetExplorerOptions) options).UnhandledPromptBehavior = value.ToEnum<UnhandledPromptBehavior>() },
33-
{ "ie.browserCommandLineSwitches", (options, value) => ((InternetExplorerOptions) options).BrowserCommandLineArguments = value.ToString() },
34-
{ "elementScrollBehavior", (options, value) => ((InternetExplorerOptions) options).ElementScrollBehavior = value.ToEnum<InternetExplorerElementScrollBehavior>() }
31+
{ "ie.browserCommandLineSwitches", (options, value) => ((InternetExplorerOptions) options).BrowserCommandLineArguments = value.ToString() }
3532
};
3633

3734
public override DriverOptions DriverOptions
@@ -41,7 +38,7 @@ public override DriverOptions DriverOptions
4138
var options = new InternetExplorerOptions();
4239
SetCapabilities(options);
4340
SetOptionsByPropertyNames(options);
44-
SetPageLoadStratergy(options);
41+
SetPageLoadStrategy(options);
4542
options.BrowserCommandLineArguments = string.Join(" ", BrowserStartArguments);
4643
return options;
4744
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override DriverOptions DriverOptions
2727
var options = new SafariOptions();
2828
SetCapabilities(options);
2929
SetOptionsByPropertyNames(options);
30-
SetPageLoadStratergy(options);
30+
SetPageLoadStrategy(options);
3131
return options;
3232
}
3333
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"driver": "INFO",
1414
"server": "OFF",
1515
"browser": "FINE"
16-
}
16+
},
17+
"unhandledPromptBehavior": "default"
1718
},
1819
"options": {
1920
"intl.accept_languages": "en",
@@ -29,7 +30,8 @@
2930
"firefox": {
3031
"webDriverVersion": "Latest",
3132
"capabilities": {
32-
"enableVNC": true
33+
"enableVNC": true,
34+
"unhandledPromptBehavior": "default"
3335
},
3436
"options": {
3537
"intl.accept_languages": "en",
@@ -52,7 +54,8 @@
5254
"webDriverVersion": "Latest",
5355
"systemArchitecture": "X32",
5456
"capabilities": {
55-
"ignoreProtectedModeSettings": true
57+
"ignoreProtectedModeSettings": true,
58+
"unhandledPromptBehavior": "default"
5659
},
5760
"options": {
5861
},
@@ -61,6 +64,7 @@
6164
"edge": {
6265
"systemArchitecture": "X32",
6366
"capabilities": {
67+
"unhandledPromptBehavior": "default"
6468
},
6569
"options": {
6670
},

Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/AlertTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@ public void Should_BePossibleTo_AcceptConfirmationAlert()
3131
Assert.AreEqual("You clicked: Ok", alertsForm.ResultLabel.GetText());
3232
}
3333

34+
[Test]
35+
public void Should_BePossibleTo_AcceptConfirmationAlert_InWaitFor()
36+
{
37+
alertsForm.JsConfirmButton.Click();
38+
AqualityServices.ConditionalWait.WaitFor(driver =>
39+
{
40+
try
41+
{
42+
AqualityServices.Logger.Debug($"Current url: {driver.Url}");
43+
return false;
44+
}
45+
catch (UnhandledAlertException e)
46+
{
47+
AqualityServices.Logger.Debug($"Alert appeared: {e.Message}");
48+
AqualityServices.Browser.HandleAlert(AlertAction.Accept);
49+
return true;
50+
}
51+
52+
});
53+
Assert.AreEqual("You clicked: Ok", alertsForm.ResultLabel.GetText());
54+
}
55+
3456
[Test]
3557
public void Should_BePossibleTo_DeclineConfirmationAlert()
3658
{

Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/settings.azure.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"chrome": {
99
"webDriverVersion": "MatchingBrowser",
1010
"capabilities": {
11-
"enableVNC": true
11+
"enableVNC": true,
12+
"unhandledPromptBehavior": "ignore"
1213
},
1314
"options": {
1415
"intl.accept_languages": "en",

0 commit comments

Comments
 (0)