Skip to content
8 changes: 4 additions & 4 deletions dotnet/src/webdriver/BiDi/Broker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ public Broker(ITransport transport, BiDi bidi)
_processingTask = Task.Run(ProcessMessagesAsync);
}

public async Task<Subscription> SubscribeAsync<TEventArgs, TEventParams>(string eventName, Action<TEventArgs> action, Func<IBiDi, TEventParams, TEventArgs> factory, SubscriptionOptions? options, JsonTypeInfo<TEventParams> jsonTypeInfo, CancellationToken cancellationToken)
public async Task<Subscription> SubscribeAsync<TEventArgs, TEventParams>(Event<TEventArgs, TEventParams> descriptor, Action<TEventArgs> action, SubscriptionOptions? options, CancellationToken cancellationToken)
where TEventArgs : EventArgs
{
ValueTask InvokeAction(EventArgs args) { action((TEventArgs)args); return default; }
return await SubscribeAsync(eventName, InvokeAction, (bidi, ep) => factory(bidi, (TEventParams)ep), jsonTypeInfo, options, cancellationToken).ConfigureAwait(false);
return await SubscribeAsync(descriptor.Name, InvokeAction, (bidi, ep) => descriptor.Factory(bidi, (TEventParams)ep), descriptor.JsonTypeInfo, options, cancellationToken).ConfigureAwait(false);
}

public async Task<Subscription> SubscribeAsync<TEventArgs, TEventParams>(string eventName, Func<TEventArgs, Task> func, Func<IBiDi, TEventParams, TEventArgs> factory, SubscriptionOptions? options, JsonTypeInfo<TEventParams> jsonTypeInfo, CancellationToken cancellationToken)
public async Task<Subscription> SubscribeAsync<TEventArgs, TEventParams>(Event<TEventArgs, TEventParams> descriptor, Func<TEventArgs, Task> func, SubscriptionOptions? options, CancellationToken cancellationToken)
where TEventArgs : EventArgs
{
ValueTask InvokeFunc(EventArgs args) => new(func((TEventArgs)args));
return await SubscribeAsync(eventName, InvokeFunc, (bidi, ep) => factory(bidi, (TEventParams)ep), jsonTypeInfo, options, cancellationToken).ConfigureAwait(false);
return await SubscribeAsync(descriptor.Name, InvokeFunc, (bidi, ep) => descriptor.Factory(bidi, (TEventParams)ep), descriptor.JsonTypeInfo, options, cancellationToken).ConfigureAwait(false);
}

private async Task<Subscription> SubscribeAsync(string eventName, Func<EventArgs, ValueTask> handler, Func<IBiDi, object, EventArgs> argsFactory, JsonTypeInfo jsonTypeInfo, SubscriptionOptions? options, CancellationToken cancellationToken)
Expand Down
164 changes: 103 additions & 61 deletions dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="ContextCreatedEventArgs.cs" company="Selenium Committers">
// <copyright file="ContextCreatedEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="ContextDestroyedEventArgs.cs" company="Selenium Committers">
// <copyright file="ContextDestroyedEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="DomContentLoadedEventArgs.cs" company="Selenium Committers">
// <copyright file="DomContentLoadedEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="DownloadEndEventArgs.cs" company="Selenium Committers">
// <copyright file="DownloadEndEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="DownloadWillBeginEventArgs.cs" company="Selenium Committers">
// <copyright file="DownloadWillBeginEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="FragmentNavigatedEventArgs.cs" company="Selenium Committers">
// <copyright file="FragmentNavigatedEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="HistoryUpdatedEventArgs.cs" company="Selenium Committers">
// <copyright file="HistoryUpdatedEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="LoadEventArgs.cs" company="Selenium Committers">
// <copyright file="LoadEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="NavigationAbortedEventArgs.cs" company="Selenium Committers">
// <copyright file="NavigationAbortedEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="NavigationCommittedEventArgs.cs" company="Selenium Committers">
// <copyright file="NavigationCommittedEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="NavigationEventArgs.cs" company="Selenium Committers">
// <copyright file="NavigationEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="NavigationFailedEventArgs.cs" company="Selenium Committers">
// <copyright file="NavigationFailedEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="NavigationStartedEventArgs.cs" company="Selenium Committers">
// <copyright file="NavigationStartedEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="UserPromptClosedEventArgs.cs" company="Selenium Committers">
// <copyright file="UserPromptClosedEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="UserPromptOpenedEventArgs.cs" company="Selenium Committers">
// <copyright file="UserPromptOpenedEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="EventArgs.cs" company="Selenium Committers">
// <copyright file="Event.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
Expand All @@ -17,6 +17,14 @@
// under the License.
// </copyright>

