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: 8 additions & 8 deletions dotnet/src/webdriver/BiDi/BiDi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,28 @@ private BiDi(string url)

public Emulation.EmulationModule Emulation => AsModule<Emulation.EmulationModule>();

public static async Task<BiDi> ConnectAsync(string url, BiDiOptions? options = null)
public static async Task<BiDi> ConnectAsync(string url, BiDiOptions? options = null, CancellationToken cancellationToken = default)
{
var bidi = new BiDi(url);

await bidi.Broker.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
await bidi.Broker.ConnectAsync(cancellationToken).ConfigureAwait(false);

return bidi;
}

public Task<Session.StatusResult> StatusAsync(Session.StatusOptions? options = null)
public Task<Session.StatusResult> StatusAsync(Session.StatusOptions? options = null, CancellationToken cancellationToken = default)
{
return SessionModule.StatusAsync(options);
return SessionModule.StatusAsync(options, cancellationToken);
}

public Task<Session.NewResult> NewAsync(Session.CapabilitiesRequest capabilities, Session.NewOptions? options = null)
public Task<Session.NewResult> NewAsync(Session.CapabilitiesRequest capabilities, Session.NewOptions? options = null, CancellationToken cancellationToken = default)
{
return SessionModule.NewAsync(capabilities, options);
return SessionModule.NewAsync(capabilities, options, cancellationToken);
}

public Task EndAsync(Session.EndOptions? options = null)
public Task EndAsync(Session.EndOptions? options = null, CancellationToken cancellationToken = default)
{
return SessionModule.EndAsync(options);
return SessionModule.EndAsync(options, cancellationToken);
}

public async ValueTask DisposeAsync()
Expand Down
11 changes: 9 additions & 2 deletions dotnet/src/webdriver/BiDi/Broker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,21 @@ private async Task ProcessEventsAwaiterAsync()
}
}

