Skip to content

Commit 878ea2f

Browse files
committed
Tests
1 parent f72d056 commit 878ea2f

File tree

5 files changed

+119
-6
lines changed

5 files changed

+119
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
// </copyright>
1919

2020
using System;
21-
using System.Text.Json.Serialization;
2221

2322
namespace OpenQA.Selenium.BiDi.BrowsingContext;
2423

25-
[JsonPolymorphic(TypeDiscriminatorPropertyName = "status")]
26-
[JsonDerivedType(typeof(DownloadCanceledEventArgs), "canceled")]
27-
[JsonDerivedType(typeof(DownloadCompleteEventArgs), "complete")]
24+
// https://github.com/dotnet/runtime/issues/72604
25+
//[JsonPolymorphic(TypeDiscriminatorPropertyName = "status")]
26+
//[JsonDerivedType(typeof(DownloadCanceledEventArgs), "canceled")]
27+
//[JsonDerivedType(typeof(DownloadCompleteEventArgs), "complete")]
2828
public abstract record DownloadEndEventArgs(BiDi BiDi, BrowsingContext Context)
2929
: BrowsingContextEventArgs(BiDi, Context);
3030

31-
public abstract record DownloadCanceledEventArgs(BiDi BiDi, BrowsingContext Context, Navigation? Navigation, DateTimeOffset Timestamp, string Url)
31+
public sealed record DownloadCanceledEventArgs(BiDi BiDi, BrowsingContext Context, Navigation? Navigation, DateTimeOffset Timestamp, string Url)
3232
: DownloadEndEventArgs(BiDi, Context), IBaseNavigationInfo;
3333

34-
public abstract record DownloadCompleteEventArgs(BiDi BiDi, string? Filepath, BrowsingContext Context, Navigation? Navigation, DateTimeOffset Timestamp, string Url)
34+
public sealed record DownloadCompleteEventArgs(BiDi BiDi, string? Filepath, BrowsingContext Context, Navigation? Navigation, DateTimeOffset Timestamp, string Url)
3535
: DownloadEndEventArgs(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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
114114
[JsonSerializable(typeof(BrowsingContext.BrowsingContextInfo))]
115115
[JsonSerializable(typeof(BrowsingContext.DownloadWillBeginEventArgs))]
116116
[JsonSerializable(typeof(BrowsingContext.DownloadEndEventArgs))]
117+
[JsonSerializable(typeof(BrowsingContext.DownloadCanceledEventArgs))]
118+
[JsonSerializable(typeof(BrowsingContext.DownloadCompleteEventArgs))]
117119
[JsonSerializable(typeof(BrowsingContext.HistoryUpdatedEventArgs))]
118120
[JsonSerializable(typeof(BrowsingContext.NavigationInfo))]
119121
[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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
public async Task CanListenDownloadWillBeginEvent()
30+
{
31+
await context.NavigateAsync(UrlBuilder.WhereIs("downloads/download.html"), new() { Wait = ReadinessState.Complete });
32+
33+
TaskCompletionSource<DownloadWillBeginEventArgs> tcs = new();
34+
35+
await using var subscription = await context.OnDownloadWillBeginAsync(tcs.SetResult);
36+
37+
driver.FindElement(By.Id("file-1")).Click();
38+
39+
var eventArgs = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5));
40+
41+
Assert.That(eventArgs, Is.Not.Null);
42+
Assert.That(eventArgs.Context, Is.EqualTo(context));
43+
Assert.That(eventArgs.Url, Does.EndWith("downloads/file_1.txt"));
44+
Assert.That(eventArgs.SuggestedFilename, Is.EqualTo("file_1.txt"));
45+
}
46+
47+
[Test]
48+
public async Task CanListenDownloadEndEvent()
49+
{
50+
await context.NavigateAsync(UrlBuilder.WhereIs("downloads/download.html"), new() { Wait = ReadinessState.Complete });
51+
52+
TaskCompletionSource<DownloadEndEventArgs> tcs = new();
53+
54+
await using var subscription = await context.OnDownloadEndAsync(tcs.SetResult);
55+
56+
driver.FindElement(By.Id("file-1")).Click();
57+
58+
var eventArgs = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5));
59+
60+
Assert.That(eventArgs, Is.Not.Null);
61+
Assert.That(eventArgs.Context, Is.EqualTo(context));
62+
Assert.That(eventArgs, Is.TypeOf<DownloadCompleteEventArgs>());
63+
Assert.That(((DownloadCompleteEventArgs)eventArgs).Filepath, Is.Not.Empty);
64+
}
65+
}

0 commit comments

Comments
 (0)