-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
[dotnet] Support UnhandledPromptBehavior option as string and map (breaking change)
#16557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RenderMichael
merged 30 commits into
SeleniumHQ:trunk
from
nvborisenko:dotnet-prompt-behavior-dict
Apr 25, 2026
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
cb357bf
First implementation
nvborisenko 5a2cdd8
Don't change tests to emphasize backward compatibility
nvborisenko 5c53619
Static factory?
nvborisenko fc98a78
Throw exception if enum is not branched
nvborisenko 75fc654
Revert "Throw exception if enum is not branched"
nvborisenko 50275d2
Throw exception if enum is not branched
nvborisenko 9accc2b
Non nullable enum values
nvborisenko 1acc7de
Merge branch 'trunk' into dotnet-prompt-behavior-dict
nvborisenko 727a5b3
Move to new UnhandledPromptBehaviorOption file
nvborisenko 96cd5cd
Docs
nvborisenko 83b5218
Docs for possible types
nvborisenko f5e5845
Clear as options
nvborisenko c83c67c
Merge branch 'trunk' into dotnet-prompt-behavior-dict
nvborisenko baf2939
Fix static factory
nvborisenko bfd55e9
Consider the property in MergeResult
nvborisenko c94cc5d
Refactor serialization
nvborisenko 4f60927
Fully delegate to ToCapabilities
nvborisenko aeb98ae
Test
nvborisenko 53efd4c
Merge remote-tracking branch 'upstream/trunk' into dotnet-prompt-beha…
nvborisenko 1e53b6b
UserPromptHandler - spec defined type
nvborisenko 3225204
Support File handler
nvborisenko b984c9f
Clean
nvborisenko 832d645
Nullable instead of default, obsoleting Default
nvborisenko 8186fb7
Init
nvborisenko 9894ffc
Docs UnhandledPromptBehavior
nvborisenko 0f1836f
Update UserPromptHandler.cs
nvborisenko f15b022
Thows.Nothing in assertion
nvborisenko b46633c
Move test to proper fixture
nvborisenko aa0ccef
Serialize empty
nvborisenko 89eeffa
Merge branch 'trunk' into dotnet-prompt-behavior-dict
nvborisenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| // <copyright file="UserPromptHandler.cs" company="Selenium Committers"> | ||
| // 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. | ||
| // </copyright> | ||
|
|
||
| namespace OpenQA.Selenium; | ||
|
|
||
| /// <summary> | ||
| /// Represents a WebDriver session's user prompt handler, which defines how unhandled browser prompts | ||
| /// (alerts, confirms, prompts, beforeunload dialogs, file selection dialogs) are managed during automation. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This corresponds to the W3C WebDriver <c>unhandledPromptBehavior</c> capability, which may be expressed | ||
| /// either as a single string applied to all prompt types, or as a per-prompt-type map. | ||
| /// <para> | ||
| /// Available variants: | ||
| /// <list type="bullet"> | ||
| /// <item><description><see cref="Uniform"/> - Wraps a single <see cref="UnhandledPromptBehavior"/> value applied to all prompt types. Create via the implicit conversion from <see cref="UnhandledPromptBehavior"/> or by constructing <see cref="Uniform"/> directly.</description></item> | ||
| /// <item><description><see cref="PerPromptType"/> - Allows configuring per-prompt behaviors (Alert, Confirm, Prompt, BeforeUnload, File, Default).</description></item> | ||
| /// </list> | ||
| /// </para> | ||
| /// </remarks> | ||
| public abstract record UserPromptHandler | ||
| { | ||
| private UserPromptHandler() { } | ||
|
nvborisenko marked this conversation as resolved.
nvborisenko marked this conversation as resolved.
|
||
|
|
||
| /// <summary> | ||
| /// Converts a nullable <see cref="UnhandledPromptBehavior"/> to a <see cref="UserPromptHandler"/> instance, | ||
| /// or <see langword="null"/> when <paramref name="value"/> is <see langword="null"/>. | ||
| /// </summary> | ||
| /// <param name="value">The <see cref="UnhandledPromptBehavior"/> value to convert.</param> | ||
| public static implicit operator UserPromptHandler?(UnhandledPromptBehavior? value) | ||
| => value is { } v ? new Uniform(v) : null; | ||
|
nvborisenko marked this conversation as resolved.
|
||
|
|
||
| internal abstract object? ToCapabilities(); | ||
|
|
||
| private static string ConvertBehaviorToString(UnhandledPromptBehavior behavior) => | ||
| behavior switch | ||
| { | ||
| UnhandledPromptBehavior.Ignore => "ignore", | ||
| UnhandledPromptBehavior.Accept => "accept", | ||
| UnhandledPromptBehavior.Dismiss => "dismiss", | ||
| UnhandledPromptBehavior.AcceptAndNotify => "accept and notify", | ||
| UnhandledPromptBehavior.DismissAndNotify => "dismiss and notify", | ||
| _ => throw new InvalidOperationException($"UnhandledPromptBehavior value '{behavior}' is not recognized."), | ||
| }; | ||
|
|
||
| /// <summary> | ||
| /// Represents a user prompt handler that applies a single <see cref="UnhandledPromptBehavior"/> value | ||
| /// as the fallback default for all prompt types. | ||
| /// </summary> | ||
| /// <param name="Value">The unhandled prompt behavior to apply. Specifies how unexpected browser prompts are handled during automation.</param> | ||
| public sealed record Uniform(UnhandledPromptBehavior Value) : UserPromptHandler | ||
| { | ||
| internal override object? ToCapabilities() | ||
| { | ||
| #pragma warning disable CS0618 // UnhandledPromptBehavior.Default is obsolete | ||
| if (Value == UnhandledPromptBehavior.Default) | ||
| { | ||
| return null; | ||
| } | ||
| #pragma warning restore CS0618 | ||
|
|
||
| return ConvertBehaviorToString(Value); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Represents a user prompt handler that specifies distinct <see cref="UnhandledPromptBehavior"/> values | ||
| /// for individual prompt types (alert, confirm, prompt, beforeunload, file), with a fallback default. | ||
| /// </summary> | ||
| /// <remarks>Use this variant to configure distinct behaviors for alert, confirm, prompt, beforeunload, and file | ||
| /// selection 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.</remarks> | ||
| public sealed record PerPromptType : UserPromptHandler | ||
| { | ||
| /// <summary> | ||
| /// Gets the behavior to use when an unexpected alert is encountered during automation, | ||
| /// or <see langword="null"/> to leave it unset. | ||
| /// </summary> | ||
| public UnhandledPromptBehavior? Alert { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the behavior to use when a confirmation prompt is encountered, | ||
| /// or <see langword="null"/> to leave it unset. | ||
| /// </summary> | ||
| public UnhandledPromptBehavior? Confirm { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the behavior to use when an unexpected prompt is encountered during automation, | ||
| /// or <see langword="null"/> to leave it unset. | ||
| /// </summary> | ||
| public UnhandledPromptBehavior? Prompt { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the behavior to use when an unexpected beforeunload dialog is encountered, | ||
| /// or <see langword="null"/> to leave it unset. | ||
| /// </summary> | ||
| public UnhandledPromptBehavior? BeforeUnload { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the behavior to use when an unexpected file selection dialog is encountered, | ||
| /// or <see langword="null"/> to leave it unset. | ||
| /// </summary> | ||
| /// <remarks>The "file" prompt type is respected only in WebDriver BiDi sessions.</remarks> | ||
| public UnhandledPromptBehavior? File { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the fallback behavior to use when no specific handler is defined for a given prompt type, | ||
| /// or <see langword="null"/> to leave it unset. | ||
| /// </summary> | ||
| public UnhandledPromptBehavior? Default { get; init; } | ||
|
|
||
| internal override object? ToCapabilities() | ||
| { | ||
| Dictionary<string, string> capabilities = []; | ||
| AddIfSet(capabilities, "alert", Alert); | ||
| AddIfSet(capabilities, "confirm", Confirm); | ||
| AddIfSet(capabilities, "prompt", Prompt); | ||
| AddIfSet(capabilities, "beforeUnload", BeforeUnload); | ||
| AddIfSet(capabilities, "file", File); | ||
| AddIfSet(capabilities, "default", Default); | ||
| return capabilities; | ||
|
nvborisenko marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private static void AddIfSet(Dictionary<string, string> capabilities, string key, UnhandledPromptBehavior? value) | ||
| { | ||
| #pragma warning disable CS0618 // UnhandledPromptBehavior.Default is obsolete | ||
| if (value is { } v && v != UnhandledPromptBehavior.Default) | ||
| { | ||
| capabilities[key] = ConvertBehaviorToString(v); | ||
| } | ||
| #pragma warning restore CS0618 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Specifies how a WebDriver session handles an unhandled user prompt. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Corresponds to the handler values defined for the W3C WebDriver | ||
| /// <c>unhandledPromptBehavior</c> capability: | ||
| /// <c>dismiss</c>, <c>accept</c>, <c>dismiss and notify</c>, <c>accept and notify</c>, and <c>ignore</c>. | ||
| /// When no handler is configured, the spec's implicit default is <see cref="DismissAndNotify"/>. | ||
| /// </remarks> | ||
| public enum UnhandledPromptBehavior | ||
| { | ||
| /// <summary> | ||
| /// Sentinel value meaning "behavior not set". Not part of the W3C WebDriver spec. | ||
| /// </summary> | ||
| [Obsolete("Use a nullable UnhandledPromptBehavior? and pass null to leave the behavior unset. This member will be removed in v4.46.")] | ||
| Default, | ||
|
|
||
| /// <summary> | ||
| /// Ignore unexpected alerts, such that the user must handle them. | ||
| /// </summary> | ||
| Ignore, | ||
|
|
||
| /// <summary> | ||
| /// Accept unexpected alerts. | ||
| /// </summary> | ||
| Accept, | ||
|
|
||
| /// <summary> | ||
| /// Dismiss unexpected alerts. | ||
| /// </summary> | ||
| Dismiss, | ||
|
|
||
| /// <summary> | ||
| /// Accepts unexpected alerts and notifies the user that the alert has | ||
| /// been accepted by throwing an <see cref="UnhandledAlertException"/> | ||
| /// </summary> | ||
| AcceptAndNotify, | ||
|
|
||
| /// <summary> | ||
| /// Dismisses unexpected alerts and notifies the user that the alert has | ||
| /// been dismissed by throwing an <see cref="UnhandledAlertException"/> | ||
| /// </summary> | ||
| DismissAndNotify | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -555,5 +555,4 @@ private Func<bool> WindowHandleCountToBe(int count) | |
| return driver.WindowHandles.Count == count; | ||
| }; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.