public async Task<TResult> ExecuteCommandAsync<TCommand, TResult>(TCommand command, CommandOptions? options, JsonTypeInfo<TCommand> jsonCommandTypeInfo, JsonTypeInfo<TResult> jsonResultTypeInfo)
public async Task<TResult> ExecuteCommandAsync<TCommand, TResult>(TCommand command, CommandOptions? options, JsonTypeInfo<TCommand> jsonCommandTypeInfo, JsonTypeInfo<TResult> jsonResultTypeInfo, CancellationToken cancellationToken)
where TCommand : Command
where TResult : EmptyResult
{
command.Id = Interlocked.Increment(ref _currentCommandId);

var tcs = new TaskCompletionSource<EmptyResult>(TaskCreationOptions.RunContinuationsAsynchronously);

using var cts = cancellationToken.CanBeCanceled
? CancellationTokenSource.CreateLinkedTokenSource(cancellationToken)
: new CancellationTokenSource();

var timeout = options?.Timeout ?? TimeSpan.FromSeconds(30);
using var cts = new CancellationTokenSource(timeout);
cts.CancelAfter(timeout);

cts.Token.Register(() => tcs.TrySetCanceled(cts.Token));
var commandInfo = new CommandInfo(command.Id, tcs, jsonResultTypeInfo);
_pendingCommands[command.Id] = commandInfo;
Expand Down
34 changes: 17 additions & 17 deletions dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using OpenQA.Selenium.BiDi.Json.Converters;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;

namespace OpenQA.Selenium.BiDi.Browser;
Expand All @@ -28,55 +29,54 @@ public sealed class BrowserModule : Module
{
private BrowserJsonSerializerContext _jsonContext = null!;

public async Task<CloseResult> CloseAsync(CloseOptions? options = null)
public async Task<CloseResult> CloseAsync(CloseOptions? options = null, CancellationToken cancellationToken = default)
{
return await ExecuteCommandAsync(new CloseCommand(), options, _jsonContext.CloseCommand, _jsonContext.CloseResult).ConfigureAwait(false);
return await ExecuteCommandAsync(new CloseCommand(), options, _jsonContext.CloseCommand, _jsonContext.CloseResult, cancellationToken).ConfigureAwait(false);
}

public async Task<CreateUserContextResult> CreateUserContextAsync(CreateUserContextOptions? options = null)
public async Task<CreateUserContextResult> CreateUserContextAsync(CreateUserContextOptions? options = null, CancellationToken cancellationToken = default)
{
var @params = new CreateUserContextParameters(options?.AcceptInsecureCerts, options?.Proxy, options?.UnhandledPromptBehavior);

return await ExecuteCommandAsync(new CreateUserContextCommand(@params), options, _jsonContext.CreateUserContextCommand, _jsonContext.CreateUserContextResult).ConfigureAwait(false);
return await ExecuteCommandAsync(new CreateUserContextCommand(@params), options, _jsonContext.CreateUserContextCommand, _jsonContext.CreateUserContextResult, cancellationToken).ConfigureAwait(false);
}

public async Task<GetUserContextsResult> GetUserContextsAsync(GetUserContextsOptions? options = null)
public async Task<GetUserContextsResult> GetUserContextsAsync(GetUserContextsOptions? options = null, CancellationToken cancellationToken = default)
{
return await ExecuteCommandAsync(new GetUserContextsCommand(), options, _jsonContext.GetUserContextsCommand, _jsonContext.GetUserContextsResult).ConfigureAwait(false);
return await ExecuteCommandAsync(new GetUserContextsCommand(), options, _jsonContext.GetUserContextsCommand, _jsonContext.GetUserContextsResult, cancellationToken).ConfigureAwait(false);
}

public async Task<RemoveUserContextResult> RemoveUserContextAsync(UserContext userContext, RemoveUserContextOptions? options = null)
public async Task<RemoveUserContextResult> RemoveUserContextAsync(UserContext userContext, RemoveUserContextOptions? options = null, CancellationToken cancellationToken = default)
{
var @params = new RemoveUserContextParameters(userContext);

return await ExecuteCommandAsync(new RemoveUserContextCommand(@params), options, _jsonContext.RemoveUserContextCommand, _jsonContext.RemoveUserContextResult).ConfigureAwait(false);
return await ExecuteCommandAsync(new RemoveUserContextCommand(@params), options, _jsonContext.RemoveUserContextCommand, _jsonContext.RemoveUserContextResult, cancellationToken).ConfigureAwait(false);
}

public async Task<GetClientWindowsResult> GetClientWindowsAsync(GetClientWindowsOptions? options = null)
public async Task<GetClientWindowsResult> GetClientWindowsAsync(GetClientWindowsOptions? options = null, CancellationToken cancellationToken = default)
{
return await ExecuteCommandAsync(new(), options, _jsonContext.GetClientWindowsCommand, _jsonContext.GetClientWindowsResult
).ConfigureAwait(false);
return await ExecuteCommandAsync(new(), options, _jsonContext.GetClientWindowsCommand, _jsonContext.GetClientWindowsResult, cancellationToken).ConfigureAwait(false);
}

public async Task<SetDownloadBehaviorResult> SetDownloadBehaviorAllowedAsync(string destinationFolder, SetDownloadBehaviorOptions? options = null)
public async Task<SetDownloadBehaviorResult> SetDownloadBehaviorAllowedAsync(string destinationFolder, SetDownloadBehaviorOptions? options = null, CancellationToken cancellationToken = default)
{
var @params = new SetDownloadBehaviorParameters(new DownloadBehaviorAllowed(destinationFolder), options?.UserContexts);

return await ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, _jsonContext.SetDownloadBehaviorCommand, _jsonContext.SetDownloadBehaviorResult).ConfigureAwait(false);
return await ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, _jsonContext.SetDownloadBehaviorCommand, _jsonContext.SetDownloadBehaviorResult, cancellationToken).ConfigureAwait(false);
}

