Skip to content

Commit 763bb9d

Browse files
Merge branch 'trunk' into driver-factory-test
2 parents 41eb277 + 8f424e2 commit 763bb9d

36 files changed

+212
-381
lines changed

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ internal Broker(BiDi bidi, ITransport transport)
8383
new DateTimeOffsetConverter(),
8484
new PrintPageRangeConverter(),
8585
new InputOriginConverter(),
86+
new SubscriptionConverter(),
8687
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase),
8788

8889
// https://github.com/dotnet/runtime/issues/72604
@@ -223,23 +224,23 @@ public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Act
223224

224225
if (options is BrowsingContextsSubscriptionOptions browsingContextsOptions)
225226
{
226-
await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = browsingContextsOptions.Contexts }).ConfigureAwait(false);
227+
var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = browsingContextsOptions.Contexts }).ConfigureAwait(false);
227228

228229
var eventHandler = new SyncEventHandler<TEventArgs>(eventName, action, browsingContextsOptions?.Contexts);
229230

230231
handlers.Add(eventHandler);
231232

232-
return new Subscription(this, eventHandler);
233+
return new Subscription(subscribeResult.Subscription, this, eventHandler);
233234
}
234235
else
235236
{
236-
await _bidi.SessionModule.SubscribeAsync([eventName]).ConfigureAwait(false);
237+
var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName]).ConfigureAwait(false);
237238

238239
var eventHandler = new SyncEventHandler<TEventArgs>(eventName, action);
239240

240241
handlers.Add(eventHandler);
241242

242-
return new Subscription(this, eventHandler);
243+
return new Subscription(subscribeResult.Subscription, this, eventHandler);
243244
}
244245
}
245246

@@ -250,44 +251,51 @@ public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Fun
250251

251252
if (options is BrowsingContextsSubscriptionOptions browsingContextsOptions)
252253
{
253-
await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = browsingContextsOptions.Contexts }).ConfigureAwait(false);
254+
var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = browsingContextsOptions.Contexts }).ConfigureAwait(false);
254255

255256
var eventHandler = new AsyncEventHandler<TEventArgs>(eventName, func, browsingContextsOptions.Contexts);
256257

257258
handlers.Add(eventHandler);
258259

259-
return new Subscription(this, eventHandler);
260+
return new Subscription(subscribeResult.Subscription, this, eventHandler);
260261
}
261262
else
262263
{
263-
await _bidi.SessionModule.SubscribeAsync([eventName]).ConfigureAwait(false);
264+
var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName]).ConfigureAwait(false);
264265

265266
var eventHandler = new AsyncEventHandler<TEventArgs>(eventName, func);
266267

267268
handlers.Add(eventHandler);
268269

269-
return new Subscription(this, eventHandler);
270+
return new Subscription(subscribeResult.Subscription, this, eventHandler);
270271
}
271272
}
272273

273-
public async Task UnsubscribeAsync(EventHandler eventHandler)
274+
public async Task UnsubscribeAsync(Modules.Session.Subscription subscription, EventHandler eventHandler)
274275
{
275276
var eventHandlers = _eventHandlers[eventHandler.EventName];
276277

277278
eventHandlers.Remove(eventHandler);
278279

279-
if (eventHandler.Contexts is not null)
280+
if (subscription is not null)
280281
{
281-
if (!eventHandlers.Any(h => eventHandler.Contexts.Equals(h.Contexts)) && !eventHandlers.Any(h => h.Contexts is null))
282-
{
283-
await _bidi.SessionModule.UnsubscribeAsync([eventHandler.EventName], new() { Contexts = eventHandler.Contexts }).ConfigureAwait(false);
284-
}
282+
await _bidi.SessionModule.UnsubscribeAsync([subscription]).ConfigureAwait(false);
285283
}
286284
else
287285
{
288-
if (!eventHandlers.Any(h => h.Contexts is not null) && !eventHandlers.Any(h => h.Contexts is null))
286+
if (eventHandler.Contexts is not null)
287+
{
288+
if (!eventHandlers.Any(h => eventHandler.Contexts.Equals(h.Contexts)) && !eventHandlers.Any(h => h.Contexts is null))
289+
{
290+
await _bidi.SessionModule.UnsubscribeAsync([eventHandler.EventName], new() { Contexts = eventHandler.Contexts }).ConfigureAwait(false);
291+
}
292+
}
293+
else
289294
{
290-
await _bidi.SessionModule.UnsubscribeAsync([eventHandler.EventName]).ConfigureAwait(false);
295+
if (!eventHandlers.Any(h => h.Contexts is not null) && !eventHandlers.Any(h => h.Contexts is null))
296+
{
297+
await _bidi.SessionModule.UnsubscribeAsync([eventHandler.EventName]).ConfigureAwait(false);
298+
}
291299
}
292300
}
293301
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
7979
[JsonSerializable(typeof(Modules.Session.NewResult))]
8080
[JsonSerializable(typeof(Modules.Session.EndCommand))]
8181
[JsonSerializable(typeof(Modules.Session.SubscribeCommand))]
82-
[JsonSerializable(typeof(Modules.Session.UnsubscribeCommand))]
82+
[JsonSerializable(typeof(Modules.Session.SubscribeResult))]
83+
[JsonSerializable(typeof(Modules.Session.UnsubscribeByIdCommand))]
84+
[JsonSerializable(typeof(Modules.Session.UnsubscribeByAttributesCommand))]
8385

8486
[JsonSerializable(typeof(Modules.Browser.CloseCommand), TypeInfoPropertyName = "Browser_CloseCommand")]
8587
[JsonSerializable(typeof(Modules.Browser.CreateUserContextCommand))]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// <copyright file="SubscriptionConverter.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+
using System.Text.Json;
22+
using System.Text.Json.Serialization;
23+
24+
#nullable enable
25+
26+
namespace OpenQA.Selenium.BiDi.Communication.Json.Converters;
27+
28+
internal class SubscriptionConverter : JsonConverter<Modules.Session.Subscription>
29+
{
30+
public override Modules.Session.Subscription? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
31+
{
32+
var id = reader.GetString();
33+
34+
return new Modules.Session.Subscription(id!);
35+
}
36+
37+
public override void Write(Utf8JsonWriter writer, Modules.Session.Subscription value, JsonSerializerOptions options)
38+
{
39+
writer.WriteStringValue(value.Id);
40+
}
41+
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ public Task<Subscription> OnDomContentLoadedAsync(Func<NavigationInfo, Task> han
154154
return BiDi.BrowsingContext.OnDomContentLoadedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
155155
}
156156

157+
public Task<Subscription> OnDomContentLoadedAsync(Action<NavigationInfo> handler, SubscriptionOptions? options = null)
158+
{
159+
return BiDi.BrowsingContext.OnDomContentLoadedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
160+
}
161+
157162
public Task<Subscription> OnLoadAsync(Action<NavigationInfo> handler, SubscriptionOptions? options = null)
158163
{
159164
return BiDi.BrowsingContext.OnLoadAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
@@ -194,9 +199,14 @@ public Task<Subscription> OnNavigationFailedAsync(Func<NavigationInfo, Task> han
194199
return BiDi.BrowsingContext.OnNavigationFailedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
195200
}
196201

197-
public Task<Subscription> OnDomContentLoadedAsync(Action<NavigationInfo> handler, SubscriptionOptions? options = null)
202+
public Task<Subscription> OnNavigationCommittedAsync(Action<NavigationInfo> handler, SubscriptionOptions? options = null)
198203
{
199-
return BiDi.BrowsingContext.OnDomContentLoadedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
204+
return BiDi.BrowsingContext.OnNavigationCommittedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
205+
}
206+
207+
public Task<Subscription> OnNavigationCommittedAsync(Func<NavigationInfo, Task> handler, SubscriptionOptions? options = null)
208+
{
209+
return BiDi.BrowsingContext.OnNavigationCommittedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] });
200210
}
201211

202212
public override bool Equals(object? obj)

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

