Skip to content

Commit 3baf4bc

Browse files
authored
[dotnet] [bidi] Implement browsing context download events (#16382)
1 parent 2a8261d commit 3baf4bc

File tree

8 files changed

+201
-4
lines changed

8 files changed

+201
-4
lines changed

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,26 @@ public Task<Subscription> OnLoadAsync(Func<NavigationInfo, Task> handler, Subscr
167167
return BiDi.BrowsingContext.OnLoadAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
168168
}
169169

170-
public Task<Subscription> OnDownloadWillBeginAsync(Action<NavigationInfo> handler, SubscriptionOptions? options = null)
170+
public Task<Subscription> OnDownloadWillBeginAsync(Action<DownloadWillBeginEventArgs> handler, SubscriptionOptions? options = null)
171171
{
172172
return BiDi.BrowsingContext.OnDownloadWillBeginAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
173173
}
174174

175-
public Task<Subscription> OnDownloadWillBeginAsync(Func<NavigationInfo, Task> handler, SubscriptionOptions? options = null)
175+
public Task<Subscription> OnDownloadWillBeginAsync(Func<DownloadWillBeginEventArgs, Task> handler, SubscriptionOptions? options = null)
176176
{
177177
return BiDi.BrowsingContext.OnDownloadWillBeginAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
178178
}
179179

180+
public Task<Subscription> OnDownloadEndAsync(Action<DownloadEndEventArgs> handler, SubscriptionOptions? options = null)
181+
{
182+
return BiDi.BrowsingContext.OnDownloadEndAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
183+
}
184+
185+
public Task<Subscription> OnDownloadEndAsync(Func<DownloadEndEventArgs, Task> handler, SubscriptionOptions? options = null)
186+
{
187+
return BiDi.BrowsingContext.OnDownloadEndAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
188+
}
189+
180190
public Task<Subscription> OnNavigationAbortedAsync(Action<NavigationInfo> handler, SubscriptionOptions? options = null)
181191
{
182192
return BiDi.BrowsingContext.OnNavigationAbortedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,26 @@ public async Task<Subscription> OnLoadAsync(Action<NavigationInfo> handler, Brow
161161
return await Broker.SubscribeAsync("browsingContext.load", handler, options).ConfigureAwait(false);
162162
}
163163

164-
public async Task<Subscription> OnDownloadWillBeginAsync(Func<NavigationInfo, Task> handler, BrowsingContextsSubscriptionOptions? options = null)
164+
public async Task<Subscription> OnDownloadWillBeginAsync(Func<DownloadWillBeginEventArgs, Task> handler, BrowsingContextsSubscriptionOptions? options = null)
165165
{
166166
return await Broker.SubscribeAsync("browsingContext.downloadWillBegin", handler, options).ConfigureAwait(false);
167167
}
168168

169-
public async Task<Subscription> OnDownloadWillBeginAsync(Action<NavigationInfo> handler, BrowsingContextsSubscriptionOptions? options = null)
169+
public async Task<Subscription> OnDownloadWillBeginAsync(Action<DownloadWillBeginEventArgs> handler, BrowsingContextsSubscriptionOptions? options = null)
170170
{
171171
return await Broker.SubscribeAsync("browsingContext.downloadWillBegin", handler, options).ConfigureAwait(false);
172172
}
173173

174+
public async Task<Subscription> OnDownloadEndAsync(Func<DownloadEndEventArgs, Task> handler, BrowsingContextsSubscriptionOptions? options = null)
175+
{
176+
return await Broker.SubscribeAsync("browsingContext.downloadEnd", handler, options).ConfigureAwait(false);
177+
}
178+
179+
public async Task<Subscription> OnDownloadEndAsync(Action<DownloadEndEventArgs> handler, BrowsingContextsSubscriptionOptions? options = null)
180+
{
181+
return await Broker.SubscribeAsync("browsingContext.downloadEnd", handler, options).ConfigureAwait(false);
182+
}
183+
174184
public async Task<Subscription> OnNavigationAbortedAsync(Func<NavigationInfo, Task> handler, BrowsingContextsSubscriptionOptions? options = null)
175185
{
176186
return await Broker.SubscribeAsync("browsingContext.navigationAborted", handler, options).ConfigureAwait(false);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// <copyright file="DownloadEndEventArgs.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 System;
21+
22+
namespace OpenQA.Selenium.BiDi.BrowsingContext;
23+
24+
// https://github.com/dotnet/runtime/issues/72604
25+
//[JsonPolymorphic(TypeDiscriminatorPropertyName = "status")]
26+
//[JsonDerivedType(typeof(DownloadCanceledEventArgs), "canceled")]
27+
//[JsonDerivedType(typeof(DownloadCompleteEventArgs), "complete")]
28+
public abstract record DownloadEndEventArgs(BiDi BiDi, BrowsingContext Context)
29+
: BrowsingContextEventArgs(BiDi, Context);
30+
31+
public sealed record DownloadCanceledEventArgs(BiDi BiDi, BrowsingContext Context, Navigation? Navigation, DateTimeOffset Timestamp, string Url)
32+
: DownloadEndEventArgs(BiDi, Context), IBaseNavigationInfo;
33+
34+
public sealed record DownloadCompleteEventArgs(BiDi BiDi, string? Filepath, BrowsingContext Context, Navigation? Navigation, DateTimeOffset Timestamp, string Url)
35+
: DownloadEndEventArgs(BiDi, Context), IBaseNavigationInfo;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// <copyright file="DownloadWillBeginEventArgs.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 System;
21+
22+
namespace OpenQA.Selenium.BiDi.BrowsingContext;
23+
24+
public sealed record DownloadWillBeginEventArgs(BiDi BiDi, string SuggestedFilename, BrowsingContext Context, Navigation? Navigation, DateTimeOffset Timestamp, string Url)
25+
: BrowsingContextEventArgs(BiDi, Context), IBaseNavigationInfo;

dotnet/src/webdriver/BiDi/Communication/Broker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ internal Broker(BiDi bidi, Uri url)
9797
new Json.Converters.Polymorphic.RemoteValueConverter(),
9898
new Json.Converters.Polymorphic.RealmInfoConverter(),
9999
new Json.Converters.Polymorphic.LogEntryConverter(),
100+
new Json.Converters.Polymorphic.DownloadEndEventArgsConverter(),
100101
//
101102

102103
// Enumerable

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
112112
[JsonSerializable(typeof(BrowsingContext.TraverseHistoryResult))]
113113

114114
[JsonSerializable(typeof(BrowsingContext.BrowsingContextInfo))]
115+
[JsonSerializable(typeof(BrowsingContext.DownloadWillBeginEventArgs))]
116+
[JsonSerializable(typeof(BrowsingContext.DownloadEndEventArgs))]
117+
[JsonSerializable(typeof(BrowsingContext.DownloadCanceledEventArgs))]
118+
[JsonSerializable(typeof(BrowsingContext.DownloadCompleteEventArgs))]
115119
[JsonSerializable(typeof(BrowsingContext.HistoryUpdatedEventArgs))]
116120
[JsonSerializable(typeof(BrowsingContext.NavigationInfo))]
117121
[JsonSerializable(typeof(BrowsingContext.UserPromptOpenedEventArgs))]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// <copyright file="DownloadEndEventArgsConverter.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.BrowsingContext;
21+
using OpenQA.Selenium.BiDi.Communication.Json.Internal;
22+
using System;
23+
using System.Text.Json;
24+
using System.Text.Json.Serialization;
25+
26+
namespace OpenQA.Selenium.BiDi.Communication.Json.Converters.Polymorphic;
27+
28+
// https://github.com/dotnet/runtime/issues/72604
29+
internal class DownloadEndEventArgsConverter : JsonConverter<DownloadEndEventArgs>
30+
{
31+
public override DownloadEndEventArgs? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
32+
{
33+
return reader.GetDiscriminator("status") switch
34+
{
35+
"canceled" => JsonSerializer.Deserialize(ref reader, options.GetTypeInfo<DownloadCanceledEventArgs>()),
36+
"complete" => JsonSerializer.Deserialize(ref reader, options.GetTypeInfo<DownloadCompleteEventArgs>()),
37+
_ => null,
38+
};
39+
}
40+
41+
public override void Write(Utf8JsonWriter writer, DownloadEndEventArgs value, JsonSerializerOptions options)
42+
{
43+
throw new NotImplementedException();
44+
}
45+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// <copyright file="BrowsingContextEventsTest.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 NUnit.Framework;
21+
using System;
22+
using System.Threading.Tasks;
23+
24+
namespace OpenQA.Selenium.BiDi.BrowsingContext;
25+
26+
class BrowsingContextEventsTest : BiDiTestFixture
27+
{
28+
[Test]
29+
[IgnoreBrowser(Selenium.Browser.Firefox, "Not supported yet?")]
30+
public async Task CanListenDownloadWillBeginEvent()
31+
{
32+
await context.NavigateAsync(UrlBuilder.WhereIs("downloads/download.html"), new() { Wait = ReadinessState.Complete });
33+
34+
TaskCompletionSource<DownloadWillBeginEventArgs> tcs = new();
35+
36+
await using var subscription = await context.OnDownloadWillBeginAsync(tcs.SetResult);
37+
38+
driver.FindElement(By.Id("file-1")).Click();
39+
40+
var eventArgs = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5));
41+
42+
Assert.That(eventArgs, Is.Not.Null);
43+
Assert.That(eventArgs.Context, Is.EqualTo(context));
44+
Assert.That(eventArgs.Url, Does.EndWith("downloads/file_1.txt"));
45+
Assert.That(eventArgs.SuggestedFilename, Is.EqualTo("file_1.txt"));
46+
}
47+
48+
[Test]
49+
[IgnoreBrowser(Selenium.Browser.Firefox, "Not supported yet?")]
50+
public async Task CanListenDownloadEndEvent()
51+
{
52+
await context.NavigateAsync(UrlBuilder.WhereIs("downloads/download.html"), new() { Wait = ReadinessState.Complete });
53+
54+
TaskCompletionSource<DownloadEndEventArgs> tcs = new();
55+
56+
await using var subscription = await context.OnDownloadEndAsync(tcs.SetResult);
57+
58+
driver.FindElement(By.Id("file-1")).Click();
59+
60+
var eventArgs = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5));
61+
62+
Assert.That(eventArgs, Is.Not.Null);
63+
Assert.That(eventArgs.Context, Is.EqualTo(context));
64+
Assert.That(eventArgs, Is.TypeOf<DownloadCompleteEventArgs>());
65+
Assert.That(((DownloadCompleteEventArgs)eventArgs).Filepath, Is.Not.Empty);
66+
}
67+
}

0 commit comments

Comments
 (0)