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
16 changes: 3 additions & 13 deletions dotnet/src/webdriver/BiDi/Broker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
Expand Down Expand Up @@ -113,16 +112,7 @@ private async Task ProcessEventsAwaiterAsync()

args.BiDi = _bidi;

// handle browsing context subscriber
if (handler.Contexts is not null && args is BrowsingContextEventArgs browsingContextEventArgs && handler.Contexts.Contains(browsingContextEventArgs.Context))
{
await handler.InvokeAsync(args).ConfigureAwait(false);
}
// handle only session subscriber
else if (handler.Contexts is null)
{
await handler.InvokeAsync(args).ConfigureAwait(false);
}
await handler.InvokeAsync(args).ConfigureAwait(false);
}
}
}
Expand Down Expand Up @@ -164,7 +154,7 @@ public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Act

var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = options?.Contexts, UserContexts = options?.UserContexts }).ConfigureAwait(false);

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

handlers.Add(eventHandler);

Expand All @@ -180,7 +170,7 @@ public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Fun

var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = options?.Contexts, UserContexts = options?.UserContexts }).ConfigureAwait(false);

var eventHandler = new AsyncEventHandler<TEventArgs>(eventName, func, options?.Contexts);
var eventHandler = new AsyncEventHandler<TEventArgs>(eventName, func);

handlers.Add(eventHandler);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
namespace OpenQA.Selenium.BiDi.BrowsingContext;