Lines changed: 20 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,7 @@ public class BrowsingContextModule(Broker broker) : Module(broker)
3030
{
3131
public async Task<BrowsingContext> CreateAsync(ContextType type, CreateOptions? options = null)
3232
{
33-
var @params = new CreateCommandParameters(type);
34-
35-
if (options is not null)
36-
{
37-
@params.ReferenceContext = options.ReferenceContext;
38-
@params.Background = options.Background;
39-
@params.UserContext = options.UserContext;
40-
}
33+
var @params = new CreateCommandParameters(type, options?.ReferenceContext, options?.Background, options?.UserContext);
4134

4235
var createResult = await Broker.ExecuteCommandAsync<CreateCommand, CreateResult>(new CreateCommand(@params), options).ConfigureAwait(false);
4336

@@ -46,12 +39,7 @@ public async Task<BrowsingContext> CreateAsync(ContextType type, CreateOptions?
4639

4740
public async Task<NavigateResult> NavigateAsync(BrowsingContext context, string url, NavigateOptions? options = null)
4841
{
49-
var @params = new NavigateCommandParameters(context, url);
50-
51-
if (options is not null)
52-
{
53-
@params.Wait = options.Wait;
54-
}
42+
var @params = new NavigateCommandParameters(context, url, options?.Wait);
5543

5644
return await Broker.ExecuteCommandAsync<NavigateCommand, NavigateResult>(new NavigateCommand(@params), options).ConfigureAwait(false);
5745
}
@@ -65,35 +53,21 @@ public async Task ActivateAsync(BrowsingContext context, ActivateOptions? option
6553

6654
public async Task<LocateNodesResult> LocateNodesAsync(BrowsingContext context, Locator locator, LocateNodesOptions? options = null)
6755
{
68-
var @params = new LocateNodesCommandParameters(context, locator);
69-
70-
if (options is not null)
71-
{
72-
@params.MaxNodeCount = options.MaxNodeCount;
73-
@params.SerializationOptions = options.SerializationOptions;
74-
@params.StartNodes = options.StartNodes;
75-
}
56+
var @params = new LocateNodesCommandParameters(context, locator, options?.MaxNodeCount, options?.SerializationOptions, options?.StartNodes);
7657

7758
return await Broker.ExecuteCommandAsync<LocateNodesCommand, LocateNodesResult>(new LocateNodesCommand(@params), options).ConfigureAwait(false);
7859
}
7960

8061
public async Task<CaptureScreenshotResult> CaptureScreenshotAsync(BrowsingContext context, CaptureScreenshotOptions? options = null)
8162
{
82-
var @params = new CaptureScreenshotCommandParameters(context);
83-
84-
if (options is not null)
85-
{
86-
@params.Origin = options.Origin;
87-
@params.Format = options.Format;
88-
@params.Clip = options.Clip;
89-
}
63+
var @params = new CaptureScreenshotCommandParameters(context, options?.Origin, options?.Format, options?.Clip);
9064

9165
return await Broker.ExecuteCommandAsync<CaptureScreenshotCommand, CaptureScreenshotResult>(new CaptureScreenshotCommand(@params), options).ConfigureAwait(false);
9266
}
9367

9468
public async Task CloseAsync(BrowsingContext context, CloseOptions? options = null)
9569
{
96-
var @params = new CloseCommandParameters(context);
70+
var @params = new CloseCommandParameters(context, options?.PromptUnload);
9771

9872
await Broker.ExecuteCommandAsync(new CloseCommand(@params), options).ConfigureAwait(false);
9973
}
@@ -107,39 +81,21 @@ public async Task<TraverseHistoryResult> TraverseHistoryAsync(BrowsingContext co
10781

10882
public async Task<NavigateResult> ReloadAsync(BrowsingContext context, ReloadOptions? options = null)
10983
{
110-
var @params = new ReloadCommandParameters(context);
111-
112-
if (options is not null)
113-
{
114-
@params.IgnoreCache = options.IgnoreCache;
115-
@params.Wait = options.Wait;
116-
}
84+
var @params = new ReloadCommandParameters(context, options?.IgnoreCache, options?.Wait);
11785

11886
return await Broker.ExecuteCommandAsync<ReloadCommand, NavigateResult>(new ReloadCommand(@params), options).ConfigureAwait(false);
11987
}
12088

12189
public async Task SetViewportAsync(BrowsingContext context, SetViewportOptions? options = null)
12290
{
123-
var @params = new SetViewportCommandParameters(context);
124-
125-
if (options is not null)
126-
{
127-
@params.Viewport = options.Viewport;
128-
@params.DevicePixelRatio = options?.DevicePixelRatio;
129-
}
91+
var @params = new SetViewportCommandParameters(context, options?.Viewport, options?.DevicePixelRatio);
13092

13193
await Broker.ExecuteCommandAsync(new SetViewportCommand(@params), options).ConfigureAwait(false);
13294
}
13395

13496
public async Task<IReadOnlyList<BrowsingContextInfo>> GetTreeAsync(GetTreeOptions? options = null)
13597
{
136-
var @params = new GetTreeCommandParameters();
137-
138-
if (options is not null)
139-
{
140-
@params.MaxDepth = options.MaxDepth;
141-
@params.Root = options.Root;
142-
}
98+
var @params = new GetTreeCommandParameters(options?.MaxDepth, options?.Root);
14399

144100
var getTreeResult = await Broker.ExecuteCommandAsync<GetTreeCommand, GetTreeResult>(new GetTreeCommand(@params), options).ConfigureAwait(false);
145101

@@ -148,31 +104,14 @@ public async Task<IReadOnlyList<BrowsingContextInfo>> GetTreeAsync(GetTreeOption
148104

149105
public async Task<PrintResult> PrintAsync(BrowsingContext context, PrintOptions? options = null)
150106
{
151-
var @params = new PrintCommandParameters(context);
152-
153-
if (options is not null)
154-
{
155-
@params.Background = options.Background;
156-
@params.Margin = options.Margin;
157-
@params.Orientation = options.Orientation;
158-
@params.Page = options.Page;
159-
@params.PageRanges = options.PageRanges;
160-
@params.Scale = options.Scale;
161-
@params.ShrinkToFit = options.ShrinkToFit;
162-
}
107+
var @params = new PrintCommandParameters(context, options?.Background, options?.Margin, options?.Orientation, options?.Page, options?.PageRanges, options?.Scale, options?.ShrinkToFit);
163108

164109
return await Broker.ExecuteCommandAsync<PrintCommand, PrintResult>(new PrintCommand(@params), options).ConfigureAwait(false);
165110
}
166111

167112
public async Task HandleUserPromptAsync(BrowsingContext context, HandleUserPromptOptions? options = null)
168113
{
169-
var @params = new HandleUserPromptCommandParameters(context);
170-
171-
if (options is not null)
172-
{
173-
@params.Accept = options.Accept;
174-
@params.UserText = options.UserText;
175-
}
114+
var @params = new HandleUserPromptCommandParameters(context, options?.Accept, options?.UserText);
176115

177116
await Broker.ExecuteCommandAsync(new HandleUserPromptCommand(@params), options).ConfigureAwait(false);
178117
}
@@ -247,6 +186,16 @@ public async Task<Subscription> OnNavigationFailedAsync(Action<NavigationInfo> h
247186
return await Broker.SubscribeAsync("browsingContext.navigationFailed", handler, options).ConfigureAwait(false);
248187
}
249188

189+
public async Task<Subscription> OnNavigationCommittedAsync(Func<NavigationInfo, Task> handler, BrowsingContextsSubscriptionOptions? options = null)
190+
{
191+
return await Broker.SubscribeAsync("browsingContext.navigationCommitted", handler, options).ConfigureAwait(false);
192+
}
193+
194+
public async Task<Subscription> OnNavigationCommittedAsync(Action<NavigationInfo> handler, BrowsingContextsSubscriptionOptions? options = null)
195+
{
196+
return await Broker.SubscribeAsync("browsingContext.navigationCommitted", handler, options).ConfigureAwait(false);
197+
}
198+
250199
public async Task<Subscription> OnContextCreatedAsync(Func<BrowsingContextInfo, Task> handler, BrowsingContextsSubscriptionOptions? options = null)
251200
{
252201
return await Broker.SubscribeAsync("browsingContext.contextCreated", handler, options).ConfigureAwait(false);

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,7 @@ namespace OpenQA.Selenium.BiDi.Modules.BrowsingContext;
2727
internal class CaptureScreenshotCommand(CaptureScreenshotCommandParameters @params)
2828
: Command<CaptureScreenshotCommandParameters>(@params, "browsingContext.captureScreenshot");
2929

30-
internal record CaptureScreenshotCommandParameters(BrowsingContext Context) : CommandParameters
31-
{
32-
public Origin? Origin { get; set; }
33-
34-
public ImageFormat? Format { get; set; }
35-
36-
public ClipRectangle? Clip { get; set; }
37-
}
30+
internal record CaptureScreenshotCommandParameters(BrowsingContext Context, Origin? Origin, ImageFormat? Format, ClipRectangle? Clip) : CommandParameters;
3831

3932
public record CaptureScreenshotOptions : CommandOptions
4033
{

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ namespace OpenQA.Selenium.BiDi.Modules.BrowsingContext;
2626
internal class CloseCommand(CloseCommandParameters @params)
2727
: Command<CloseCommandParameters>(@params, "browsingContext.close");
2828

29-
internal record CloseCommandParameters(BrowsingContext Context) : CommandParameters;
29+
internal record CloseCommandParameters(BrowsingContext Context, bool? PromptUnload) : CommandParameters;
3030

31-
public record CloseOptions : CommandOptions;
31+
public record CloseOptions : CommandOptions
32+
{
33+
public bool? PromptUnload { get; set; }
34+
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,7 @@ namespace OpenQA.Selenium.BiDi.Modules.BrowsingContext;
2626
internal class CreateCommand(CreateCommandParameters @params)
2727
: Command<CreateCommandParameters>(@params, "browsingContext.create");
2828

29-
internal record CreateCommandParameters(ContextType Type) : CommandParameters
30-
{
31-
public BrowsingContext? ReferenceContext { get; set; }
32-
33-
public bool? Background { get; set; }
34-
35-
public Browser.UserContext? UserContext { get; set; }
36-
}
29+
internal record CreateCommandParameters(ContextType Type, BrowsingContext? ReferenceContext, bool? Background, Browser.UserContext? UserContext) : CommandParameters;
3730

3831
public record CreateOptions : CommandOptions
3932
{

0 commit comments

Comments
 (0)