Skip to content

Add test for UnhandledPromptBehavior in ChromeOptions#2611

Closed
kimtg wants to merge 2 commits into
SeleniumHQ:trunkfrom
kimtg:patch-1
Closed

Add test for UnhandledPromptBehavior in ChromeOptions#2611
kimtg wants to merge 2 commits into
SeleniumHQ:trunkfrom
kimtg:patch-1

Conversation

@kimtg

@kimtg kimtg commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

Thanks for contributing to the Selenium site and documentation!
A PR well described will help maintainers to review and merge it quickly

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, and help reviewers by making them as simple and short as possible.

Description

Motivation and Context

Types of changes

  • Change to the site (I have double-checked the Netlify deployment, and my changes look good)
  • Code example added (and I also added the example to all translated languages)
  • Improved translation
  • Added new translation (and I also added a notice to each document missing translation)

Checklist

  • I have read the contributing document.
  • I have used hugo to render the site/docs locally and I am sure it works.

@netlify

netlify Bot commented Mar 30, 2026

Copy link
Copy Markdown

👷 Deploy request for selenium-dev pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit fdae63b

@CLAassistant

CLAassistant commented Mar 30, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ rpallavisharma
❌ kimtg
You have signed the CLA already but the status is still pending? Let us recheck it.

@qodo-code-review

Copy link
Copy Markdown
Contributor

Review Summary by Qodo

Add test for UnhandledPromptBehavior in ChromeOptions

🧪 Tests

Grey Divider

Walkthroughs

Description
• Add test method for UnhandledPromptBehavior option
• Demonstrates setting DismissAndNotify behavior in ChromeOptions
• Follows existing test pattern with driver initialization and cleanup
Diagram
flowchart LR
  A["OptionsTest.cs"] -- "adds new test method" --> B["SetUnhandledPromptBehavior"]
  B -- "sets UnhandledPromptBehavior" --> C["DismissAndNotify"]
  C -- "creates ChromeDriver" --> D["Navigate and cleanup"]
Loading

Grey Divider

File Changes

1. examples/dotnet/SeleniumDocs/Drivers/OptionsTest.cs 🧪 Tests +16/-1

Add UnhandledPromptBehavior test method

• Added new test method SetUnhandledPromptBehavior() with [TestMethod] attribute
• Sets chromeOptions.UnhandledPromptBehavior to UnhandledPromptBehavior.DismissAndNotify
• Initializes ChromeDriver with configured options and navigates to selenium.dev
• Includes proper try-finally block for driver cleanup
• Fixed missing newline at end of file

examples/dotnet/SeleniumDocs/Drivers/OptionsTest.cs


Grey Divider

Qodo Logo

@qodo-code-review

qodo-code-review Bot commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider


Remediation recommended

1. Prompt behavior untested 🐞 Bug ✓ Correctness
Description
SetUnhandledPromptBehavior sets ChromeOptions.UnhandledPromptBehavior but never triggers an
alert/prompt/confirm, so the option is not exercised and the test provides false confidence.
Regressions in unhandled prompt handling would still pass because the only action is a normal
navigation.
Code

examples/dotnet/SeleniumDocs/Drivers/OptionsTest.cs[R56-65]