public sealed record BrowsingContextInfo(IReadOnlyList<BrowsingContextInfo>? Children, Browser.ClientWindow ClientWindow, BrowsingContext Context, BrowsingContext? OriginalOpener, string Url, Browser.UserContext UserContext, BrowsingContext? Parent)
: BrowsingContextEventArgs(Context);
: EventArgs;
Original file line number Diff line number Diff line change
Expand Up @@ -91,52 +91,112 @@ public Task<SetCacheBehaviorResult> SetCacheBehaviorAsync(CacheBehavior behavior

public Task<Subscription> OnBeforeRequestSentAsync(Func<BeforeRequestSentEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
{
return networkModule.OnBeforeRequestSentAsync(handler, options.WithContext(context));
return networkModule.OnBeforeRequestSentAsync(async e =>
{
if (context.Equals(e.Context))
{
await handler(e).ConfigureAwait(false);
}
}, options.WithContext(context));
}

public Task<Subscription> OnBeforeRequestSentAsync(Action<BeforeRequestSentEventArgs> handler, ContextSubscriptionOptions? options = null)
{
return networkModule.OnBeforeRequestSentAsync(handler, options.WithContext(context));
return networkModule.OnBeforeRequestSentAsync(e =>
{
if (context.Equals(e.Context))
{
handler(e);
}
}, options.WithContext(context));
}

public Task<Subscription> OnResponseStartedAsync(Func<ResponseStartedEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
{
return networkModule.OnResponseStartedAsync(handler, options.WithContext(context));
return networkModule.OnResponseStartedAsync(async e =>
{
if (context.Equals(e.Context))
{
await handler(e).ConfigureAwait(false);
}
}, options.WithContext(context));
}

public Task<Subscription> OnResponseStartedAsync(Action<ResponseStartedEventArgs> handler, ContextSubscriptionOptions? options = null)
{
return networkModule.OnResponseStartedAsync(handler, options.WithContext(context));
return networkModule.OnResponseStartedAsync(e =>
{
if (context.Equals(e.Context))
{
handler(e);
}
}, options.WithContext(context));
}

public Task<Subscription> OnResponseCompletedAsync(Func<ResponseCompletedEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
{
return networkModule.OnResponseCompletedAsync(handler, options.WithContext(context));
return networkModule.OnResponseCompletedAsync(async e =>
{
if (context.Equals(e.Context))
{
await handler(e).ConfigureAwait(false);
}
}, options.WithContext(context));
}

public Task<Subscription> OnResponseCompletedAsync(Action<ResponseCompletedEventArgs> handler, ContextSubscriptionOptions? options = null)
{
return networkModule.OnResponseCompletedAsync(handler, options.WithContext(context));
return networkModule.OnResponseCompletedAsync(e =>
{
if (context.Equals(e.Context))
{
handler(e);
}
}, options.WithContext(context));
}

public Task<Subscription> OnFetchErrorAsync(Func<FetchErrorEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
{
return networkModule.OnFetchErrorAsync(handler, options.WithContext(context));
return networkModule.OnFetchErrorAsync(async e =>
{
if (context.Equals(e.Context))
{
await handler(e).ConfigureAwait(false);
}
}, options.WithContext(context));
}

public Task<Subscription> OnFetchErrorAsync(Action<FetchErrorEventArgs> handler, ContextSubscriptionOptions? options = null)
{
return networkModule.OnFetchErrorAsync(handler, options.WithContext(context));
return networkModule.OnFetchErrorAsync(e =>
{
if (context.Equals(e.Context))
{
handler(e);
}
}, options.WithContext(context));
}

public Task<Subscription> OnAuthRequiredAsync(Func<AuthRequiredEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
{
return networkModule.OnAuthRequiredAsync(handler, options.WithContext(context));
return networkModule.OnAuthRequiredAsync(async e =>
{
if (context.Equals(e.Context))
{
await handler(e).ConfigureAwait(false);
}
}, options.WithContext(context));
}

public Task<Subscription> OnAuthRequiredAsync(Action<AuthRequiredEventArgs> handler, ContextSubscriptionOptions? options = null)
{
return networkModule.OnAuthRequiredAsync(handler, options.WithContext(context));
return networkModule.OnAuthRequiredAsync(e =>
{
if (context.Equals(e.Context))
{
handler(e);
}
}, options.WithContext(context));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// </copyright>

using OpenQA.Selenium.BiDi.Script;
using OpenQA.Selenium.Internal;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace OpenQA.Selenium.BiDi.BrowsingContext;
//[JsonDerivedType(typeof(DownloadCompleteEventArgs), "complete")]
[JsonConverter(typeof(DownloadEndEventArgsConverter))]
public abstract record DownloadEndEventArgs(BrowsingContext Context)
: BrowsingContextEventArgs(Context);
: EventArgs;

public sealed record DownloadCanceledEventArgs(BrowsingContext Context, Navigation? Navigation, DateTimeOffset Timestamp, string Url)
: DownloadEndEventArgs(Context), IBaseNavigationInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
namespace OpenQA.Selenium.BiDi.BrowsingContext;

public sealed record DownloadWillBeginEventArgs(string SuggestedFilename, BrowsingContext Context, Navigation? Navigation, DateTimeOffset Timestamp, string Url)
: BrowsingContextEventArgs(Context), IBaseNavigationInfo;
: EventArgs, IBaseNavigationInfo;
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
namespace OpenQA.Selenium.BiDi.BrowsingContext;

public sealed record HistoryUpdatedEventArgs(BrowsingContext Context, DateTimeOffset Timestamp, string Url)
: BrowsingContextEventArgs(Context);
: EventArgs;
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
namespace OpenQA.Selenium.BiDi.BrowsingContext;

public sealed record NavigationInfo(BrowsingContext Context, Navigation? Navigation, DateTimeOffset Timestamp, string Url)
: BrowsingContextEventArgs(Context), IBaseNavigationInfo;
: EventArgs, IBaseNavigationInfo;
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
namespace OpenQA.Selenium.BiDi.BrowsingContext;

public sealed record UserPromptClosedEventArgs(BrowsingContext Context, bool Accepted, string? UserText)
: BrowsingContextEventArgs(Context);
: EventArgs;
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace OpenQA.Selenium.BiDi.BrowsingContext;

public sealed record UserPromptOpenedEventArgs(BrowsingContext Context, Session.UserPromptHandlerType Handler, UserPromptType Type, string Message, string? DefaultValue)
: BrowsingContextEventArgs(Context);
: EventArgs;

[JsonConverter(typeof(CamelCaseEnumConverter<UserPromptType>))]
public enum UserPromptType
Expand Down
3 changes: 0 additions & 3 deletions dotnet/src/webdriver/BiDi/EventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,3 @@ public BiDi BiDi
internal set => _bidi = value;
}
}

public abstract record BrowsingContextEventArgs(BrowsingContext.BrowsingContext Context)
: EventArgs;
13 changes: 5 additions & 8 deletions dotnet/src/webdriver/BiDi/EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,19 @@
// </copyright>

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace OpenQA.Selenium.BiDi;

internal abstract class EventHandler(string eventName, IEnumerable<BrowsingContext.BrowsingContext>? contexts = null)
internal abstract class EventHandler(string eventName)
{
public string EventName { get; } = eventName;

public IEnumerable<BrowsingContext.BrowsingContext>? Contexts { get; } = contexts;

public abstract ValueTask InvokeAsync(EventArgs args);
}

internal class AsyncEventHandler<TEventArgs>(string eventName, Func<TEventArgs, Task> func, IEnumerable<BrowsingContext.BrowsingContext>? contexts = null)
: EventHandler(eventName, contexts) where TEventArgs : EventArgs
internal class AsyncEventHandler<TEventArgs>(string eventName, Func<TEventArgs, Task> func)
: EventHandler(eventName) where TEventArgs : EventArgs
{
private readonly Func<TEventArgs, Task> _func = func;

Expand All @@ -43,8 +40,8 @@ public override async ValueTask InvokeAsync(EventArgs args)
}
}

internal class SyncEventHandler<TEventArgs>(string eventName, Action<TEventArgs> action, IEnumerable<BrowsingContext.BrowsingContext>? contexts = null)
: EventHandler(eventName, contexts) where TEventArgs : EventArgs
internal class SyncEventHandler<TEventArgs>(string eventName, Action<TEventArgs> action)
: EventHandler(eventName) where TEventArgs : EventArgs
{
private readonly Action<TEventArgs> _action = action;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
namespace OpenQA.Selenium.BiDi.Network;

public abstract record BaseParametersEventArgs(BrowsingContext.BrowsingContext? Context, bool IsBlocked, BrowsingContext.Navigation? Navigation, long RedirectCount, RequestData Request, DateTimeOffset Timestamp, IReadOnlyList<Intercept>? Intercepts)
: BrowsingContextEventArgs(Context);
: EventArgs;