Skip to content

Commit 9c8b7b8

Browse files
committed
[dotnet] [bidi] Support SetClientWindowState in Browser module
1 parent 99419ef commit 9c8b7b8

File tree

7 files changed

+129
-5
lines changed

7 files changed

+129
-5
lines changed

dotnet/src/webdriver/BiDi/Communication/Broker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ internal Broker(BiDi bidi, Uri url)
7171
Converters =
7272
{
7373
new BrowsingContextConverter(_bidi),
74-
new BrowserUserContextConverter(bidi),
75-
new BrowserClientWindowConverter(),
74+
new BrowserUserContextConverter(_bidi),
75+
new BrowserClientWindowConverter(_bidi),
7676
new NavigationConverter(),
7777
new InterceptConverter(_bidi),
7878
new RequestConverter(_bidi),

dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
9090
[JsonSerializable(typeof(Modules.Browser.RemoveUserContextCommand))]
9191
[JsonSerializable(typeof(Modules.Browser.GetClientWindowsCommand))]
9292
[JsonSerializable(typeof(Modules.Browser.GetClientWindowsResult))]
93+
[JsonSerializable(typeof(Modules.Browser.SetClientWindowStateCommand))]
9394
[JsonSerializable(typeof(Modules.Browser.UserContextInfo))]
9495
[JsonSerializable(typeof(IReadOnlyList<Modules.Browser.UserContextInfo>))]
9596
[JsonSerializable(typeof(IReadOnlyList<Modules.Browser.ClientWindowInfo>))]

dotnet/src/webdriver/BiDi/Communication/Json/Converters/BrowserClientWindowConverter.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ namespace OpenQA.Selenium.BiDi.Communication.Json.Converters;
2626

2727
internal class BrowserClientWindowConverter : JsonConverter<ClientWindow>
2828
{
29+
private readonly BiDi _bidi;
30+
31+
public BrowserClientWindowConverter(BiDi bidi)
32+
{
33+
_bidi = bidi;
34+
}
35+
2936
public override ClientWindow? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
3037
{
3138
var id = reader.GetString();
3239

33-
return new ClientWindow(id!);
40+
return new ClientWindow(_bidi, id!);
3441
}
3542

3643
public override void Write(Utf8JsonWriter writer, ClientWindow value, JsonSerializerOptions options)

dotnet/src/webdriver/BiDi/Modules/Browser/BrowserModule.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
// under the License.
1818
// </copyright>
1919

20-
using System.Collections.Generic;
2120
using System.Threading.Tasks;
2221
using OpenQA.Selenium.BiDi.Communication;
2322

@@ -51,4 +50,18 @@ public async Task<GetClientWindowsResult> GetClientWindowsAsync(GetClientWindows
5150
{
5251
return await Broker.ExecuteCommandAsync<GetClientWindowsCommand, GetClientWindowsResult>(new(), options).ConfigureAwait(false);
5352
}
53+
54+
internal async Task<ClientWindowInfo> SetClientWindowStateAsync(ClientWindow clientWindow, ClientWindowNamedState state, SetClientWindowNamedStateOptions? options = null)
55+
{
56+
var @params = new SetClientWindowNamedStateCommandParameters(clientWindow, state);
57+
58+
return await Broker.ExecuteCommandAsync<SetClientWindowStateCommand, ClientWindowInfo>(new SetClientWindowStateCommand(@params), options).ConfigureAwait(false);
59+
}
60+
61+
internal async Task<ClientWindowInfo> SetClientWindowStateAsync(ClientWindow clientWindow, SetClientWindowRectStateOptions? options = null)
62+
{
63+
var @params = new SetClientWindowRectStateCommandParameters(clientWindow, options);
64+
65+
return await Broker.ExecuteCommandAsync<SetClientWindowStateCommand, ClientWindowInfo>(new SetClientWindowStateCommand(@params), options).ConfigureAwait(false);
66+
}
5467
}

dotnet/src/webdriver/BiDi/Modules/Browser/ClientWindow.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,29 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using System.Threading.Tasks;
21+
2022
namespace OpenQA.Selenium.BiDi.Modules.Browser;
2123

2224
public record ClientWindow
2325
{
24-
internal ClientWindow(string id)
26+
internal ClientWindow(BiDi bidi, string id)
2527
{
28+
BiDi = bidi;
2629
Id = id;
2730
}
2831

32+
internal BiDi BiDi { get; }
33+
2934
internal string Id { get; }
35+
36+
public Task<ClientWindowInfo> SetStateAsync(ClientWindowNamedState state, SetClientWindowNamedStateOptions? options = null)
37+
{
38+
return BiDi.Browser.SetClientWindowStateAsync(this, state, options);
39+
}
40+
41+
public Task<ClientWindowInfo> SetStateAsync(SetClientWindowRectStateOptions? options = null)
42+
{
43+
return BiDi.Browser.SetClientWindowStateAsync(this, options);
44+
}
3045
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// <copyright file="SetClientWindowStateCommand.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
using OpenQA.Selenium.BiDi.Communication;
21+
using System.Text.Json.Serialization;
22+
23+
namespace OpenQA.Selenium.BiDi.Modules.Browser;
24+
25+
internal class SetClientWindowStateCommand(SetClientWindowStateCommandParameters @params)
26+
: Command<SetClientWindowStateCommandParameters>(@params, "browser.setClientWindowState");
27+
28+
[JsonDerivedType(typeof(SetClientWindowNamedStateCommandParameters))]
29+
[JsonDerivedType(typeof(SetClientWindowRectStateCommandParameters))]
30+
internal abstract record SetClientWindowStateCommandParameters(ClientWindow ClientWindow) : CommandParameters;
31+
32+
internal record SetClientWindowNamedStateCommandParameters(ClientWindow ClientWindow, ClientWindowNamedState State) : SetClientWindowStateCommandParameters(ClientWindow);
33+
34+
internal record SetClientWindowRectStateCommandParameters(ClientWindow ClientWindow, [property: JsonIgnore] SetClientWindowRectStateOptions? Options) : SetClientWindowStateCommandParameters(ClientWindow)
35+
{
36+
[JsonInclude]
37+
internal string State { get; } = "normal";
38+
39+
public int? Width { get; set; } = Options?.Width;
40+
41+
public int? Height { get; set; } = Options?.Height;
42+
43+
public int? X { get; set; } = Options?.X;
44+
45+
public int? Y { get; set; } = Options?.Y;
46+
}
47+
48+
public record SetClientWindowNamedStateOptions : CommandOptions;
49+
50+
public record SetClientWindowRectStateOptions : CommandOptions
51+
{
52+
public int? Width { get; set; }
53+
54+
public int? Height { get; set; }
55+
56+
public int? X { get; set; }
57+
58+
public int? Y { get; set; }
59+
}
60+
61+
public enum ClientWindowNamedState
62+
{
63+
Fullscreen,
64+
Maximized,
65+
Minimized
66+
}

dotnet/test/common/BiDi/Browser/BrowserTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,26 @@ public async Task CanGetClientWindows()
7171
Assert.That(clientWindows, Has.Count.GreaterThanOrEqualTo(1));
7272
Assert.That(clientWindows[0].ClientWindow, Is.Not.Null);
7373
}
74+
75+
[Test]
76+
public async Task CanSetClientWindowNamedState()
77+
{
78+
var clientWindows = await bidi.Browser.GetClientWindowsAsync();
79+
80+
var clientWindowInfo = await clientWindows[0].ClientWindow.SetStateAsync(Modules.Browser.ClientWindowNamedState.Fullscreen);
81+
82+
// TODO: Make assertion meaningfull
83+
Assert.That(clientWindowInfo, Is.Not.Null);
84+
}
85+
86+
[Test]
87+
public async Task CanSetClientWindowRectState()
88+
{
89+
var clientWindows = await bidi.Browser.GetClientWindowsAsync();
90+
91+
var clientWindowInfo = await clientWindows[0].ClientWindow.SetStateAsync(new() { Width = 500, Height = 300, X = 10, Y = 10 });
92+
93+
// TODO: Make assertion meaningfull
94+
Assert.That(clientWindowInfo, Is.Not.Null);
95+
}
7496
}

0 commit comments

Comments
 (0)