From 81679ca6bdc898381228ad243fe65c1f5a077fc7 Mon Sep 17 00:00:00 2001 From: titusfortner Date: Tue, 28 Sep 2021 17:27:49 -0500 Subject: [PATCH] [dotnet] implement getting and setting permissions on Safari --- dotnet/src/webdriver/Safari/SafariDriver.cs | 41 +++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/dotnet/src/webdriver/Safari/SafariDriver.cs b/dotnet/src/webdriver/Safari/SafariDriver.cs index 725214637e183..a86bcb532695c 100644 --- a/dotnet/src/webdriver/Safari/SafariDriver.cs +++ b/dotnet/src/webdriver/Safari/SafariDriver.cs @@ -17,6 +17,7 @@ // using System; +using System.Collections.Generic; using OpenQA.Selenium.Remote; namespace OpenQA.Selenium.Safari @@ -61,6 +62,9 @@ namespace OpenQA.Selenium.Safari /// public class SafariDriver : WebDriver { + private const string GetPermissionsCommand = "getPermissions"; + private const string SetPermissionsCommand = "setPermissions"; + /// /// Initializes a new instance of the class. /// @@ -140,6 +144,37 @@ public SafariDriver(SafariDriverService service, SafariOptions options) public SafariDriver(SafariDriverService service, SafariOptions options, TimeSpan commandTimeout) : base(new DriverServiceCommandExecutor(service, commandTimeout), ConvertOptionsToCapabilities(options)) { + this.AddCustomSafariCommand(GetPermissionsCommand, HttpCommandInfo.GetCommand, "/session/{sessionId}/apple/permissions"); + this.AddCustomSafariCommand(SetPermissionsCommand, HttpCommandInfo.PostCommand, "/session/{sessionId}/apple/permissions"); + } + + /// + /// Set permission of an item on the browser. The only supported permission at this time is "getUserMedia". + /// + /// The name of the item to set permission on. + /// Whether the permission has been granted. + public void SetPermission(string permissionName, bool permissionValue) + { + if (string.IsNullOrEmpty(permissionName)) + { + throw new ArgumentNullException("permissionName", "permission must not be null or the empty string"); + } + + Dictionary permissions = new Dictionary(); + permissions[permissionName] = permissionValue; + Dictionary parameters = new Dictionary(); + parameters["permissions"] = permissions; + this.Execute(SetPermissionsCommand, parameters); + } + + /// + /// Returns Each available permission item and whether it is allowed or not. + /// + /// whether the item is allowed or not. + public Object GetPermissions() + { + Response response = this.Execute(GetPermissionsCommand, null); + return response.Value; } /// @@ -167,5 +202,11 @@ private static ICapabilities ConvertOptionsToCapabilities(SafariOptions options) return options.ToCapabilities(); } + + private void AddCustomSafariCommand(string commandName, string method, string resourcePath) + { + HttpCommandInfo commandInfoToAdd = new HttpCommandInfo(method, resourcePath); + this.CommandExecutor.TryAddCommand(commandName, commandInfoToAdd); + } } }