+        [TestMethod]
+        public void SetUnhandledPromptBehavior()
+        {
+            var chromeOptions = new ChromeOptions();
+            chromeOptions.UnhandledPromptBehavior = UnhandledPromptBehavior.DismissAndNotify;
+            IWebDriver driver = new ChromeDriver(chromeOptions);
+            try
+            {
+                driver.Navigate().GoToUrl("https://selenium.dev");
+            }
Evidence
The added test sets UnhandledPromptBehavior and then only navigates to https://selenium.dev; there
is no code that opens a JavaScript dialog (alert/prompt/confirm) or executes a command while such a
dialog is present, so the configured behavior cannot affect the run.

examples/dotnet/SeleniumDocs/Drivers/OptionsTest.cs[56-70]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`SetUnhandledPromptBehavior` sets `UnhandledPromptBehavior` but never triggers an alert/prompt/confirm, so it does not actually test the configured behavior.

### Issue Context
`UnhandledPromptBehavior` only matters when a JavaScript dialog is open and a subsequent WebDriver command is executed. The test currently performs a normal navigation to an external page that does not open such a dialog.

### Fix Focus Areas
- examples/dotnet/SeleniumDocs/Drivers/OptionsTest.cs[56-70]

### Suggested change
Update the test to load a deterministic page that opens an alert (e.g., via a `data:` URL with `alert()` on load), then execute a follow-up WebDriver command and assert the expected outcome for `DismissAndNotify` (typically an exception is thrown while the alert is dismissed).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Prompt behavior untested 🐞 Bug ≡ Correctness
Description
SetUnhandledPromptBehavior sets ChromeOptions.UnhandledPromptBehavior but never triggers an
alert/prompt/confirm, so the option is not exercised and the test provides false confidence.
Regressions in unhandled prompt handling would still pass because the only action is a normal
navigation.
Code

examples/dotnet/SeleniumDocs/Drivers/OptionsTest.cs[R56-65]

+        [TestMethod]
+        public void SetUnhandledPromptBehavior()
+        {
+            var chromeOptions = new ChromeOptions();
+            chromeOptions.UnhandledPromptBehavior = UnhandledPromptBehavior.DismissAndNotify;
+            IWebDriver driver = new ChromeDriver(chromeOptions);
+            try
+            {
+                driver.Navigate().GoToUrl("https://selenium.dev");
+            }
Evidence
The added test sets UnhandledPromptBehavior and then only navigates to https://selenium.dev; there
is no code that opens a JavaScript dialog (alert/prompt/confirm) or executes a command while such a
dialog is present, so the configured behavior cannot affect the run.

examples/dotnet/SeleniumDocs/Drivers/OptionsTest.cs[56-70]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`SetUnhandledPromptBehavior` sets `UnhandledPromptBehavior` but never triggers an alert/prompt/confirm, so it does not actually test the configured behavior.

### Issue Context
`UnhandledPromptBehavior` only matters when a JavaScript dialog is open and a subsequent WebDriver command is executed. The test currently performs a normal navigation to an external page that does not open such a dialog.

### Fix Focus Areas
- examples/dotnet/SeleniumDocs/Drivers/OptionsTest.cs[56-70]

### Suggested change
Update the test to load a deterministic page that opens an alert (e.g., via a `data:` URL with `alert()` on load), then execute a follow-up WebDriver command and assert the expected outcome for `DismissAndNotify` (typically an exception is thrown while the alert is dismissed).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

@rpallavisharma

Copy link
Copy Markdown
Member

@kimtg tests are failing in some environments. will come back with update on this later.

@diemol

diemol commented Jul 11, 2026

Copy link
Copy Markdown
Member

@kimtg can you please sign the CLA?

@diemol

diemol commented Jul 18, 2026

Copy link
Copy Markdown
Member

Superseeded by #2724

@diemol diemol closed this Jul 18, 2026
diemol added a commit that referenced this pull request Jul 18, 2026
* Add C# example for unhandledPromptBehavior option

Adds the missing SetUnhandledPromptBehavior test to OptionsTest.cs and
wires the CSharp tab in the docs to it, replacing the placeholder badge.

Based on #2611 and #2612 by kimtg.

Co-authored-by: KIM Taegyoon <steloflute@gmail.com>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix: avoid Windows file-lock races in EdgeTest log tests

On Windows, msedgedriver can still hold its log file open for a moment
after driver.Quit(), so reading the log right after Quit() intermittently
threw IOException in LogsToFile, LogsLevel, ConfigureDriverLogs, and
DisableBuildCheck.

Mirrors the Python and Ruby examples, which read the log while the
service is still running instead of after quitting (see 4decac3
for the equivalent Python fix). Also guards the log file deletion in
Cleanup() against the same race, tolerating IOException like Python's
contextlib.suppress(OSError) and Ruby's FileUtils.rm_f.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix: retry reading Edge log file to survive Windows lock window

CI showed the log file is locked for the entire life of the Edge
driver service on Windows, not just briefly after Quit() -- moving
the read before Quit() (previous commit) failed immediately since
the driver process was still running. Reading after Quit() is
correct, but the OS still needs a moment to release the handle once
the process exits, so wrap the read in a short bounded retry instead.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix: null-safe driver.Quit() in EdgeTest cleanup

If driver creation throws before the field is assigned, Cleanup()'s
unconditional driver.Quit() throws NullReferenceException and masks
the real test failure. ChromeTest.cs already uses this driver?.Quit()
pattern in its own cleanup; apply it here too.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix: widen retry budget for Edge log file read on Windows

The previous 2s retry budget (10 x 200ms) wasn't enough -- CI still
failed with the same IOException after exhausting all attempts.
EdgeDriverService.LogPath is fed by DriverService's async
OutputDataReceived event handler (per the Selenium.WebDriver XML
docs), so the log FileStream can stay open for several seconds after
Quit() returns while the last buffered output drains. Widen the
budget to ~9.5s (20 x 500ms) to reliably outlast that tail.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix: narrow EdgeTest log retry to sharing/lock violations only

ReadLogLines was retrying on any IOException, so a genuine failure
(missing file, bad path, permissions) would silently eat up to ~10s
of sleeping before surfacing, slowing down and obscuring real
failures. Restrict the retry to the specific Win32 sharing/lock
violation HRESULTs the Windows file-lock race actually produces;
everything else propagates immediately.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* try: replace log-read retry with explicit service.Dispose()

Drop the retry loop and try forcing a real synchronous teardown
instead: EdgeDriverService.LogPath is fed by DriverService's async
OutputDataReceived handler, so driver.Quit() alone may return before
that stream is fully flushed and closed. Holding the service
reference and calling service.Dispose() explicitly should block
until the log file is actually released, letting us read it with a
plain File.ReadAllLines and no polling.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* revert: bring back narrowed retry, keep explicit service.Dispose()

The explicit-Dispose-only approach didn't work: CI showed the same
IOException immediately after driver.Quit() + service.Dispose(), so
DriverService's async OutputDataReceived teardown isn't fully
synchronous even through an explicit Dispose() call. Restore the
retry (narrowed to actual sharing/lock-violation HRESULTs, ~9.5s
budget), and keep the Dispose() call alongside it since it's still
correct resource cleanup and may shrink the typical wait.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* simplify: drop HResult filtering and explanatory comments in EdgeTest

This is a doc code example, not production code -- the narrowed
sharing/lock-violation HResult check added unnecessary verbosity, and
comments like "Close the Service log file before reading" get
confusing once the file is translated. Keep the retry and
service.Dispose(), drop the rest.

---------

Co-authored-by: KIM Taegyoon <steloflute@gmail.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants