Skip to content

Commit

Permalink
[dotnet] implement getting the context of Firefox commands
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Sep 29, 2021
1 parent 90e8e61 commit 45a06f4
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion dotnet/src/webdriver/Firefox/FirefoxDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using OpenQA.Selenium.DevTools;
using OpenQA.Selenium.Remote;
Expand Down Expand Up @@ -71,6 +72,7 @@ public class FirefoxDriver : WebDriver, IDevTools
private const int FirefoxDevToolsProtocolVersion = 85;
private const string FirefoxDevToolsCapabilityName = "moz:debuggerAddress";
private const string SetContextCommand = "setContext";
private const string GetContextCommand = "getContext";
private const string InstallAddOnCommand = "installAddOn";
private const string UninstallAddOnCommand = "uninstallAddOn";
private const string GetFullPageScreenshotCommand = "fullPageScreenshot";
Expand Down Expand Up @@ -157,6 +159,7 @@ public FirefoxDriver(FirefoxDriverService service, FirefoxOptions options, TimeS
{
// Add the custom commands unique to Firefox
this.AddCustomFirefoxCommand(SetContextCommand, HttpCommandInfo.PostCommand, "/session/{sessionId}/moz/context");
this.AddCustomFirefoxCommand(GetContextCommand, HttpCommandInfo.GetCommand, "/session/{sessionId}/moz/context");
this.AddCustomFirefoxCommand(InstallAddOnCommand, HttpCommandInfo.PostCommand, "/session/{sessionId}/moz/addon/install");
this.AddCustomFirefoxCommand(UninstallAddOnCommand, HttpCommandInfo.PostCommand, "/session/{sessionId}/moz/addon/uninstall");
this.AddCustomFirefoxCommand(GetFullPageScreenshotCommand, HttpCommandInfo.GetCommand, "/session/{sessionId}/moz/screenshot/full");
Expand All @@ -178,6 +181,25 @@ public override IFileDetector FileDetector
set { }
}

/// <summary>
/// Sets the command context used when issuing commands to geckodriver.
/// </summary>
/// <exception cref="WebDriverException">If response is not recognized</exception>
/// <returns>The context of commands.</returns>
public FirefoxCommandContext GetContext()
{
FirefoxCommandContext output;
string response = this.Execute(GetContextCommand, null).Value.ToString();

bool success = Enum.TryParse<FirefoxCommandContext>(response, true, out output);
if (!success)
{
throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Do not recognize response: {0}; expected Context or Chrome"));
}

return output;
}

/// <summary>
/// Sets the command context used when issuing commands to geckodriver.
/// </summary>
Expand All @@ -187,7 +209,7 @@ public void SetContext(FirefoxCommandContext context)
string contextValue = context.ToString().ToLowerInvariant();
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters["context"] = contextValue;
Response response = this.Execute(SetContextCommand, parameters);
this.Execute(SetContextCommand, parameters);
}

/// <summary>
Expand Down

0 comments on commit 45a06f4

Please sign in to comment.