Skip to content

Commit 7ff4763

Browse files
committed
Adding ability to set additional ChromeOptions in the .NET bindings
This allows the ability to set user profile preferences and local state preferences using the ChromeOptions class. The new AddUserProfilePreference and AddLocalStatePreference methods correspond to the "prefs" and "localState" capabilities documented in the chromedriver documentation. Note that the values passed in to these methods must be JSON-serializable, and no validation is done on these values at present. We now also allow setting the "detach" capability, which allows the user to specify that Chrome remain running after chromedriver.exe has quit. This commit also removes legacy code for the obsolete chrome.binary and chrome.switches capabilities, and changes the command line arguments to chromedriver.exe to include two hyphens, instead of one (e.g., "--silent" instead of "-silent").
1 parent d7b7e7f commit 7ff4763

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

dotnet/src/webdriver/Chrome/ChromeDriverService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ protected override string CommandLineArguments
6363
StringBuilder argsBuilder = new StringBuilder(base.CommandLineArguments);
6464
if (this.SuppressInitialDiagnosticInformation)
6565
{
66-
argsBuilder.Append(" -silent");
66+
argsBuilder.Append(" --silent");
6767
}
6868

6969
if (!string.IsNullOrEmpty(this.logPath))
7070
{
71-
argsBuilder.Append(string.Format(CultureInfo.InvariantCulture, " -log-path={0}", this.logPath));
71+
argsBuilder.Append(string.Format(CultureInfo.InvariantCulture, " --log-path={0}", this.logPath));
7272
}
7373

7474
return argsBuilder.ToString();

dotnet/src/webdriver/Chrome/ChromeOptions.cs

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@ public class ChromeOptions
6262
/// </summary>
6363
public static readonly string Capability = "chromeOptions";
6464

65+
private bool leaveBrowserRunning;
6566
private string binaryLocation;
6667
private List<string> arguments = new List<string>();
6768
private List<string> extensionFiles = new List<string>();
6869
private List<string> encodedExtensions = new List<string>();
6970
private Dictionary<string, object> additionalCapabilities = new Dictionary<string, object>();
71+
private Dictionary<string, object> userProfilePreferences;
72+
private Dictionary<string, object> localStatePreferences;
7073
private Proxy proxy;
7174

7275
/// <summary>
@@ -79,6 +82,17 @@ public string BinaryLocation
7982
set { this.binaryLocation = value; }
8083
}
8184

85+
/// <summary>
86+
/// Gets or sets a value indicating whether Chrome should be left running after the
87+
/// ChromeDriver instance is exited. Defaults to <see langword="false"/>.
88+
/// </summary>
89+
[JsonProperty("detach")]
90+
public bool LeaveBrowserRunning
91+
{
92+
get { return leaveBrowserRunning; }
93+
set { leaveBrowserRunning = value; }
94+
}
95+
8296
/// <summary>
8397
/// Gets or sets the proxy to use with this instance of Chrome.
8498
/// </summary>
@@ -118,6 +132,18 @@ public ReadOnlyCollection<string> Extensions
118132
}
119133
}
120134

135+
[JsonProperty("prefs", NullValueHandling = NullValueHandling.Ignore)]
136+
private Dictionary<string, object> UserProfilePreferences
137+
{
138+
get { return userProfilePreferences; }
139+
}
140+
141+
[JsonProperty("localState", NullValueHandling = NullValueHandling.Ignore)]
142+
private Dictionary<string, object> LocalStatePreferences
143+
{
144+
get { return localStatePreferences; }
145+
}
146+
121147
/// <summary>
122148
/// Adds a single argument to the list of arguments to be appended to the Chrome.exe command line.
123149
/// </summary>
@@ -258,6 +284,38 @@ public void AddEncodedExtensions(IEnumerable<string> extensions)
258284
}
259285
}
260286

287+
/// <summary>
288+
/// Adds a preference for the user-specific profile or "user data directory."
289+
/// If the specified preference already exists, it will be overwritten.
290+
/// </summary>
291+
/// <param name="preferenceName">The name of the preference to set.</param>
292+
/// <param name="preferenceValue">The value of the preference to set.</param>
293+
public void AddUserProfilePreference(string preferenceName, object preferenceValue)
294+
{
295+
if (this.userProfilePreferences == null)
296+
{
297+
this.userProfilePreferences = new Dictionary<string, object>();
298+
}
299+
300+
this.userProfilePreferences[preferenceName] = preferenceValue;
301+
}
302+
303+
/// <summary>
304+
/// Adds a preference for the local state file in the user's data directory for Chrome.
305+
/// If the specified preference already exists, it will be overwritten.
306+
/// </summary>
307+
/// <param name="preferenceName">The name of the preference to set.</param>
308+
/// <param name="preferenceValue">The value of the preference to set.</param>
309+
public void AddLocalStatePreference(string preferenceName, object preferenceValue)
310+
{
311+
if (this.localStatePreferences == null)
312+
{
313+
this.localStatePreferences = new Dictionary<string, object>();
314+
}
315+
316+
this.localStatePreferences[preferenceName] = preferenceValue;
317+
}
318+
261319
/// <summary>
262320
/// Provides a means to add additional capabilities not yet added as type safe options
263321
/// for the Chrome driver.
@@ -297,16 +355,6 @@ public ICapabilities ToCapabilities()
297355
DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
298356
capabilities.SetCapability(ChromeOptions.Capability, this);
299357

300-
// chromeOptions is only recognized by chromedriver 17.0.963.0 or newer.
301-
// Provide backwards compatibility for capabilities supported by older
302-
// versions of chromedriver.
303-
// TODO: remove this once the deprecated capabilities are no longer supported.
304-
capabilities.SetCapability("chrome.switches", this.arguments);
305-
if (!string.IsNullOrEmpty(this.binaryLocation))
306-
{
307-
capabilities.SetCapability("chrome.binary", this.binaryLocation);
308-
}
309-
310358
if (this.proxy != null)
311359
{
312360
capabilities.SetCapability(CapabilityType.Proxy, this.proxy);

0 commit comments

Comments
 (0)