|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using OpenQA.Selenium.BiDi.Communication; |
| 5 | +using OpenQA.Selenium.BiDi.Communication.Transport; |
| 6 | + |
| 7 | +namespace OpenQA.Selenium.BiDi; |
| 8 | + |
| 9 | +public class BiDi : IAsyncDisposable |
| 10 | +{ |
| 11 | + private readonly ITransport _transport; |
| 12 | + private readonly Broker _broker; |
| 13 | + |
| 14 | + private readonly Lazy<Modules.Session.SessionModule> _sessionModule; |
| 15 | + private readonly Lazy<Modules.BrowsingContext.BrowsingContextModule> _browsingContextModule; |
| 16 | + private readonly Lazy<Modules.Browser.BrowserModule> _browserModule; |
| 17 | + private readonly Lazy<Modules.Network.NetworkModule> _networkModule; |
| 18 | + private readonly Lazy<Modules.Input.InputModule> _inputModule; |
| 19 | + private readonly Lazy<Modules.Script.ScriptModule> _scriptModule; |
| 20 | + private readonly Lazy<Modules.Log.LogModule> _logModule; |
| 21 | + private readonly Lazy<Modules.Storage.StorageModule> _storageModule; |
| 22 | + |
| 23 | + internal BiDi(string url) |
| 24 | + { |
| 25 | + var uri = new Uri(url); |
| 26 | + |
| 27 | + _transport = new WebSocketTransport(new Uri(url)); |
| 28 | + _broker = new Broker(this, _transport); |
| 29 | + |
| 30 | + _sessionModule = new Lazy<Modules.Session.SessionModule>(() => new Modules.Session.SessionModule(_broker)); |
| 31 | + _browsingContextModule = new Lazy<Modules.BrowsingContext.BrowsingContextModule>(() => new Modules.BrowsingContext.BrowsingContextModule(_broker)); |
| 32 | + _browserModule = new Lazy<Modules.Browser.BrowserModule>(() => new Modules.Browser.BrowserModule(_broker)); |
| 33 | + _networkModule = new Lazy<Modules.Network.NetworkModule>(() => new Modules.Network.NetworkModule(_broker)); |
| 34 | + _inputModule = new Lazy<Modules.Input.InputModule>(() => new Modules.Input.InputModule(_broker)); |
| 35 | + _scriptModule = new Lazy<Modules.Script.ScriptModule>(() => new Modules.Script.ScriptModule(_broker)); |
| 36 | + _logModule = new Lazy<Modules.Log.LogModule>(() => new Modules.Log.LogModule(_broker)); |
| 37 | + _storageModule = new Lazy<Modules.Storage.StorageModule>(() => new Modules.Storage.StorageModule(_broker)); |
| 38 | + } |
| 39 | + |
| 40 | + internal Modules.Session.SessionModule SessionModule => _sessionModule.Value; |
| 41 | + internal Modules.BrowsingContext.BrowsingContextModule BrowsingContextModule => _browsingContextModule.Value; |
| 42 | + public Modules.Browser.BrowserModule Browser => _browserModule.Value; |
| 43 | + public Modules.Network.NetworkModule Network => _networkModule.Value; |
| 44 | + internal Modules.Input.InputModule InputModule => _inputModule.Value; |
| 45 | + internal Modules.Script.ScriptModule ScriptModule => _scriptModule.Value; |
| 46 | + public Modules.Log.LogModule Log => _logModule.Value; |
| 47 | + public Modules.Storage.StorageModule Storage => _storageModule.Value; |
| 48 | + |
| 49 | + public Task<Modules.Session.StatusResult> StatusAsync() |
| 50 | + { |
| 51 | + return SessionModule.StatusAsync(); |
| 52 | + } |
| 53 | + |
| 54 | + public static async Task<BiDi> ConnectAsync(string url) |
| 55 | + { |
| 56 | + var bidi = new BiDi(url); |
| 57 | + |
| 58 | + await bidi._broker.ConnectAsync(default).ConfigureAwait(false); |
| 59 | + |
| 60 | + return bidi; |
| 61 | + } |
| 62 | + |
| 63 | + public Task<Modules.BrowsingContext.BrowsingContext> CreateBrowsingContextAsync(Modules.BrowsingContext.BrowsingContextType type, Modules.BrowsingContext.CreateOptions? options = null) |
| 64 | + { |
| 65 | + return BrowsingContextModule.CreateAsync(type, options); |
| 66 | + } |
| 67 | + |
| 68 | + public Task<IReadOnlyList<Modules.BrowsingContext.BrowsingContextInfo>> GetBrowsingContextTreeAsync(Modules.BrowsingContext.GetTreeOptions? options = null) |
| 69 | + { |
| 70 | + return BrowsingContextModule.GetTreeAsync(options); |
| 71 | + } |
| 72 | + |
| 73 | + public Task EndAsync(Modules.Session.EndOptions? options = null) |
| 74 | + { |
| 75 | + return SessionModule.EndAsync(options); |
| 76 | + } |
| 77 | + |
| 78 | + public async ValueTask DisposeAsync() |
| 79 | + { |
| 80 | + await _broker.DisposeAsync().ConfigureAwait(false); |
| 81 | + |
| 82 | + _transport?.Dispose(); |
| 83 | + } |
| 84 | + |
| 85 | + public Task<Subscription> OnBrowsingContextCreatedAsync(Func<Modules.BrowsingContext.BrowsingContextInfo, Task> handler, BrowsingContextsSubscriptionOptions? options = null) |
| 86 | + { |
| 87 | + return BrowsingContextModule.OnContextCreatedAsync(handler, options); |
| 88 | + } |
| 89 | + |
| 90 | + public Task<Subscription> OnBrowsingContextCreatedAsync(Action<Modules.BrowsingContext.BrowsingContextInfo> handler, BrowsingContextsSubscriptionOptions? options = null) |
| 91 | + { |
| 92 | + return BrowsingContextModule.OnContextCreatedAsync(handler, options); |
| 93 | + } |
| 94 | + |
| 95 | + public Task<Subscription> OnBrowsingContextDestroyedAsync(Func<Modules.BrowsingContext.BrowsingContextInfo, Task> handler, BrowsingContextsSubscriptionOptions? options = null) |
| 96 | + { |
| 97 | + return BrowsingContextModule.OnContextDestroyedAsync(handler, options); |
| 98 | + } |
| 99 | + |
| 100 | + public Task<Subscription> OnBrowsingContextDestroyedAsync(Action<Modules.BrowsingContext.BrowsingContextInfo> handler, BrowsingContextsSubscriptionOptions? options = null) |
| 101 | + { |
| 102 | + return BrowsingContextModule.OnContextDestroyedAsync(handler, options); |
| 103 | + } |
| 104 | + |
| 105 | + public Task<Subscription> OnUserPromptOpenedAsync(Func<Modules.BrowsingContext.UserPromptOpenedEventArgs, Task> handler, BrowsingContextsSubscriptionOptions? options = null) |
| 106 | + { |
| 107 | + return BrowsingContextModule.OnUserPromptOpenedAsync(handler, options); |
| 108 | + } |
| 109 | + |
| 110 | + public Task<Subscription> OnUserPromptOpenedAsync(Action<Modules.BrowsingContext.UserPromptOpenedEventArgs> handler, BrowsingContextsSubscriptionOptions? options = null) |
| 111 | + { |
| 112 | + return BrowsingContextModule.OnUserPromptOpenedAsync(handler, options); |
| 113 | + } |
| 114 | + |
| 115 | + public Task<Subscription> OnUserPromptClosedAsync(Func<Modules.BrowsingContext.UserPromptClosedEventArgs, Task> handler, BrowsingContextsSubscriptionOptions? options = null) |
| 116 | + { |
| 117 | + return BrowsingContextModule.OnUserPromptClosedAsync(handler, options); |
| 118 | + } |
| 119 | + |
| 120 | + public Task<Subscription> OnUserPromptClosedAsync(Action<Modules.BrowsingContext.UserPromptClosedEventArgs> handler, BrowsingContextsSubscriptionOptions? options = null) |
| 121 | + { |
| 122 | + return BrowsingContextModule.OnUserPromptClosedAsync(handler, options); |
| 123 | + } |
| 124 | +} |
0 commit comments