From 8eadea71e290168466c3c7384e31604fa5e82cb0 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sun, 5 Oct 2025 15:05:54 +0300 Subject: [PATCH 1/2] SetDownloadBehavior --- .../webdriver/BiDi/Browser/BrowserModule.cs | 21 +++++++++ .../Browser/SetDownloadBehaviorCommand.cs | 43 +++++++++++++++++++ .../Json/BiDiJsonSerializerContext.cs | 1 + 3 files changed, 65 insertions(+) create mode 100644 dotnet/src/webdriver/BiDi/Browser/SetDownloadBehaviorCommand.cs diff --git a/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs b/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs index ef4860e1ad997..cb23e20ad06e8 100644 --- a/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs +++ b/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs @@ -52,4 +52,25 @@ public async Task GetClientWindowsAsync(GetClientWindows { return await Broker.ExecuteCommandAsync(new(), options).ConfigureAwait(false); } + + public async Task SetDownloadBehaviorAllowedAsync(string destinationFolder, SetDownloadBehaviorOptions? options = null) + { + var @params = new SetDownloadBehaviorParameters(new DownloadBehaviorAllowed(destinationFolder), options?.UserContexts); + + return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options).ConfigureAwait(false); + } + + public async Task SetDownloadBehaviorAllowedAsync(SetDownloadBehaviorOptions? options = null) + { + var @params = new SetDownloadBehaviorParameters(null, options?.UserContexts); + + return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options).ConfigureAwait(false); + } + + public async Task SetDownloadBehaviorDeniedAsync(SetDownloadBehaviorOptions? options = null) + { + var @params = new SetDownloadBehaviorParameters(new DownloadBehaviorDenied(), options?.UserContexts); + + return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options).ConfigureAwait(false); + } } diff --git a/dotnet/src/webdriver/BiDi/Browser/SetDownloadBehaviorCommand.cs b/dotnet/src/webdriver/BiDi/Browser/SetDownloadBehaviorCommand.cs new file mode 100644 index 0000000000000..3158777f1caf2 --- /dev/null +++ b/dotnet/src/webdriver/BiDi/Browser/SetDownloadBehaviorCommand.cs @@ -0,0 +1,43 @@ +// +// 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. +// + +using OpenQA.Selenium.BiDi.Communication; +using System.Collections.Generic; +using System.Text.Json.Serialization; + +namespace OpenQA.Selenium.BiDi.Browser; + +internal sealed class SetDownloadBehaviorCommand(SetDownloadBehaviorParameters @params) + : Command(@params, "browser.setDownloadBehavior"); + +internal sealed record SetDownloadBehaviorParameters(DownloadBehavior? DownloadBehavior, IEnumerable? UserContexts) : Parameters; + +[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")] +[JsonDerivedType(typeof(DownloadBehaviorAllowed), "allowed")] +[JsonDerivedType(typeof(DownloadBehaviorDenied), "denied")] +internal abstract record DownloadBehavior; + +internal sealed record DownloadBehaviorAllowed(string DestinationFolder) : DownloadBehavior; + +internal sealed record DownloadBehaviorDenied : DownloadBehavior; + +public sealed class SetDownloadBehaviorOptions : CommandOptions +{ + public IEnumerable? UserContexts { get; set; } +} diff --git a/dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs b/dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs index 4a74468315f4c..b55199bb44d59 100644 --- a/dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs +++ b/dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs @@ -86,6 +86,7 @@ namespace OpenQA.Selenium.BiDi.Communication.Json; [JsonSerializable(typeof(Browser.RemoveUserContextCommand))] [JsonSerializable(typeof(Browser.GetClientWindowsCommand))] [JsonSerializable(typeof(Browser.GetClientWindowsResult))] +[JsonSerializable(typeof(Browser.SetDownloadBehaviorCommand))] [JsonSerializable(typeof(Browser.UserContextInfo))] [JsonSerializable(typeof(IReadOnlyList))] [JsonSerializable(typeof(IReadOnlyList))] From bf6375f8f5f00dfee8605528abd7cd7b7180d35f Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sun, 5 Oct 2025 15:15:08 +0300 Subject: [PATCH 2/2] Tests --- .../Browser/SetDownloadBehaviorCommand.cs | 2 +- .../test/common/BiDi/Browser/BrowserTest.cs | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/BiDi/Browser/SetDownloadBehaviorCommand.cs b/dotnet/src/webdriver/BiDi/Browser/SetDownloadBehaviorCommand.cs index 3158777f1caf2..9660dc5b90326 100644 --- a/dotnet/src/webdriver/BiDi/Browser/SetDownloadBehaviorCommand.cs +++ b/dotnet/src/webdriver/BiDi/Browser/SetDownloadBehaviorCommand.cs @@ -26,7 +26,7 @@ namespace OpenQA.Selenium.BiDi.Browser; internal sealed class SetDownloadBehaviorCommand(SetDownloadBehaviorParameters @params) : Command(@params, "browser.setDownloadBehavior"); -internal sealed record SetDownloadBehaviorParameters(DownloadBehavior? DownloadBehavior, IEnumerable? UserContexts) : Parameters; +internal sealed record SetDownloadBehaviorParameters([property: JsonIgnore(Condition = JsonIgnoreCondition.Never)] DownloadBehavior? DownloadBehavior, IEnumerable? UserContexts) : Parameters; [JsonPolymorphic(TypeDiscriminatorPropertyName = "type")] [JsonDerivedType(typeof(DownloadBehaviorAllowed), "allowed")] diff --git a/dotnet/test/common/BiDi/Browser/BrowserTest.cs b/dotnet/test/common/BiDi/Browser/BrowserTest.cs index c00792c310268..c1c230b993f63 100644 --- a/dotnet/test/common/BiDi/Browser/BrowserTest.cs +++ b/dotnet/test/common/BiDi/Browser/BrowserTest.cs @@ -69,4 +69,37 @@ public async Task CanGetClientWindows() Assert.That(clientWindows, Has.Count.GreaterThanOrEqualTo(1)); Assert.That(clientWindows[0].ClientWindow, Is.Not.Null); } + + [Test] + [IgnoreBrowser(Selenium.Browser.Chrome, "Not supported yet?")] + [IgnoreBrowser(Selenium.Browser.Edge, "Not supported yet?")] + [IgnoreBrowser(Selenium.Browser.Firefox, "Not supported yet?")] + public async Task CanSetDownloadBehaviorAllowed() + { + var result = await bidi.Browser.SetDownloadBehaviorAllowedAsync("/my/path"); + + Assert.That(result, Is.Not.Null); + } + + [Test] + [IgnoreBrowser(Selenium.Browser.Chrome, "Not supported yet?")] + [IgnoreBrowser(Selenium.Browser.Edge, "Not supported yet?")] + [IgnoreBrowser(Selenium.Browser.Firefox, "Not supported yet?")] + public async Task CanSetDownloadBehaviorAllowedDefault() + { + var result = await bidi.Browser.SetDownloadBehaviorAllowedAsync(); + + Assert.That(result, Is.Not.Null); + } + + [Test] + [IgnoreBrowser(Selenium.Browser.Chrome, "Not supported yet?")] + [IgnoreBrowser(Selenium.Browser.Edge, "Not supported yet?")] + [IgnoreBrowser(Selenium.Browser.Firefox, "Not supported yet?")] + public async Task CanSetDownloadBehaviorDenied() + { + var result = await bidi.Browser.SetDownloadBehaviorDeniedAsync(); + + Assert.That(result, Is.Not.Null); + } }