Skip to content

Commit

Permalink
[dotnet] implement getting and setting permissions on Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Sep 29, 2021
1 parent 45a06f4 commit 81679ca
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions dotnet/src/webdriver/Safari/SafariDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// </copyright>

using System;
using System.Collections.Generic;
using OpenQA.Selenium.Remote;

namespace OpenQA.Selenium.Safari
Expand Down Expand Up @@ -61,6 +62,9 @@ namespace OpenQA.Selenium.Safari
/// </example>
public class SafariDriver : WebDriver
{
private const string GetPermissionsCommand = "getPermissions";
private const string SetPermissionsCommand = "setPermissions";

/// <summary>
/// Initializes a new instance of the <see cref="SafariDriver"/> class.
/// </summary>
Expand Down Expand Up @@ -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");
}

/// <summary>
/// Set permission of an item on the browser. The only supported permission at this time is "getUserMedia".
/// </summary>
/// <param name="permissionName">The name of the item to set permission on.</param>
/// <param name="permissionValue">Whether the permission has been granted.</param>
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<string, object> permissions = new Dictionary<string, object>();
permissions[permissionName] = permissionValue;
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters["permissions"] = permissions;
this.Execute(SetPermissionsCommand, parameters);
}

/// <summary>
/// Returns Each available permission item and whether it is allowed or not.
/// </summary>
/// <returns>whether the item is allowed or not.</returns>
public Object GetPermissions()
{
Response response = this.Execute(GetPermissionsCommand, null);
return response.Value;
}

/// <summary>
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 81679ca

Please sign in to comment.