From e734cd549a1880ea51ed89c63efaab1dfc4f6a78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alaksiej=20Miale=C5=A1ka?= Date: Wed, 18 Nov 2020 16:21:51 +0200 Subject: [PATCH] Rework known capabilities setting logic for Enum types to handle "unhandledPromptBehavior" capability (#196) resolves #195 --- .../WebDriverSettings/ChromeSettings.cs | 2 +- .../WebDriverSettings/DriverSettings.cs | 54 +++++++++++++++---- .../WebDriverSettings/EdgeChromiumSettings.cs | 2 +- .../WebDriverSettings/EdgeSettings.cs | 2 +- .../WebDriverSettings/FirefoxSettings.cs | 18 +++---- .../InternetExplorerSettings.cs | 7 +-- .../WebDriverSettings/SafariSettings.cs | 2 +- .../Aquality.Selenium/Resources/settings.json | 10 ++-- .../Integration/AlertTests.cs | 22 ++++++++ .../Resources/settings.azure.json | 3 +- .../Resources/settings.json | 13 +++-- 11 files changed, 99 insertions(+), 36 deletions(-) diff --git a/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/ChromeSettings.cs b/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/ChromeSettings.cs index d3df132e..155a4592 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/ChromeSettings.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/ChromeSettings.cs @@ -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; } } diff --git a/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/DriverSettings.cs b/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/DriverSettings.cs index fd5c8c73..eb292b83 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/DriverSettings.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/DriverSettings.cs @@ -67,7 +67,7 @@ protected IReadOnlyDictionary BrowserCapabilities args: string.Join(",", capabilities.Select(cap => $"{Environment.NewLine}{cap.Key}: {cap.Value}"))); } } - + return capabilities; } } @@ -78,14 +78,14 @@ protected IReadOnlyDictionary BrowserOptions { if (options == null) { - options = SettingsFile.GetValueDictionaryOrEmpty($"{DriverSettingsPath}.{nameof(options)}"); + options = SettingsFile.GetValueDictionaryOrEmpty($"{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; } } @@ -107,13 +107,13 @@ protected IReadOnlyList BrowserStartArguments } } - private string DriverSettingsPath => $".driverSettings.{BrowserName.ToString().ToLowerInvariant()}"; + private string DriverSettingsPath => $".driverSettings.{BrowserName.ToString().ToLowerInvariant()}"; protected abstract BrowserName BrowserName { get; } protected virtual IDictionary> KnownCapabilitySetters => new Dictionary>(); - protected void SetPageLoadStratergy(DriverOptions options) + protected void SetPageLoadStrategy(DriverOptions options) { options.PageLoadStrategy = PageLoadStrategy; } @@ -126,10 +126,10 @@ protected void SetCapabilities(DriverOptions options, Action 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); } @@ -150,7 +150,7 @@ private void SetKnownProperty(DriverOptions options, KeyValuePair 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) diff --git a/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/EdgeChromiumSettings.cs b/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/EdgeChromiumSettings.cs index 8a1f8764..1c6b16a3 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/EdgeChromiumSettings.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/EdgeChromiumSettings.cs @@ -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; } } diff --git a/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/EdgeSettings.cs b/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/EdgeSettings.cs index dd364166..f9b9ce17 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/EdgeSettings.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/EdgeSettings.cs @@ -28,7 +28,7 @@ public override DriverOptions DriverOptions var options = new EdgeOptions(); SetCapabilities(options); SetOptionsByPropertyNames(options); - SetPageLoadStratergy(options); + SetPageLoadStrategy(options); return options; } } diff --git a/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/FirefoxSettings.cs b/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/FirefoxSettings.cs index 1568a377..657abf0a 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/FirefoxSettings.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/FirefoxSettings.cs @@ -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; } } @@ -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) { diff --git a/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/InternetExplorerSettings.cs b/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/InternetExplorerSettings.cs index 9a873ef2..aa4d7699 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/InternetExplorerSettings.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/InternetExplorerSettings.cs @@ -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; @@ -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() }, - { "ie.browserCommandLineSwitches", (options, value) => ((InternetExplorerOptions) options).BrowserCommandLineArguments = value.ToString() }, - { "elementScrollBehavior", (options, value) => ((InternetExplorerOptions) options).ElementScrollBehavior = value.ToEnum() } + { "ie.browserCommandLineSwitches", (options, value) => ((InternetExplorerOptions) options).BrowserCommandLineArguments = value.ToString() } }; public override DriverOptions DriverOptions @@ -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; } diff --git a/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/SafariSettings.cs b/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/SafariSettings.cs index 469f2622..fbacf576 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/SafariSettings.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Configurations/WebDriverSettings/SafariSettings.cs @@ -27,7 +27,7 @@ public override DriverOptions DriverOptions var options = new SafariOptions(); SetCapabilities(options); SetOptionsByPropertyNames(options); - SetPageLoadStratergy(options); + SetPageLoadStrategy(options); return options; } } diff --git a/Aquality.Selenium/src/Aquality.Selenium/Resources/settings.json b/Aquality.Selenium/src/Aquality.Selenium/Resources/settings.json index 4d07a6b7..ecfba590 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Resources/settings.json +++ b/Aquality.Selenium/src/Aquality.Selenium/Resources/settings.json @@ -13,7 +13,8 @@ "driver": "INFO", "server": "OFF", "browser": "FINE" - } + }, + "unhandledPromptBehavior": "default" }, "options": { "intl.accept_languages": "en", @@ -29,7 +30,8 @@ "firefox": { "webDriverVersion": "Latest", "capabilities": { - "enableVNC": true + "enableVNC": true, + "unhandledPromptBehavior": "default" }, "options": { "intl.accept_languages": "en", @@ -52,7 +54,8 @@ "webDriverVersion": "Latest", "systemArchitecture": "X32", "capabilities": { - "ignoreProtectedModeSettings": true + "ignoreProtectedModeSettings": true, + "unhandledPromptBehavior": "default" }, "options": { }, @@ -61,6 +64,7 @@ "edge": { "systemArchitecture": "X32", "capabilities": { + "unhandledPromptBehavior": "default" }, "options": { }, diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/AlertTests.cs b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/AlertTests.cs index f74f4e18..7eb3ffa2 100644 --- a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/AlertTests.cs +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/AlertTests.cs @@ -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() { diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/settings.azure.json b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/settings.azure.json index 81abfbb2..35d8b343 100644 --- a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/settings.azure.json +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/settings.azure.json @@ -8,7 +8,8 @@ "chrome": { "webDriverVersion": "MatchingBrowser", "capabilities": { - "enableVNC": true + "enableVNC": true, + "unhandledPromptBehavior": "ignore" }, "options": { "intl.accept_languages": "en", diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/settings.json b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/settings.json index ea41032b..22043435 100644 --- a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/settings.json +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/settings.json @@ -8,7 +8,8 @@ "chrome": { "webDriverVersion": "MatchingBrowser", "capabilities": { - "enableVNC": true + "enableVNC": true, + "unhandledPromptBehavior": "ignore" }, "options": { "intl.accept_languages": "en", @@ -24,7 +25,8 @@ "webDriverVersion": "Latest", "systemArchitecture": "X64", "capabilities": { - "enableVNC": true + "enableVNC": true, + "unhandledPromptBehavior": "ignore" }, "options": { "intl.accept_languages": "en", @@ -49,12 +51,14 @@ "capabilities": { "ignoreProtectedModeSettings": true, "requireWindowFocus": false, - "elementScrollBehavior": 2 + "elementScrollBehavior": 2, + "unhandledPromptBehavior": "ignore" } }, "edge": { "systemArchitecture": "X64", "capabilities": { + "unhandledPromptBehavior": "ignore" }, "options": { }, @@ -63,7 +67,8 @@ "edgechromium": { "webDriverVersion": "82.0.458.0", "capabilities": { - "enableVNC": true + "enableVNC": true, + "unhandledPromptBehavior": "ignore" }, "options": { "intl.accept_languages": "en",