using System.Text.Json.Serialization.Metadata;

namespace OpenQA.Selenium.BiDi;

public readonly record struct Event<TEventArgs, TEventParams>(
string Name,
Func<IBiDi, TEventParams, TEventArgs> Factory,
JsonTypeInfo<TEventParams> JsonTypeInfo)
where TEventArgs : EventArgs;

public abstract record EventArgs(IBiDi BiDi);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="FileDialogOpenedEventArgs.cs" company="Selenium Committers">
// <copyright file="FileDialogOpenedEvent.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
Expand Down
14 changes: 7 additions & 7 deletions dotnet/src/webdriver/BiDi/Input/InputModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ internal sealed class InputModule : Module, IInputModule
private static readonly Command<SetFilesParameters, SetFilesResult> SetFilesCommand = new(
"input.setFiles", Default.SetFilesParameters, Default.SetFilesResult);

private static readonly Event<FileDialogOpenedEventArgs, FileDialogInfo> FileDialogOpenedEvent = new(
"input.fileDialogOpened",
static (bidi, p) => new FileDialogOpenedEventArgs(bidi, p.Context, p.UserContext, p.Multiple, p.Element),
Default.FileDialogInfo);

public async Task<PerformActionsResult> PerformActionsAsync(BrowsingContext.BrowsingContext context, IEnumerable<SourceActions> actions, PerformActionsOptions? options = null, CancellationToken cancellationToken = default)
{
var @params = new PerformActionsParameters(context, actions);
Expand All @@ -56,17 +61,12 @@ public async Task<SetFilesResult> SetFilesAsync(BrowsingContext.BrowsingContext

public async Task<Subscription> OnFileDialogOpenedAsync(Func<FileDialogOpenedEventArgs, Task> handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default)
{
return await SubscribeAsync("input.fileDialogOpened", handler, CreateFileDialogOpenedEventArgs, options, Default.FileDialogInfo, cancellationToken).ConfigureAwait(false);
return await SubscribeAsync(FileDialogOpenedEvent, handler, options, cancellationToken).ConfigureAwait(false);
}

public async Task<Subscription> OnFileDialogOpenedAsync(Action<FileDialogOpenedEventArgs> handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default)
{
return await SubscribeAsync("input.fileDialogOpened", handler, CreateFileDialogOpenedEventArgs, options, Default.FileDialogInfo, cancellationToken).ConfigureAwait(false);
}

private static FileDialogOpenedEventArgs CreateFileDialogOpenedEventArgs(IBiDi bidi, FileDialogInfo p)
{
return new FileDialogOpenedEventArgs(bidi, p.Context, p.UserContext, p.Multiple, p.Element);
return await SubscribeAsync(FileDialogOpenedEvent, handler, options, cancellationToken).ConfigureAwait(false);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="EntryAddedEventArgs.cs" company="Selenium Committers">
// <copyright file="EntryAddedEvent.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
Expand Down
23 changes: 13 additions & 10 deletions dotnet/src/webdriver/BiDi/Log/LogModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,26 @@ namespace OpenQA.Selenium.BiDi.Log;

internal sealed class LogModule : Module, ILogModule
{
private static readonly Event<EntryAddedEventArgs, LogEntry> EntryAddedEvent = new(
"log.entryAdded",
static (bidi, p) => p switch
{
ConsoleLogEntry c => new ConsoleEntryAddedEventArgs(bidi, c.Level, c.Source, c.Text, c.Timestamp, c.Method, c.Args) { StackTrace = c.StackTrace },
JavascriptLogEntry j => new JavascriptEntryAddedEventArgs(bidi, j.Level, j.Source, j.Text, j.Timestamp) { StackTrace = j.StackTrace },
GenericLogEntry g => new GenericEntryAddedEventArgs(bidi, g.Type, g.Level, g.Source, g.Text, g.Timestamp) { StackTrace = g.StackTrace },
_ => throw new BiDiException($"Unknown {nameof(LogEntry)} type: {p.GetType()}")
},
Default.LogEntry);

public async Task<Subscription> OnEntryAddedAsync(Func<EntryAddedEventArgs, Task> handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default)
{
return await SubscribeAsync("log.entryAdded", handler, CreateEntryAddedEventArgs, options, Default.LogEntry, cancellationToken).ConfigureAwait(false);
return await SubscribeAsync(EntryAddedEvent, handler, options, cancellationToken).ConfigureAwait(false);
}

public async Task<Subscription> OnEntryAddedAsync(Action<EntryAddedEventArgs> handler, SubscriptionOptions? options = null, CancellationToken cancellationToken = default)
{
return await SubscribeAsync("log.entryAdded", handler, CreateEntryAddedEventArgs, options, Default.LogEntry, cancellationToken).ConfigureAwait(false);
return await SubscribeAsync(EntryAddedEvent, handler, options, cancellationToken).ConfigureAwait(false);
}

private static EntryAddedEventArgs CreateEntryAddedEventArgs(IBiDi bidi, LogEntry p) => p switch
{
ConsoleLogEntry c => new ConsoleEntryAddedEventArgs(bidi, c.Level, c.Source, c.Text, c.Timestamp, c.Method, c.Args) { StackTrace = c.StackTrace },
JavascriptLogEntry j => new JavascriptEntryAddedEventArgs(bidi, j.Level, j.Source, j.Text, j.Timestamp) { StackTrace = j.StackTrace },
GenericLogEntry g => new GenericEntryAddedEventArgs(bidi, g.Type, g.Level, g.Source, g.Text, g.Timestamp) { StackTrace = g.StackTrace },
_ => throw new BiDiException($"Unknown {nameof(LogEntry)} type: {p.GetType()}")
};
}

#region https://github.com/dotnet/runtime/issues/72604 Script.RemoteValue type dependency
Expand Down
10 changes: 4 additions & 6 deletions dotnet/src/webdriver/BiDi/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
// under the License.
// </copyright>

using System.Text.Json.Serialization.Metadata;

namespace OpenQA.Selenium.BiDi;

public abstract class Module
Expand All @@ -32,16 +30,16 @@ protected Task<TResult> ExecuteAsync<TParameters, TResult>(Command<TParameters,
return Broker.ExecuteAsync(descriptor, @params, options, cancellationToken);
}

protected Task<Subscription> SubscribeAsync<TEventArgs, TEventParams>(string name, Action<TEventArgs> action, Func<IBiDi, TEventParams, TEventArgs> factory, SubscriptionOptions? options, JsonTypeInfo<TEventParams> jsonTypeInfo, CancellationToken cancellationToken)
protected Task<Subscription> SubscribeAsync<TEventArgs, TEventParams>(Event<TEventArgs, TEventParams> descriptor, Action<TEventArgs> action, SubscriptionOptions? options, CancellationToken cancellationToken)
where TEventArgs : EventArgs
{
return Broker.SubscribeAsync(name, action, factory, options, jsonTypeInfo, cancellationToken);
return Broker.SubscribeAsync(descriptor, action, options, cancellationToken);
}

protected Task<Subscription> SubscribeAsync<TEventArgs, TEventParams>(string name, Func<TEventArgs, Task> func, Func<IBiDi, TEventParams, TEventArgs> factory, SubscriptionOptions? options, JsonTypeInfo<TEventParams> jsonTypeInfo, CancellationToken cancellationToken)
protected Task<Subscription> SubscribeAsync<TEventArgs, TEventParams>(Event<TEventArgs, TEventParams> descriptor, Func<TEventArgs, Task> func, SubscriptionOptions? options, CancellationToken cancellationToken)
where TEventArgs : EventArgs
{
return Broker.SubscribeAsync(name, func, factory, options, jsonTypeInfo, cancellationToken);
return Broker.SubscribeAsync(descriptor, func, options, cancellationToken);
}

internal static TModule Create<TModule>(IBiDi bidi, Broker broker)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="AuthRequiredEventArgs.cs" company="Selenium Committers">
// <copyright file="AuthRequiredEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="BeforeRequestSentEventArgs.cs" company="Selenium Committers">
// <copyright file="BeforeRequestSentEvent.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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="FetchErrorEventArgs.cs" company="Selenium Committers">
// <copyright file="FetchErrorEvent.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
Expand Down
Loading
Loading