Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,26 @@ public Task<Subscription> OnLoadAsync(Func<NavigationInfo, Task> handler, Subscr
return BiDi.BrowsingContext.OnLoadAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
}

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

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

public Task<Subscription> OnDownloadEndAsync(Action<DownloadEndEventArgs> handler, SubscriptionOptions? options = null)
{
return BiDi.BrowsingContext.OnDownloadEndAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
}

public Task<Subscription> OnDownloadEndAsync(Func<DownloadEndEventArgs, Task> handler, SubscriptionOptions? options = null)
{
return BiDi.BrowsingContext.OnDownloadEndAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
}

public Task<Subscription> OnNavigationAbortedAsync(Action<NavigationInfo> handler, SubscriptionOptions? options = null)
{
return BiDi.BrowsingContext.OnNavigationAbortedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
Expand Down
14 changes: 12 additions & 2 deletions dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,26 @@ public async Task<Subscription> OnLoadAsync(Action<NavigationInfo> handler, Brow
return await Broker.SubscribeAsync("browsingContext.load", handler, options).ConfigureAwait(false);
}

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

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

public async Task<Subscription> OnDownloadEndAsync(Func<DownloadEndEventArgs, Task> handler, BrowsingContextsSubscriptionOptions? options = null)
{
return await Broker.SubscribeAsync("browsingContext.downloadEnd", handler, options).ConfigureAwait(false);
}

public async Task<Subscription> OnDownloadEndAsync(Action<DownloadEndEventArgs> handler, BrowsingContextsSubscriptionOptions? options = null)
{
return await Broker.SubscribeAsync("browsingContext.downloadEnd", handler, options).ConfigureAwait(false);
}

public async Task<Subscription> OnNavigationAbortedAsync(Func<NavigationInfo, Task> handler, BrowsingContextsSubscriptionOptions? options = null)
{
return await Broker.SubscribeAsync("browsingContext.navigationAborted", handler, options).ConfigureAwait(false);
Expand Down
35 changes: 35 additions & 0 deletions dotnet/src/webdriver/BiDi/BrowsingContext/DownloadEndEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// <copyright file="DownloadEndEventArgs.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>

using System;

namespace OpenQA.Selenium.BiDi.BrowsingContext;

// https://github.com/dotnet/runtime/issues/72604
//[JsonPolymorphic(TypeDiscriminatorPropertyName = "status")]
//[JsonDerivedType(typeof(DownloadCanceledEventArgs), "canceled")]
//[JsonDerivedType(typeof(DownloadCompleteEventArgs), "complete")]
public abstract record DownloadEndEventArgs(BiDi BiDi, BrowsingContext Context)
: BrowsingContextEventArgs(BiDi, Context);

public sealed record DownloadCanceledEventArgs(BiDi BiDi, BrowsingContext Context, Navigation? Navigation, DateTimeOffset Timestamp, string Url)
: DownloadEndEventArgs(BiDi, Context), IBaseNavigationInfo;

public sealed record DownloadCompleteEventArgs(BiDi BiDi, string? Filepath, BrowsingContext Context, Navigation? Navigation, DateTimeOffset Timestamp, string Url)
: DownloadEndEventArgs(BiDi, Context), IBaseNavigationInfo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// <copyright file="DownloadWillBeginEventArgs.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>

using System;

namespace OpenQA.Selenium.BiDi.BrowsingContext;

public sealed record DownloadWillBeginEventArgs(BiDi BiDi, string SuggestedFilename, BrowsingContext Context, Navigation? Navigation, DateTimeOffset Timestamp, string Url)
: BrowsingContextEventArgs(BiDi, Context), IBaseNavigationInfo;
1 change: 1 addition & 0 deletions dotnet/src/webdriver/BiDi/Communication/Broker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ internal Broker(BiDi bidi, Uri url)
new Json.Converters.Polymorphic.RemoteValueConverter(),
new Json.Converters.Polymorphic.RealmInfoConverter(),
new Json.Converters.Polymorphic.LogEntryConverter(),
new Json.Converters.Polymorphic.DownloadEndEventArgsConverter(),
//

// Enumerable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
[JsonSerializable(typeof(BrowsingContext.TraverseHistoryResult))]

[JsonSerializable(typeof(BrowsingContext.BrowsingContextInfo))]
[JsonSerializable(typeof(BrowsingContext.DownloadWillBeginEventArgs))]
[JsonSerializable(typeof(BrowsingContext.DownloadEndEventArgs))]
[JsonSerializable(typeof(BrowsingContext.DownloadCanceledEventArgs))]
[JsonSerializable(typeof(BrowsingContext.DownloadCompleteEventArgs))]
[JsonSerializable(typeof(BrowsingContext.HistoryUpdatedEventArgs))]
[JsonSerializable(typeof(BrowsingContext.NavigationInfo))]
[JsonSerializable(typeof(BrowsingContext.UserPromptOpenedEventArgs))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// <copyright file="DownloadEndEventArgsConverter.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>

using OpenQA.Selenium.BiDi.BrowsingContext;
using OpenQA.Selenium.BiDi.Communication.Json.Internal;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace OpenQA.Selenium.BiDi.Communication.Json.Converters.Polymorphic;

// https://github.com/dotnet/runtime/issues/72604
internal class DownloadEndEventArgsConverter : JsonConverter<DownloadEndEventArgs>
{
public override DownloadEndEventArgs? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.GetDiscriminator("status") switch
{
"canceled" => JsonSerializer.Deserialize(ref reader, options.GetTypeInfo<DownloadCanceledEventArgs>()),
"complete" => JsonSerializer.Deserialize(ref reader, options.GetTypeInfo<DownloadCompleteEventArgs>()),
_ => null,
};
}

public override void Write(Utf8JsonWriter writer, DownloadEndEventArgs value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// <copyright file="BrowsingContextEventsTest.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>

using NUnit.Framework;
using System;
using System.Threading.Tasks;

namespace OpenQA.Selenium.BiDi.BrowsingContext;

class BrowsingContextEventsTest : BiDiTestFixture
{
[Test]
[IgnoreBrowser(Selenium.Browser.Firefox, "Not supported yet?")]
public async Task CanListenDownloadWillBeginEvent()
{
await context.NavigateAsync(UrlBuilder.WhereIs("downloads/download.html"), new() { Wait = ReadinessState.Complete });

TaskCompletionSource<DownloadWillBeginEventArgs> tcs = new();

await using var subscription = await context.OnDownloadWillBeginAsync(tcs.SetResult);

driver.FindElement(By.Id("file-1")).Click();

var eventArgs = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5));

Assert.That(eventArgs, Is.Not.Null);
Assert.That(eventArgs.Context, Is.EqualTo(context));
Assert.That(eventArgs.Url, Does.EndWith("downloads/file_1.txt"));
Assert.That(eventArgs.SuggestedFilename, Is.EqualTo("file_1.txt"));
}

[Test]
[IgnoreBrowser(Selenium.Browser.Firefox, "Not supported yet?")]
public async Task CanListenDownloadEndEvent()
{
await context.NavigateAsync(UrlBuilder.WhereIs("downloads/download.html"), new() { Wait = ReadinessState.Complete });

TaskCompletionSource<DownloadEndEventArgs> tcs = new();

await using var subscription = await context.OnDownloadEndAsync(tcs.SetResult);

driver.FindElement(By.Id("file-1")).Click();

var eventArgs = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5));

Assert.That(eventArgs, Is.Not.Null);
Assert.That(eventArgs.Context, Is.EqualTo(context));
Assert.That(eventArgs, Is.TypeOf<DownloadCompleteEventArgs>());
Assert.That(((DownloadCompleteEventArgs)eventArgs).Filepath, Is.Not.Empty);
}
}