Skip to content

Commit

Permalink
Rework known capabilities setting logic for Enum types to handle "unh…
Browse files Browse the repository at this point in the history
…andledPromptBehavior" capability (#196)

resolves #195
  • Loading branch information
mialeska authored Nov 18, 2020
1 parent 2847889 commit e734cd5
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override DriverOptions DriverOptions
SetChromePrefs(options);
SetCapabilities(options, (name, value) => options.AddAdditionalCapability(name, value, isGlobalCapability: true));
SetChromeArguments(options);
SetPageLoadStratergy(options);
SetPageLoadStrategy(options);
return options;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected IReadOnlyDictionary<string, object> BrowserCapabilities
args: string.Join(",", capabilities.Select(cap => $"{Environment.NewLine}{cap.Key}: {cap.Value}")));
}
}

return capabilities;
}
}
Expand All @@ -78,14 +78,14 @@ protected IReadOnlyDictionary<string, object> BrowserOptions
{
if (options == null)
{
options = SettingsFile.GetValueDictionaryOrEmpty<object>($"{DriverSettingsPath}.{nameof(options)}");
options = SettingsFile.GetValueDictionaryOrEmpty<object>($"{DriverSettingsPath}.{nameof(options)}");
if (options.Any())
{
AqualityServices.LocalizedLogger.Debug("loc.browser.options",
args: string.Join(",", options.Select(opt => $"{Environment.NewLine}{opt.Key}: {opt.Value}")));
}
}
}

return options;
}
}
Expand All @@ -107,13 +107,13 @@ protected IReadOnlyList<string> BrowserStartArguments
}
}

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

protected abstract BrowserName BrowserName { get; }

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

protected void SetPageLoadStratergy(DriverOptions options)
protected void SetPageLoadStrategy(DriverOptions options)
{
options.PageLoadStrategy = PageLoadStrategy;
}
Expand All @@ -126,10 +126,10 @@ protected void SetCapabilities(DriverOptions options, Action<string, object> add
{
var defaultAddCapabilityMethod = addCapabilityMethod ?? options.AddAdditionalCapability;
defaultAddCapabilityMethod(capability.Key, capability.Value);
}
}
catch (ArgumentException exception)
{
if(exception.Message.StartsWith("There is already an option"))
if (exception.Message.StartsWith("There is already an option"))
{
SetKnownProperty(options, capability, exception);
}
Expand All @@ -150,7 +150,7 @@ private void SetKnownProperty(DriverOptions options, KeyValuePair<string, object
else
{
SetOptionByPropertyName(options, capability, exception);
}
}
}

protected void SetOptionsByPropertyNames(DriverOptions options)
Expand All @@ -168,7 +168,41 @@ private void SetOptionByPropertyName(DriverOptions options, KeyValuePair<string,
.GetProperties()
.FirstOrDefault(property => IsPropertyNameMatchOption(property.Name, option.Key) && property.CanWrite)
?? throw exception;
optionProperty.SetValue(options, option.Value);
var propertyType = optionProperty.PropertyType;
var valueToSet = IsEnumValue(propertyType, option.Value)
? ParseEnumValue(propertyType, option.Value)
: option.Value;
optionProperty.SetValue(options, valueToSet);
}

private object ParseEnumValue(Type propertyType, object optionValue)
{
return optionValue is string
? Enum.Parse(propertyType, optionValue.ToString(), ignoreCase: true)
: Enum.ToObject(propertyType, Convert.ChangeType(optionValue, Enum.GetUnderlyingType(propertyType)));
}

private bool IsEnumValue(Type propertyType, object optionValue)
{
var valueAsString = optionValue.ToString();
if (!propertyType.IsEnum || string.IsNullOrEmpty(valueAsString))
{
return false;
}
var normalizedValue = char.ToUpper(valueAsString[0]) +
(valueAsString.Length > 1 ? valueAsString.Substring(1) : string.Empty);
return propertyType.IsEnumDefined(normalizedValue)
|| propertyType.IsEnumDefined(valueAsString)
|| (IsValueOfIntegralNumericType(optionValue)
&& propertyType.IsEnumDefined(Convert.ChangeType(optionValue, Enum.GetUnderlyingType(propertyType))));
}

