Skip to content

Commit b6fbe7e

Browse files
committed
[dotnet] Avoid performing JSON serialization in debugger display
1 parent f8d6c36 commit b6fbe7e

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

dotnet/src/webdriver/DriverOptions.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
using OpenQA.Selenium.Remote;
2222
using System;
2323
using System.Collections.Generic;
24+
using System.Diagnostics;
2425
using System.Globalization;
26+
using System.Text;
2527
using System.Text.Json;
2628

2729
namespace OpenQA.Selenium
@@ -111,6 +113,7 @@ public Dictionary<string, object> ToCapabilities()
111113
/// <summary>
112114
/// Base class for managing options specific to a browser driver.
113115
/// </summary>
116+
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
114117
public abstract class DriverOptions
115118
{
116119
private string browserName;
@@ -624,5 +627,40 @@ protected IWritableCapabilities GenerateDesiredCapabilities(bool isSpecification
624627

625628
return capabilities;
626629
}
630+
631+
private string GetDebuggerDisplay()
632+
{
633+
StringBuilder builder = new StringBuilder();
634+
635+
bool needComma = false;
636+
637+
if (browserName is not null)
638+
{
639+
builder.Append("Browser = ").Append(browserName);
640+
needComma = true;
641+
}
642+
643+
if (browserVersion is not null)
644+
{
645+
if (needComma)
646+
{
647+
builder.Append(',').Append(' ');
648+
}
649+
650+
builder.Append("Version = ").Append(browserVersion);
651+
}
652+
653+
if (platformName is not null)
654+
{
655+
if (needComma)
656+
{
657+
builder.Append(',').Append(' ');
658+
}
659+
660+
builder.Append("Platform = ").Append(platformName);
661+
}
662+
663+
return builder.ToString();
664+
}
627665
}
628666
}

dotnet/src/webdriver/Internal/ReturnedCapabilities.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System;
2121
using System.Collections.Generic;
2222
using System.Collections.ObjectModel;
23+
using System.Diagnostics;
2324
using System.Globalization;
2425
using System.Text.Json;
2526

@@ -29,6 +30,7 @@ namespace OpenQA.Selenium.Internal
2930
/// Class to Create the capabilities of the browser you require for <see cref="IWebDriver"/>.
3031
/// If you wish to use default values use the static methods
3132
/// </summary>
33+
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
3234
internal class ReturnedCapabilities : ICapabilities, IHasCapabilitiesDictionary
3335
{
3436
private readonly Dictionary<string, object> capabilities = new Dictionary<string, object>();
@@ -156,5 +158,15 @@ public override string ToString()
156158
{
157159
return JsonSerializer.Serialize(this.capabilities, new JsonSerializerOptions { WriteIndented = true });
158160
}
161+
162+
private string GetDebuggerDisplay()
163+
{
164+
string browser = this.BrowserName;
165+
if (!string.IsNullOrEmpty(browser))
166+
{
167+
return $"Browser = {browser}, Count = {this.capabilities.Count}";
168+
}
169+
return $"Count = {this.capabilities.Count}";
170+
}
159171
}
160172
}

dotnet/src/webdriver/Remote/RemoteSessionSettings.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System;
2222
using System.Collections;
2323
using System.Collections.Generic;
24+
using System.Diagnostics;
2425
using System.Globalization;
2526
using System.Text.Json;
2627

@@ -29,12 +30,13 @@ namespace OpenQA.Selenium
2930
/// <summary>
3031
/// Base class for managing options specific to a browser driver.
3132
/// </summary>
33+
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
3234
public class RemoteSessionSettings : ICapabilities
3335
{
3436
private const string FirstMatchCapabilityName = "firstMatch";
3537
private const string AlwaysMatchCapabilityName = "alwaysMatch";
3638

37-
private readonly List<string> reservedSettingNames = new List<string>() { FirstMatchCapabilityName, AlwaysMatchCapabilityName };
39+
private readonly HashSet<string> reservedSettingNames = new HashSet<string>() { FirstMatchCapabilityName, AlwaysMatchCapabilityName };
3840
private DriverOptions mustMatchDriverOptions;
3941
private List<DriverOptions> firstMatchOptions = new List<DriverOptions>();
4042
private Dictionary<string, object> remoteMetadataSettings = new Dictionary<string, object>();
@@ -350,5 +352,16 @@ private bool IsJsonSerializable(object arg)
350352

351353
return true;
352354
}
355+
356+
private string GetDebuggerDisplay()
357+
{
358+
string browser = this.mustMatchDriverOptions.BrowserName;
359+
360+
if (!string.IsNullOrEmpty(browser))
361+
{
362+
return $"Browser = {browser}, Extra driver options count = {this.firstMatchOptions.Count}";
363+
}
364+
return $"Extra driver options Count = {this.firstMatchOptions.Count}, ";
365+
}
353366
}
354367
}

0 commit comments

Comments
 (0)