diff --git a/dotnet/src/webdriver/DriverOptions.cs b/dotnet/src/webdriver/DriverOptions.cs index 483ceb03130a4..555a78d7a3a51 100644 --- a/dotnet/src/webdriver/DriverOptions.cs +++ b/dotnet/src/webdriver/DriverOptions.cs @@ -26,44 +26,6 @@ namespace OpenQA.Selenium; -/// -/// Specifies the behavior of handling unexpected alerts in the IE driver. -/// -public enum UnhandledPromptBehavior -{ - /// - /// Indicates the behavior is not set. - /// - Default, - - /// - /// Ignore unexpected alerts, such that the user must handle them. - /// - Ignore, - - /// - /// Accept unexpected alerts. - /// - Accept, - - /// - /// Dismiss unexpected alerts. - /// - Dismiss, - - /// - /// Accepts unexpected alerts and notifies the user that the alert has - /// been accepted by throwing an - /// - AcceptAndNotify, - - /// - /// Dismisses unexpected alerts and notifies the user that the alert has - /// been dismissed by throwing an - /// - DismissAndNotify -} - /// /// Specifies the behavior of waiting for page loads in the driver. /// @@ -164,7 +126,7 @@ protected DriverOptions() /// Gets or sets the value for describing how unexpected alerts are to be handled in the browser. /// Defaults to . /// - public UnhandledPromptBehavior UnhandledPromptBehavior { get; set; } = UnhandledPromptBehavior.Default; + public UnhandledPromptBehaviorOption? UnhandledPromptBehavior { get; set; } /// /// Gets or sets the value for describing how the browser is to wait for pages to load in the browser. @@ -303,7 +265,7 @@ public virtual DriverOptionsMergeResult GetMergeResult(DriverOptions other) return result; } - if (this.UnhandledPromptBehavior != UnhandledPromptBehavior.Default && other.UnhandledPromptBehavior != UnhandledPromptBehavior.Default) + if (this.UnhandledPromptBehavior is not null && other.UnhandledPromptBehavior is not null) { result.IsMergeConflict = true; result.MergeConflictOptionName = "UnhandledPromptBehavior"; @@ -508,29 +470,55 @@ protected IWritableCapabilities GenerateDesiredCapabilities(bool isSpecification capabilities.SetCapability(CapabilityType.PageLoadStrategy, pageLoadStrategySetting); } - if (this.UnhandledPromptBehavior != UnhandledPromptBehavior.Default) + static string UnhandledPromptBehaviorToString(UnhandledPromptBehavior behavior) => behavior switch + { + Selenium.UnhandledPromptBehavior.Ignore => "ignore", + Selenium.UnhandledPromptBehavior.Accept => "accept", + Selenium.UnhandledPromptBehavior.Dismiss => "dismiss", + Selenium.UnhandledPromptBehavior.AcceptAndNotify => "accept and notify", + Selenium.UnhandledPromptBehavior.DismissAndNotify => "dismiss and notify", + _ => throw new ArgumentOutOfRangeException(nameof(behavior), $"UnhandledPromptBehavior value '{behavior}' is not recognized."), + }; + + if (this.UnhandledPromptBehavior is UnhandledPromptBehaviorSingleOption singleOption && singleOption.Value != Selenium.UnhandledPromptBehavior.Default) + { + var stringValue = UnhandledPromptBehaviorToString(singleOption.Value); + + capabilities.SetCapability(CapabilityType.UnhandledPromptBehavior, stringValue); + } + else if (this.UnhandledPromptBehavior is UnhandledPromptBehaviorMultiOption multiOption) { - string unhandledPropmtBehaviorSetting = "ignore"; - switch (this.UnhandledPromptBehavior) + Dictionary multiOptionDictionary = []; + + if (multiOption.Alert is not Selenium.UnhandledPromptBehavior.Default) { - case UnhandledPromptBehavior.Accept: - unhandledPropmtBehaviorSetting = "accept"; - break; + multiOptionDictionary["alert"] = UnhandledPromptBehaviorToString(multiOption.Alert); + } - case UnhandledPromptBehavior.Dismiss: - unhandledPropmtBehaviorSetting = "dismiss"; - break; + if (multiOption.Confirm is not Selenium.UnhandledPromptBehavior.Default) + { + multiOptionDictionary["confirm"] = UnhandledPromptBehaviorToString(multiOption.Confirm); + } - case UnhandledPromptBehavior.AcceptAndNotify: - unhandledPropmtBehaviorSetting = "accept and notify"; - break; + if (multiOption.Prompt is not Selenium.UnhandledPromptBehavior.Default) + { + multiOptionDictionary["prompt"] = UnhandledPromptBehaviorToString(multiOption.Prompt); + } - case UnhandledPromptBehavior.DismissAndNotify: - unhandledPropmtBehaviorSetting = "dismiss and notify"; - break; + if (multiOption.BeforeUnload is not Selenium.UnhandledPromptBehavior.Default) + { + multiOptionDictionary["beforeUnload"] = UnhandledPromptBehaviorToString(multiOption.BeforeUnload); } - capabilities.SetCapability(CapabilityType.UnhandledPromptBehavior, unhandledPropmtBehaviorSetting); + if (multiOption.Default is not Selenium.UnhandledPromptBehavior.Default) + { + multiOptionDictionary["default"] = UnhandledPromptBehaviorToString(multiOption.Default); + } + + if (multiOptionDictionary.Count != 0) + { + capabilities.SetCapability(CapabilityType.UnhandledPromptBehavior, multiOptionDictionary); + } } if (this.Proxy != null) diff --git a/dotnet/src/webdriver/UnhandledPromptBehaviorOption.cs b/dotnet/src/webdriver/UnhandledPromptBehaviorOption.cs new file mode 100644 index 0000000000000..7e0dc35988243 --- /dev/null +++ b/dotnet/src/webdriver/UnhandledPromptBehaviorOption.cs @@ -0,0 +1,150 @@ +// +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +namespace OpenQA.Selenium; + +/// +/// Represents a configuration option that determines how unhandled prompts are managed during automated browser +/// interactions. +/// +/// +/// Use this type to specify whether a single unhandled prompt behavior or multiple behaviors should be applied. +/// The static methods provide convenient ways to create either a single-behavior or multi-behavior option. +/// This abstraction is typically used in scenarios where browser automation frameworks need to control the handling of +/// unexpected dialogs or prompts. +/// +/// Available options: +/// +/// - Wraps a single value applied to all prompt types. Create via the implicit conversion from or by calling . +/// - Allows configuring per-prompt behaviors (Alert, Confirm, Prompt, BeforeUnload, Default). Create via . +/// +/// +/// +public abstract record UnhandledPromptBehaviorOption +{ + /// + /// Converts a value of type to an instance. + /// + /// The value to convert. + public static implicit operator UnhandledPromptBehaviorOption(UnhandledPromptBehavior value) + => Single(value); + + /// + /// Creates an representing a single value. + /// + /// The to apply for all prompt types. + /// An wrapping the provided behavior. + public static UnhandledPromptBehaviorOption Single(UnhandledPromptBehavior value) + => new UnhandledPromptBehaviorSingleOption(value); + + /// + /// Creates an allowing individual values per prompt type. + /// + /// An with per-prompt configurable behaviors. + public static UnhandledPromptBehaviorOption Multi() + => new UnhandledPromptBehaviorMultiOption(); +} + +/// +/// Represents an option that specifies a single unhandled prompt behavior to use when interacting with browser dialogs. +/// +/// The unhandled prompt behavior to apply. Specifies how unexpected browser prompts are handled during automation. +public sealed record UnhandledPromptBehaviorSingleOption(UnhandledPromptBehavior Value) : UnhandledPromptBehaviorOption; + +/// +/// Represents a set of options that specify how unhandled browser prompts are handled for different prompt types. +/// +/// Use this class to configure distinct behaviors for alert, confirm, prompt, and beforeunload dialogs +/// encountered during browser automation. Each property allows you to control the response to a specific type of +/// unhandled prompt, enabling fine-grained handling beyond a single global setting. +public sealed record UnhandledPromptBehaviorMultiOption : UnhandledPromptBehaviorOption +{ + /// + /// Gets or sets the behavior to use when an unexpected alert is encountered during automation. + /// + public UnhandledPromptBehavior Alert { get; set; } = UnhandledPromptBehavior.Default; + + /// + /// Gets or sets the behavior to use when a confirmation prompt is encountered. + /// + /// Set this property to specify how the system should respond to confirmation dialogs, such as + /// JavaScript confirm boxes, during automated operations. The default value is , which applies the standard handling defined by the + /// environment. + public UnhandledPromptBehavior Confirm { get; set; } = UnhandledPromptBehavior.Default; + + /// + /// Gets or sets the behavior to use when an unexpected prompt is encountered during automation. + /// + /// Set this property to control how the system responds to unhandled prompts, such as alerts or + /// confirmation dialogs, that appear unexpectedly. The default behavior is determined by the value of + /// . + public UnhandledPromptBehavior Prompt { get; set; } = UnhandledPromptBehavior.Default; + + /// + /// Gets or sets the behavior to use when an unexpected beforeunload dialog is encountered. + /// + /// Use this property to specify how the application should respond to beforeunload dialogs that + /// appear unexpectedly during automated browser interactions. This setting determines whether such dialogs are + /// automatically accepted, dismissed, or cause an error. + public UnhandledPromptBehavior BeforeUnload { get; set; } = UnhandledPromptBehavior.Default; + + /// + /// Gets or sets the default behavior to use when an unexpected browser prompt is encountered. + /// + public UnhandledPromptBehavior Default { get; set; } = UnhandledPromptBehavior.Default; +} + +/// +/// Specifies the behavior of handling unexpected alerts in the IE driver. +/// +public enum UnhandledPromptBehavior +{ + /// + /// Indicates the behavior is not set. + /// + Default, + + /// + /// Ignore unexpected alerts, such that the user must handle them. + /// + Ignore, + + /// + /// Accept unexpected alerts. + /// + Accept, + + /// + /// Dismiss unexpected alerts. + /// + Dismiss, + + /// + /// Accepts unexpected alerts and notifies the user that the alert has + /// been accepted by throwing an + /// + AcceptAndNotify, + + /// + /// Dismisses unexpected alerts and notifies the user that the alert has + /// been dismissed by throwing an + /// + DismissAndNotify +}