private bool IsValueOfIntegralNumericType(object value)
{
return value is byte || value is sbyte
|| value is ushort || value is short
|| value is uint || value is int
|| value is ulong || value is long;
}

private bool IsPropertyNameMatchOption(string propertyName, string optionKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override DriverOptions DriverOptions
SetEdgeChromiumPrefs(options);
SetCapabilities(options, (name, value) => options.AddAdditionalCapability(name, value, isGlobalCapability: true));
SetEdgeChromiumArguments(options);
SetPageLoadStratergy(options);
SetPageLoadStrategy(options);
return options;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override DriverOptions DriverOptions
var options = new EdgeOptions();
SetCapabilities(options);
SetOptionsByPropertyNames(options);
SetPageLoadStratergy(options);
SetPageLoadStrategy(options);
return options;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override DriverOptions DriverOptions
SetCapabilities(options, (name, value) => options.AddAdditionalCapability(name, value, isGlobalCapability: true));
SetFirefoxPrefs(options);
SetFirefoxArguments(options);
SetPageLoadStratergy(options);
SetPageLoadStrategy(options);
return options;
}
}
Expand All @@ -54,21 +54,21 @@ private void SetFirefoxPrefs(FirefoxOptions options)
{
options.SetPreference(option.Key, DownloadDir);
}
else if (value is bool)
else if (value is bool boolean)
{
options.SetPreference(option.Key, (bool) value);
options.SetPreference(option.Key, boolean);
}
else if (value is int)
else if (value is int @int)
{
options.SetPreference(option.Key, (int) value);
options.SetPreference(option.Key, @int);
}
else if (value is long)
else if (value is long @long)
{
options.SetPreference(option.Key, (long)value);
options.SetPreference(option.Key, @long);
}
else if (value is float)
else if (value is float @float)
{
options.SetPreference(option.Key, (float) value);
options.SetPreference(option.Key, @float);
}
else if (value is string)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using Aquality.Selenium.Browsers;
using Aquality.Selenium.Core.Configurations;
using Aquality.Selenium.Core.Utilities;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Remote;
Expand All @@ -29,9 +28,7 @@ public InternetExplorerSettings(ISettingsFile settingsFile) : base(settingsFile)
{ "ignoreProtectedModeSettings", (options, value) => ((InternetExplorerOptions) options).IntroduceInstabilityByIgnoringProtectedModeSettings = (bool) value },
{ "ignoreZoomSetting", (options, value) => ((InternetExplorerOptions) options).IgnoreZoomLevel = (bool) value },
{ CapabilityType.HasNativeEvents, (options, value) => ((InternetExplorerOptions) options).EnableNativeEvents = (bool) value },
{ CapabilityType.UnexpectedAlertBehavior, (options, value) => ((InternetExplorerOptions) options).UnhandledPromptBehavior = value.ToEnum<UnhandledPromptBehavior>() },
{ "ie.browserCommandLineSwitches", (options, value) => ((InternetExplorerOptions) options).BrowserCommandLineArguments = value.ToString() },
{ "elementScrollBehavior", (options, value) => ((InternetExplorerOptions) options).ElementScrollBehavior = value.ToEnum<InternetExplorerElementScrollBehavior>() }
{ "ie.browserCommandLineSwitches", (options, value) => ((InternetExplorerOptions) options).BrowserCommandLineArguments = value.ToString() }
};