public async Task<SetDownloadBehaviorResult> SetDownloadBehaviorAllowedAsync(SetDownloadBehaviorOptions? options = null)
public async Task<SetDownloadBehaviorResult> SetDownloadBehaviorAllowedAsync(SetDownloadBehaviorOptions? options = null, CancellationToken cancellationToken = default)
{
var @params = new SetDownloadBehaviorParameters(null, options?.UserContexts);

return await ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, _jsonContext.SetDownloadBehaviorCommand, _jsonContext.SetDownloadBehaviorResult).ConfigureAwait(false);
return await ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, _jsonContext.SetDownloadBehaviorCommand, _jsonContext.SetDownloadBehaviorResult, cancellationToken).ConfigureAwait(false);
}

public async Task<SetDownloadBehaviorResult> SetDownloadBehaviorDeniedAsync(SetDownloadBehaviorOptions? options = null)
public async Task<SetDownloadBehaviorResult> SetDownloadBehaviorDeniedAsync(SetDownloadBehaviorOptions? options = null, CancellationToken cancellationToken = default)
{
var @params = new SetDownloadBehaviorParameters(new DownloadBehaviorDenied(), options?.UserContexts);

return await ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, _jsonContext.SetDownloadBehaviorCommand, _jsonContext.SetDownloadBehaviorResult).ConfigureAwait(false);
return await ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, _jsonContext.SetDownloadBehaviorCommand, _jsonContext.SetDownloadBehaviorResult, cancellationToken).ConfigureAwait(false);
}

protected override void Initialize(BiDi bidi, JsonSerializerOptions jsonSerializerOptions)
Expand Down
44 changes: 22 additions & 22 deletions dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,59 +71,59 @@ public BiDi BiDi
[JsonIgnore]
public BrowsingContextInputModule Input => _inputModule ?? Interlocked.CompareExchange(ref _inputModule, new BrowsingContextInputModule(this, BiDi.Input), null) ?? _inputModule;

public Task<NavigateResult> NavigateAsync(string url, NavigateOptions? options = null)
public Task<NavigateResult> NavigateAsync(string url, NavigateOptions? options = null, CancellationToken cancellationToken = default)
{
return BiDi.BrowsingContext.NavigateAsync(this, url, options);
return BiDi.BrowsingContext.NavigateAsync(this, url, options, cancellationToken);
}

public Task<ReloadResult> ReloadAsync(ReloadOptions? options = null)
public Task<ReloadResult> ReloadAsync(ReloadOptions? options = null, CancellationToken cancellationToken = default)
{
return BiDi.BrowsingContext.ReloadAsync(this, options);
return BiDi.BrowsingContext.ReloadAsync(this, options, cancellationToken);
}

public Task<ActivateResult> ActivateAsync(ActivateOptions? options = null)
public Task<ActivateResult> ActivateAsync(ActivateOptions? options = null, CancellationToken cancellationToken = default)
{
return BiDi.BrowsingContext.ActivateAsync(this, options);
return BiDi.BrowsingContext.ActivateAsync(this, options, cancellationToken);
}

public Task<LocateNodesResult> LocateNodesAsync(Locator locator, LocateNodesOptions? options = null)
public Task<LocateNodesResult> LocateNodesAsync(Locator locator, LocateNodesOptions? options = null, CancellationToken cancellationToken = default)
{
return BiDi.BrowsingContext.LocateNodesAsync(this, locator, options);
return BiDi.BrowsingContext.LocateNodesAsync(this, locator, options, cancellationToken);
}

public Task<CaptureScreenshotResult> CaptureScreenshotAsync(CaptureScreenshotOptions? options = null)
public Task<CaptureScreenshotResult> CaptureScreenshotAsync(CaptureScreenshotOptions? options = null, CancellationToken cancellationToken = default)
{
return BiDi.BrowsingContext.CaptureScreenshotAsync(this, options);
return BiDi.BrowsingContext.CaptureScreenshotAsync(this, options, cancellationToken);
}

public Task<CloseResult> CloseAsync(CloseOptions? options = null)
public Task<CloseResult> CloseAsync(CloseOptions? options = null, CancellationToken cancellationToken = default)
{
return BiDi.BrowsingContext.CloseAsync(this, options);
return BiDi.BrowsingContext.CloseAsync(this, options, cancellationToken);
}

public Task<TraverseHistoryResult> TraverseHistoryAsync(int delta, TraverseHistoryOptions? options = null)
public Task<TraverseHistoryResult> TraverseHistoryAsync(int delta, TraverseHistoryOptions? options = null, CancellationToken cancellationToken = default)
{
return BiDi.BrowsingContext.TraverseHistoryAsync(this, delta, options);
return BiDi.BrowsingContext.TraverseHistoryAsync(this, delta, options, cancellationToken);
}

public Task<SetViewportResult> SetViewportAsync(SetViewportOptions? options = null)
public Task<SetViewportResult> SetViewportAsync(SetViewportOptions? options = null, CancellationToken cancellationToken = default)
{
return BiDi.BrowsingContext.SetViewportAsync(this, options);
return BiDi.BrowsingContext.SetViewportAsync(this, options, cancellationToken);
}

public Task<PrintResult> PrintAsync(PrintOptions? options = null)
public Task<PrintResult> PrintAsync(PrintOptions? options = null, CancellationToken cancellationToken = default)
{
return BiDi.BrowsingContext.PrintAsync(this, options);
return BiDi.BrowsingContext.PrintAsync(this, options, cancellationToken);
}

public Task<HandleUserPromptResult> HandleUserPromptAsync(HandleUserPromptOptions? options = null)
public Task<HandleUserPromptResult> HandleUserPromptAsync(HandleUserPromptOptions? options = null, CancellationToken cancellationToken = default)
{
return BiDi.BrowsingContext.HandleUserPromptAsync(this, options);
return BiDi.BrowsingContext.HandleUserPromptAsync(this, options, cancellationToken);
}

public Task<GetTreeResult> GetTreeAsync(ContextGetTreeOptions? options = null)
public Task<GetTreeResult> GetTreeAsync(ContextGetTreeOptions? options = null, CancellationToken cancellationToken = default)
{
return BiDi.BrowsingContext.GetTreeAsync(ContextGetTreeOptions.WithContext(options, this));
return BiDi.BrowsingContext.GetTreeAsync(ContextGetTreeOptions.WithContext(options, this), cancellationToken);
}

public Task<Subscription> OnNavigationStartedAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// </copyright>

using System;
using System.Threading;
using System.Threading.Tasks;
using OpenQA.Selenium.BiDi.Input;
using System.Collections.Generic;
Expand All @@ -26,19 +27,19 @@ namespace OpenQA.Selenium.BiDi.BrowsingContext;

public sealed class BrowsingContextInputModule(BrowsingContext context, InputModule inputModule)
{
public Task<PerformActionsResult> PerformActionsAsync(IEnumerable<SourceActions> actions, PerformActionsOptions? options = null)
public Task<PerformActionsResult> PerformActionsAsync(IEnumerable<SourceActions> actions, PerformActionsOptions? options = null, CancellationToken cancellationToken = default)
{
return inputModule.PerformActionsAsync(context, actions, options);
return inputModule.PerformActionsAsync(context, actions, options, cancellationToken);
}

public Task<ReleaseActionsResult> ReleaseActionsAsync(ReleaseActionsOptions? options = null)
public Task<ReleaseActionsResult> ReleaseActionsAsync(ReleaseActionsOptions? options = null, CancellationToken cancellationToken = default)
{
return inputModule.ReleaseActionsAsync(context, options);
return inputModule.ReleaseActionsAsync(context, options, cancellationToken);
}

public Task<SetFilesResult> SetFilesAsync(Script.ISharedReference element, IEnumerable<string> files, SetFilesOptions? options = null)
public Task<SetFilesResult> SetFilesAsync(Script.ISharedReference element, IEnumerable<string> files, SetFilesOptions? options = null, CancellationToken cancellationToken = default)
{
return inputModule.SetFilesAsync(context, element, files, options);
return inputModule.SetFilesAsync(context, element, files, options, cancellationToken);
}

public Task<Subscription> OnFileDialogOpenedAsync(Func<FileDialogInfo, Task> handler, ContextSubscriptionOptions? options = null)
Expand Down
Loading