Skip to content
Draft
14 changes: 14 additions & 0 deletions dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,18 @@ public async Task<GetClientWindowsResult> GetClientWindowsAsync(GetClientWindows
{
return await Broker.ExecuteCommandAsync<GetClientWindowsCommand, GetClientWindowsResult>(new(), options).ConfigureAwait(false);
}

internal async Task<ClientWindowInfo> SetClientWindowStateAsync(ClientWindow clientWindow, ClientWindowNamedState state, SetClientWindowNamedStateOptions? options = null)
{
var @params = new SetClientWindowNamedStateCommandParameters(clientWindow, state);

return await Broker.ExecuteCommandAsync<SetClientWindowStateCommand, ClientWindowInfo>(new SetClientWindowStateCommand(@params), options).ConfigureAwait(false);
}

internal async Task<ClientWindowInfo> SetClientWindowStateAsync(ClientWindow clientWindow, SetClientWindowRectStateOptions? options = null)
{
var @params = new SetClientWindowRectStateCommandParameters(clientWindow, options);

return await Broker.ExecuteCommandAsync<SetClientWindowStateCommand, ClientWindowInfo>(new SetClientWindowStateCommand(@params), options).ConfigureAwait(false);
}
}
17 changes: 16 additions & 1 deletion dotnet/src/webdriver/BiDi/Browser/ClientWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,29 @@
// under the License.
// </copyright>

using System.Threading.Tasks;

namespace OpenQA.Selenium.BiDi.Browser;

public sealed record ClientWindow
{
internal ClientWindow(string id)
internal ClientWindow(BiDi bidi, string id)
{
BiDi = bidi;
Id = id;
}

internal BiDi BiDi { get; }

internal string Id { get; }

public Task<ClientWindowInfo> SetStateAsync(ClientWindowNamedState state, SetClientWindowNamedStateOptions? options = null)
{
return BiDi.Browser.SetClientWindowStateAsync(this, state, options);
}

public Task<ClientWindowInfo> SetStateAsync(SetClientWindowRectStateOptions? options = null)
{
return BiDi.Browser.SetClientWindowStateAsync(this, options);
}
}
4 changes: 3 additions & 1 deletion dotnet/src/webdriver/BiDi/Browser/ClientWindowInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
// under the License.
// </copyright>

using OpenQA.Selenium.BiDi.Communication;

namespace OpenQA.Selenium.BiDi.Browser;

public sealed record ClientWindowInfo(bool Active, ClientWindow ClientWindow, ClientWindowState State, int Height, int Width, int X, int Y);
public sealed record ClientWindowInfo(bool Active, ClientWindow ClientWindow, ClientWindowState State, int Height, int Width, int X, int Y) : EmptyResult;

public enum ClientWindowState
{
Expand Down
66 changes: 66 additions & 0 deletions dotnet/src/webdriver/BiDi/Browser/SetClientWindowStateCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// <copyright file="SetClientWindowStateCommand.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
// 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.
// </copyright>

using OpenQA.Selenium.BiDi.Communication;
using System.Text.Json.Serialization;

namespace OpenQA.Selenium.BiDi.Browser;

internal class SetClientWindowStateCommand(SetClientWindowStateCommandParameters @params)
: Command<SetClientWindowStateCommandParameters, ClientWindowInfo>(@params, "browser.setClientWindowState");

[JsonDerivedType(typeof(SetClientWindowNamedStateCommandParameters))]
[JsonDerivedType(typeof(SetClientWindowRectStateCommandParameters))]
internal abstract record SetClientWindowStateCommandParameters(ClientWindow ClientWindow) : Parameters;

internal record SetClientWindowNamedStateCommandParameters(ClientWindow ClientWindow, ClientWindowNamedState State) : SetClientWindowStateCommandParameters(ClientWindow);

internal record SetClientWindowRectStateCommandParameters(ClientWindow ClientWindow, [property: JsonIgnore] SetClientWindowRectStateOptions? Options) : SetClientWindowStateCommandParameters(ClientWindow)
{
[JsonInclude]
internal string State { get; } = "normal";

public int? Width { get; set; } = Options?.Width;

public int? Height { get; set; } = Options?.Height;

public int? X { get; set; } = Options?.X;

public int? Y { get; set; } = Options?.Y;
}

public sealed class SetClientWindowNamedStateOptions : CommandOptions;

public sealed class SetClientWindowRectStateOptions : CommandOptions
{
public int? Width { get; set; }

public int? Height { get; set; }

public int? X { get; set; }

public int? Y { get; set; }
}

public enum ClientWindowNamedState
{
Fullscreen,
Maximized,
Minimized
}
4 changes: 2 additions & 2 deletions dotnet/src/webdriver/BiDi/Communication/Broker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ internal Broker(BiDi bidi, Uri url)
Converters =
{
new BrowsingContextConverter(_bidi),
new BrowserUserContextConverter(bidi),
new BrowserClientWindowConverter(),
new BrowserUserContextConverter(_bidi),
new BrowserClientWindowConverter(_bidi),
new NavigationConverter(),
new CollectorConverter(_bidi),
new InterceptConverter(_bidi),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
[JsonSerializable(typeof(Browser.RemoveUserContextCommand))]
[JsonSerializable(typeof(Browser.GetClientWindowsCommand))]
[JsonSerializable(typeof(Browser.GetClientWindowsResult))]
[JsonSerializable(typeof(Browser.SetClientWindowStateCommand))]
[JsonSerializable(typeof(Browser.UserContextInfo))]
[JsonSerializable(typeof(IReadOnlyList<Browser.UserContextInfo>))]
[JsonSerializable(typeof(IReadOnlyList<Browser.ClientWindowInfo>))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ namespace OpenQA.Selenium.BiDi.Communication.Json.Converters;

internal class BrowserClientWindowConverter : JsonConverter<ClientWindow>
{
private readonly BiDi _bidi;

public BrowserClientWindowConverter(BiDi bidi)
{
_bidi = bidi;
}

public override ClientWindow? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var id = reader.GetString();

return new ClientWindow(id!);
return new ClientWindow(_bidi, id!);
}

public override void Write(Utf8JsonWriter writer, ClientWindow value, JsonSerializerOptions options)
Expand Down
24 changes: 24 additions & 0 deletions dotnet/test/common/BiDi/Browser/BrowserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,28 @@ public async Task CanGetClientWindows()
Assert.That(clientWindows, Has.Count.GreaterThanOrEqualTo(1));
Assert.That(clientWindows[0].ClientWindow, Is.Not.Null);
}

[Test]
[Ignore("Not yet implemented by all vendors")]
public async Task CanSetClientWindowNamedState()
{
var clientWindows = await bidi.Browser.GetClientWindowsAsync();

var clientWindowInfo = await clientWindows[0].ClientWindow.SetStateAsync(ClientWindowNamedState.Fullscreen);

// TODO: Make assertion meaningfull
Assert.That(clientWindowInfo, Is.Not.Null);
}

[Test]
[Ignore("Not yet implemented by all vendors")]
public async Task CanSetClientWindowRectState()
{
var clientWindows = await bidi.Browser.GetClientWindowsAsync();

var clientWindowInfo = await clientWindows[0].ClientWindow.SetStateAsync(new() { Width = 500, Height = 300, X = 10, Y = 10 });

// TODO: Make assertion meaningfull
Assert.That(clientWindowInfo, Is.Not.Null);
}
}
Loading