public override DriverOptions DriverOptions
Expand All @@ -41,7 +38,7 @@ public override DriverOptions DriverOptions
var options = new InternetExplorerOptions();
SetCapabilities(options);
SetOptionsByPropertyNames(options);
SetPageLoadStratergy(options);
SetPageLoadStrategy(options);
options.BrowserCommandLineArguments = string.Join(" ", BrowserStartArguments);
return options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override DriverOptions DriverOptions
var options = new SafariOptions();
SetCapabilities(options);
SetOptionsByPropertyNames(options);
SetPageLoadStratergy(options);
SetPageLoadStrategy(options);
return options;
}
}
Expand Down
10 changes: 7 additions & 3 deletions Aquality.Selenium/src/Aquality.Selenium/Resources/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"driver": "INFO",
"server": "OFF",
"browser": "FINE"
}
},
"unhandledPromptBehavior": "default"
},
"options": {
"intl.accept_languages": "en",
Expand All @@ -29,7 +30,8 @@
"firefox": {
"webDriverVersion": "Latest",
"capabilities": {
"enableVNC": true
"enableVNC": true,
"unhandledPromptBehavior": "default"
},
"options": {
"intl.accept_languages": "en",
Expand All @@ -52,7 +54,8 @@
"webDriverVersion": "Latest",
"systemArchitecture": "X32",
"capabilities": {
"ignoreProtectedModeSettings": true
"ignoreProtectedModeSettings": true,
"unhandledPromptBehavior": "default"
},
"options": {
},
Expand All @@ -61,6 +64,7 @@
"edge": {
"systemArchitecture": "X32",
"capabilities": {
"unhandledPromptBehavior": "default"
},
"options": {
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ public void Should_BePossibleTo_AcceptConfirmationAlert()
Assert.AreEqual("You clicked: Ok", alertsForm.ResultLabel.GetText());
}

[Test]
public void Should_BePossibleTo_AcceptConfirmationAlert_InWaitFor()
{
alertsForm.JsConfirmButton.Click();
AqualityServices.ConditionalWait.WaitFor(driver =>
{
try
{
AqualityServices.Logger.Debug($"Current url: {driver.Url}");
return false;
}
catch (UnhandledAlertException e)
{
AqualityServices.Logger.Debug($"Alert appeared: {e.Message}");
AqualityServices.Browser.HandleAlert(AlertAction.Accept);
return true;
}

});
Assert.AreEqual("You clicked: Ok", alertsForm.ResultLabel.GetText());
}

[Test]
public void Should_BePossibleTo_DeclineConfirmationAlert()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"chrome": {
"webDriverVersion": "MatchingBrowser",
"capabilities": {
"enableVNC": true
"enableVNC": true,
"unhandledPromptBehavior": "ignore"
},
"options": {
"intl.accept_languages": "en",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"chrome": {
"webDriverVersion": "MatchingBrowser",
"capabilities": {
"enableVNC": true
"enableVNC": true,
"unhandledPromptBehavior": "ignore"
},
"options": {
"intl.accept_languages": "en",
Expand All @@ -24,7 +25,8 @@
"webDriverVersion": "Latest",
"systemArchitecture": "X64",
"capabilities": {
"enableVNC": true
"enableVNC": true,
"unhandledPromptBehavior": "ignore"
},
"options": {
"intl.accept_languages": "en",
Expand All @@ -49,12 +51,14 @@
"capabilities": {
"ignoreProtectedModeSettings": true,
"requireWindowFocus": false,
"elementScrollBehavior": 2
"elementScrollBehavior": 2,
"unhandledPromptBehavior": "ignore"
}
},
"edge": {
"systemArchitecture": "X64",
"capabilities": {
"unhandledPromptBehavior": "ignore"
},
"options": {
},
Expand All @@ -63,7 +67,8 @@
"edgechromium": {
"webDriverVersion": "82.0.458.0",
"capabilities": {
"enableVNC": true
"enableVNC": true,
"unhandledPromptBehavior": "ignore"
},
"options": {
"intl.accept_languages": "en",
Expand Down

0 comments on commit e734cd5

Please sign in to comment.