Skip to content

Commit 1fbf4c6

Browse files
committed
[dotnet] Implement Permissions module
1 parent 71a7489 commit 1fbf4c6

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// <copyright file="PermissionDescriptor.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+
namespace OpenQA.Selenium.BiDi.Extensions.Permissions;
21+
22+
internal record PermissionDescriptor(string Name);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// <copyright file="PermissionState.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.Json.Converters;
21+
using System.Text.Json.Serialization;
22+
23+
namespace OpenQA.Selenium.BiDi.Extensions.Permissions;
24+
25+
[JsonConverter(typeof(CamelCaseEnumConverter<PermissionState>))]
26+
public enum PermissionState
27+
{
28+
Granted,
29+
Denied,
30+
Prompt
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using OpenQA.Selenium.BiDi.Browser;
2+
using OpenQA.Selenium.BiDi.Communication;
3+
using OpenQA.Selenium.BiDi.Communication.Json.Converters;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
using System.Threading.Tasks;
7+
8+
namespace OpenQA.Selenium.BiDi.Extensions.Permissions;
9+
10+
public class PermissionsModule : Module
11+
{
12+
private PermissionsJsonSerializerContext JsonContext => (PermissionsJsonSerializerContext)base.JsonContext;
13+
14+
public async Task SetPermissionAsync(string permissionName, PermissionState state, string origin, UserContext? userContext, SetPermissionOptions? options = null)
15+
{
16+
var @params = new SetPermissionCommandParameters(new PermissionDescriptor(permissionName), state, origin, userContext);
17+
18+
await Broker.ExecuteCommandAsync(new SetPermissionCommand(@params), options, JsonContext.Permissions_SetPermissionCommand, JsonContext.Permissions_SetPermissionResult).ConfigureAwait(false);
19+
}
20+
21+
protected override JsonSerializerContext Initialize(JsonSerializerOptions options)
22+
{
23+
return new PermissionsJsonSerializerContext(options);
24+
}
25+
}
26+
27+
[JsonSerializable(typeof(SetPermissionCommand), TypeInfoPropertyName = "Permissions_SetPermissionCommand")]
28+
[JsonSerializable(typeof(SetPermissionResult), TypeInfoPropertyName = "Permissions_SetPermissionResult")]
29+
internal partial class PermissionsJsonSerializerContext : JsonSerializerContext;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using OpenQA.Selenium.BiDi.Browser;
2+
using OpenQA.Selenium.BiDi.Communication;
3+
using OpenQA.Selenium.BiDi.Communication.Json.Converters;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using System.Text.Json.Serialization;
8+
9+
namespace OpenQA.Selenium.BiDi.Extensions.Permissions;
10+
11+
internal class SetPermissionCommand(SetPermissionCommandParameters @params)
12+
: Command<SetPermissionCommandParameters, SetPermissionResult>(@params, "permissions.setPermission");
13+
14+
public class SetPermissionOptions : CommandOptions;
15+
public sealed record SetPermissionResult : EmptyResult;
16+
17+
internal record SetPermissionCommandParameters(PermissionDescriptor Descriptor, PermissionState State, string Origin, UserContext? UserContext) : Parameters;

dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs

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

20+
using OpenQA.Selenium.BiDi.Extensions.Permissions;
2021
using System;
2122
using System.Threading.Tasks;
2223

@@ -41,4 +42,9 @@ public static async Task<BiDi> AsBiDiAsync(this IWebDriver webDriver, BiDiOption
4142

4243
return bidi;
4344
}
45+
46+
public static PermissionsModule AsPermissionsAsync(this BiDi bidi)
47+
{
48+
return Module.Create<PermissionsModule>(bidi, bidi.DefaultBiDiOptions());
49+
}
4450
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using NUnit.Framework;
2+
using OpenQA.Selenium.BiDi.BrowsingContext;
3+
using OpenQA.Selenium.BiDi.Script;
4+
using OpenQA.Selenium.Environment;
5+
using System.Threading.Tasks;
6+
7+
namespace OpenQA.Selenium.BiDi.Extensions.Permissions;
8+
9+
internal class PermissionsTests : BiDiTestFixture
10+
{
11+
[Test]
12+
public async Task SettingPermissionsTest()
13+
{
14+
var userContext = await bidi.Browser.CreateUserContextAsync();
15+
var window = (await bidi.BrowsingContext.CreateAsync(ContextType.Window, new()
16+
{
17+
ReferenceContext = context,
18+
UserContext = userContext.UserContext,
19+
Background = true
20+
})).Context;
21+
22+
var newPage = EnvironmentManager.Instance.UrlBuilder.CreateInlinePage(new InlinePage()
23+
.WithBody("<div>new page</div>"));
24+
25+
await window.NavigateAsync(newPage);
26+
27+
var before = await window.Script.CallFunctionAsync("""
28+
async () => (await navigator.permissions.query({ name: "geolocation" })).state
29+
""", awaitPromise: true, new() { UserActivation = true, });
30+
31+
Assert.That(before.AsSuccessResult(), Is.EqualTo(new StringRemoteValue("prompt")));
32+
33+
var permissions = bidi.AsPermissionsAsync();
34+
await permissions.SetPermissionAsync("geolocation", PermissionState.Denied, newPage, userContext.UserContext);
35+
36+
var after = await window.Script.CallFunctionAsync("""
37+
async () => (await navigator.permissions.query({ name: "geolocation" })).state
38+
""", awaitPromise: true, new() { UserActivation = true });
39+
40+
Assert.That(after.AsSuccessResult(), Is.EqualTo(new StringRemoteValue("denied")));
41+
}
42+
}

0 commit comments

Comments
 (0)