diff --git a/dotnet/src/webdriver/BiDi/Browser/GetClientWindowsCommand.cs b/dotnet/src/webdriver/BiDi/Browser/GetClientWindowsCommand.cs index b4d1228f4e26f..9c1dabcbd7778 100644 --- a/dotnet/src/webdriver/BiDi/Browser/GetClientWindowsCommand.cs +++ b/dotnet/src/webdriver/BiDi/Browser/GetClientWindowsCommand.cs @@ -30,18 +30,20 @@ public sealed class GetClientWindowsOptions : CommandOptions; public sealed record GetClientWindowsResult : EmptyResult, IReadOnlyList { - private readonly IReadOnlyList _clientWindows; - internal GetClientWindowsResult(IReadOnlyList clientWindows) { - _clientWindows = clientWindows; + ClientWindows = clientWindows; } - public ClientWindowInfo this[int index] => _clientWindows[index]; + public IReadOnlyList ClientWindows { get; } + + public ClientWindowInfo this[int index] => ClientWindows[index]; + + public int Count => ClientWindows.Count; + - public int Count => _clientWindows.Count; - public IEnumerator GetEnumerator() => _clientWindows.GetEnumerator(); + public IEnumerator GetEnumerator() => ClientWindows.GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() => (_clientWindows as IEnumerable).GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => (ClientWindows as IEnumerable).GetEnumerator(); } diff --git a/dotnet/src/webdriver/BiDi/Browser/GetUserContextsCommand.cs b/dotnet/src/webdriver/BiDi/Browser/GetUserContextsCommand.cs index b6efce675434c..0ea309c6394e9 100644 --- a/dotnet/src/webdriver/BiDi/Browser/GetUserContextsCommand.cs +++ b/dotnet/src/webdriver/BiDi/Browser/GetUserContextsCommand.cs @@ -30,18 +30,18 @@ public class GetUserContextsOptions : CommandOptions; public sealed record GetUserContextsResult : EmptyResult, IReadOnlyList { - private readonly IReadOnlyList _userContexts; - internal GetUserContextsResult(IReadOnlyList userContexts) { - _userContexts = userContexts; + UserContexts = userContexts; } - public UserContextInfo this[int index] => _userContexts[index]; + public IReadOnlyList UserContexts { get; } + + public UserContextInfo this[int index] => UserContexts[index]; - public int Count => _userContexts.Count; + public int Count => UserContexts.Count; - public IEnumerator GetEnumerator() => _userContexts.GetEnumerator(); + public IEnumerator GetEnumerator() => UserContexts.GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() => (_userContexts as IEnumerable).GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => (UserContexts as IEnumerable).GetEnumerator(); } diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs index 11232a165d093..88d765491d7f8 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs @@ -17,7 +17,6 @@ // under the License. // -using System.Collections.Generic; using System.Threading.Tasks; using System; @@ -117,7 +116,7 @@ public Task HandleUserPromptAsync(HandleUserPromptOptions? options = null) return BiDi.BrowsingContext.HandleUserPromptAsync(this, options); } - public Task> GetTreeAsync(BrowsingContextGetTreeOptions? options = null) + public Task GetTreeAsync(BrowsingContextGetTreeOptions? options = null) { GetTreeOptions getTreeOptions = new(options) { diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs index 7db46b952e15b..90205fa695329 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs @@ -18,7 +18,6 @@ // using System; -using System.Collections.Generic; using System.Threading.Tasks; using OpenQA.Selenium.BiDi.Communication; @@ -91,13 +90,11 @@ public async Task SetViewportAsync(BrowsingContext context, SetViewportOptions? await Broker.ExecuteCommandAsync(new SetViewportCommand(@params), options).ConfigureAwait(false); } - public async Task> GetTreeAsync(GetTreeOptions? options = null) + public async Task GetTreeAsync(GetTreeOptions? options = null) { var @params = new GetTreeCommandParameters(options?.MaxDepth, options?.Root); - var getTreeResult = await Broker.ExecuteCommandAsync(new GetTreeCommand(@params), options).ConfigureAwait(false); - - return getTreeResult.Contexts; + return await Broker.ExecuteCommandAsync(new GetTreeCommand(@params), options).ConfigureAwait(false); } public async Task PrintAsync(BrowsingContext context, PrintOptions? options = null) diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/GetTreeCommand.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/GetTreeCommand.cs index 0a0aef4dc75f4..637981f20af78 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/GetTreeCommand.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/GetTreeCommand.cs @@ -18,6 +18,7 @@ // using OpenQA.Selenium.BiDi.Communication; +using System.Collections; using System.Collections.Generic; namespace OpenQA.Selenium.BiDi.BrowsingContext; @@ -46,4 +47,20 @@ public sealed record BrowsingContextGetTreeOptions public long? MaxDepth { get; set; } } -public sealed record GetTreeResult(IReadOnlyList Contexts) : EmptyResult; +public sealed record GetTreeResult : EmptyResult, IReadOnlyList +{ + internal GetTreeResult(IReadOnlyList contexts) + { + Contexts = contexts; + } + + public IReadOnlyList Contexts { get; } + + public BrowsingContextInfo this[int index] => Contexts[index]; + + public int Count => Contexts.Count; + + public IEnumerator GetEnumerator() => Contexts.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => (Contexts as IEnumerable).GetEnumerator(); +} diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/LocateNodesCommand.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/LocateNodesCommand.cs index a8efd228e1b69..3def0f8d4118f 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/LocateNodesCommand.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/LocateNodesCommand.cs @@ -39,18 +39,18 @@ public sealed class LocateNodesOptions : CommandOptions public sealed record LocateNodesResult : EmptyResult, IReadOnlyList { - private readonly IReadOnlyList _nodes; - internal LocateNodesResult(IReadOnlyList nodes) { - _nodes = nodes; + Nodes = nodes; } - public Script.NodeRemoteValue this[int index] => _nodes[index]; + public IReadOnlyList Nodes { get; } + + public Script.NodeRemoteValue this[int index] => Nodes[index]; - public int Count => _nodes.Count; + public int Count => Nodes.Count; - public IEnumerator GetEnumerator() => _nodes.GetEnumerator(); + public IEnumerator GetEnumerator() => Nodes.GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() => (_nodes as IEnumerable).GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => (Nodes as IEnumerable).GetEnumerator(); } diff --git a/dotnet/src/webdriver/BiDi/Communication/Broker.cs b/dotnet/src/webdriver/BiDi/Communication/Broker.cs index db926e3b7228e..f7afe25559b26 100644 --- a/dotnet/src/webdriver/BiDi/Communication/Broker.cs +++ b/dotnet/src/webdriver/BiDi/Communication/Broker.cs @@ -103,6 +103,7 @@ internal Broker(BiDi bidi, Uri url) new Json.Converters.Enumerable.GetUserContextsResultConverter(), new Json.Converters.Enumerable.GetClientWindowsResultConverter(), new Json.Converters.Enumerable.GetRealmsResultConverter(), + new Json.Converters.Enumerable.GetTreeResultConverter(), } }; diff --git a/dotnet/src/webdriver/BiDi/Communication/Json/Converters/Enumerable/GetTreeResultConverter.cs b/dotnet/src/webdriver/BiDi/Communication/Json/Converters/Enumerable/GetTreeResultConverter.cs new file mode 100644 index 0000000000000..b928c9d8c5114 --- /dev/null +++ b/dotnet/src/webdriver/BiDi/Communication/Json/Converters/Enumerable/GetTreeResultConverter.cs @@ -0,0 +1,43 @@ +// +// 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 +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +using OpenQA.Selenium.BiDi.BrowsingContext; +using OpenQA.Selenium.BiDi.Communication.Json.Internal; +using System; +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace OpenQA.Selenium.BiDi.Communication.Json.Converters.Enumerable; + +internal class GetTreeResultConverter : JsonConverter +{ + public override GetTreeResult Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + using var doc = JsonDocument.ParseValue(ref reader); + var contexts = doc.RootElement.GetProperty("contexts").Deserialize(options.GetTypeInfo>()); + + return new GetTreeResult(contexts!); + } + + public override void Write(Utf8JsonWriter writer, GetTreeResult value, JsonSerializerOptions options) + { + throw new NotImplementedException(); + } +} diff --git a/dotnet/src/webdriver/BiDi/Storage/GetCookiesCommand.cs b/dotnet/src/webdriver/BiDi/Storage/GetCookiesCommand.cs index 30a5e23f00754..9e097dacf3e14 100644 --- a/dotnet/src/webdriver/BiDi/Storage/GetCookiesCommand.cs +++ b/dotnet/src/webdriver/BiDi/Storage/GetCookiesCommand.cs @@ -39,23 +39,23 @@ public sealed class GetCookiesOptions : CommandOptions public sealed record GetCookiesResult : EmptyResult, IReadOnlyList { - private readonly IReadOnlyList _cookies; - internal GetCookiesResult(IReadOnlyList cookies, PartitionKey partitionKey) { - _cookies = cookies; + Cookies = cookies; PartitionKey = partitionKey; } + public IReadOnlyList Cookies { get; } + public PartitionKey PartitionKey { get; init; } - public Network.Cookie this[int index] => _cookies[index]; + public Network.Cookie this[int index] => Cookies[index]; - public int Count => _cookies.Count; + public int Count => Cookies.Count; - public IEnumerator GetEnumerator() => _cookies.GetEnumerator(); + public IEnumerator GetEnumerator() => Cookies.GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() => (_cookies as IEnumerable).GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => (Cookies as IEnumerable).GetEnumerator(); } public sealed record CookieFilter