Skip to content

Commit 59a27f1

Browse files
authored
[dotnet] [bidi] Support browser SetDownloadBehaviour command (#16383)
1 parent 3baf4bc commit 59a27f1

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,25 @@ public async Task<GetClientWindowsResult> GetClientWindowsAsync(GetClientWindows
5252
{
5353
return await Broker.ExecuteCommandAsync<GetClientWindowsCommand, GetClientWindowsResult>(new(), options).ConfigureAwait(false);
5454
}
55+
56+
public async Task<EmptyResult> SetDownloadBehaviorAllowedAsync(string destinationFolder, SetDownloadBehaviorOptions? options = null)
57+
{
58+
var @params = new SetDownloadBehaviorParameters(new DownloadBehaviorAllowed(destinationFolder), options?.UserContexts);
59+
60+
return await Broker.ExecuteCommandAsync<SetDownloadBehaviorCommand, EmptyResult>(new SetDownloadBehaviorCommand(@params), options).ConfigureAwait(false);
61+
}
62+
63+
public async Task<EmptyResult> SetDownloadBehaviorAllowedAsync(SetDownloadBehaviorOptions? options = null)
64+
{
65+
var @params = new SetDownloadBehaviorParameters(null, options?.UserContexts);
66+
67+
return await Broker.ExecuteCommandAsync<SetDownloadBehaviorCommand, EmptyResult>(new SetDownloadBehaviorCommand(@params), options).ConfigureAwait(false);
68+
}
69+
70+
public async Task<EmptyResult> SetDownloadBehaviorDeniedAsync(SetDownloadBehaviorOptions? options = null)
71+
{
72+
var @params = new SetDownloadBehaviorParameters(new DownloadBehaviorDenied(), options?.UserContexts);
73+
74+
return await Broker.ExecuteCommandAsync<SetDownloadBehaviorCommand, EmptyResult>(new SetDownloadBehaviorCommand(@params), options).ConfigureAwait(false);
75+
}
5576
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// <copyright file="SetDownloadBehaviorCommand.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
using OpenQA.Selenium.BiDi.Communication;
21+
using System.Collections.Generic;
22+
using System.Text.Json.Serialization;
23+
24+
namespace OpenQA.Selenium.BiDi.Browser;
25+
26+
internal sealed class SetDownloadBehaviorCommand(SetDownloadBehaviorParameters @params)
27+
: Command<SetDownloadBehaviorParameters, EmptyResult>(@params, "browser.setDownloadBehavior");
28+
29+
internal sealed record SetDownloadBehaviorParameters([property: JsonIgnore(Condition = JsonIgnoreCondition.Never)] DownloadBehavior? DownloadBehavior, IEnumerable<UserContext>? UserContexts) : Parameters;
30+
31+
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
32+
[JsonDerivedType(typeof(DownloadBehaviorAllowed), "allowed")]
33+
[JsonDerivedType(typeof(DownloadBehaviorDenied), "denied")]
34+
internal abstract record DownloadBehavior;
35+
36+
internal sealed record DownloadBehaviorAllowed(string DestinationFolder) : DownloadBehavior;
37+
38+
internal sealed record DownloadBehaviorDenied : DownloadBehavior;
39+
40+
public sealed class SetDownloadBehaviorOptions : CommandOptions
41+
{
42+
public IEnumerable<UserContext>? UserContexts { get; set; }
43+
}

dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
8686
[JsonSerializable(typeof(Browser.RemoveUserContextCommand))]
8787
[JsonSerializable(typeof(Browser.GetClientWindowsCommand))]
8888
[JsonSerializable(typeof(Browser.GetClientWindowsResult))]
89+
[JsonSerializable(typeof(Browser.SetDownloadBehaviorCommand))]
8990
[JsonSerializable(typeof(Browser.UserContextInfo))]
9091
[JsonSerializable(typeof(IReadOnlyList<Browser.UserContextInfo>))]
9192
[JsonSerializable(typeof(IReadOnlyList<Browser.ClientWindowInfo>))]

dotnet/test/common/BiDi/Browser/BrowserTest.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,37 @@ public async Task CanGetClientWindows()
6969
Assert.That(clientWindows, Has.Count.GreaterThanOrEqualTo(1));
7070
Assert.That(clientWindows[0].ClientWindow, Is.Not.Null);
7171
}
72+
73+
[Test]
74+
[IgnoreBrowser(Selenium.Browser.Chrome, "Not supported yet?")]
75+
[IgnoreBrowser(Selenium.Browser.Edge, "Not supported yet?")]
76+
[IgnoreBrowser(Selenium.Browser.Firefox, "Not supported yet?")]
77+
public async Task CanSetDownloadBehaviorAllowed()
78+
{
79+
var result = await bidi.Browser.SetDownloadBehaviorAllowedAsync("/my/path");
80+
81+
Assert.That(result, Is.Not.Null);
82+
}
83+
84+
[Test]
85+
[IgnoreBrowser(Selenium.Browser.Chrome, "Not supported yet?")]
86+
[IgnoreBrowser(Selenium.Browser.Edge, "Not supported yet?")]
87+
[IgnoreBrowser(Selenium.Browser.Firefox, "Not supported yet?")]
88+
public async Task CanSetDownloadBehaviorAllowedDefault()
89+
{
90+
var result = await bidi.Browser.SetDownloadBehaviorAllowedAsync();
91+
92+
Assert.That(result, Is.Not.Null);
93+
}
94+
95+
[Test]
96+
[IgnoreBrowser(Selenium.Browser.Chrome, "Not supported yet?")]
97+
[IgnoreBrowser(Selenium.Browser.Edge, "Not supported yet?")]
98+
[IgnoreBrowser(Selenium.Browser.Firefox, "Not supported yet?")]
99+
public async Task CanSetDownloadBehaviorDenied()
100+
{
101+
var result = await bidi.Browser.SetDownloadBehaviorDeniedAsync();
102+
103+
Assert.That(result, Is.Not.Null);
104+
}
72105
}

0 commit comments

Comments
 (0)