Skip to content

Commit 1c4b33a

Browse files
authored
[Feature] DevTools command logging options (#247) +semver: feature
* [Feature] DevTools command logging options Add optional parameter for DevTools Command/Result logging options to SendComand and ExecuteCdpCommand methods closes #246 * correct workaround in GoToUrl
1 parent e0f84d7 commit 1c4b33a

File tree

5 files changed

+72
-14
lines changed

5 files changed

+72
-14
lines changed

Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.xml

Lines changed: 19 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Aquality.Selenium/src/Aquality.Selenium/Browsers/BrowserNavigation.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public void GoToUrl(string url)
5252
catch (WebDriverException e) when (driver.Url == url)
5353
{
5454
Logger.Fatal($"Navigation error occurred: [{e.Message}], but successfully navigated to URL [{url}]", e);
55+
// ignore only unknown errors
56+
if (e.GetType() != typeof(WebDriverException))
57+
{
58+
throw;
59+
}
5560
}
5661
}
5762

Aquality.Selenium/src/Aquality.Selenium/Browsers/DevToolsHandling.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Aquality.Selenium.Core.Localization;
2+
using Aquality.Selenium.Logging;
23
using Newtonsoft.Json.Linq;
34
using OpenQA.Selenium;
45
using OpenQA.Selenium.Chromium;
@@ -92,15 +93,16 @@ public DevToolsSession GetDevToolsSession(int protocolVersion)
9293
/// </summary>
9394
/// <param name="commandName">Name of the command to execute.</param>
9495
/// <param name="commandParameters">Parameters of the command to execute.</param>
96+
/// <param name="loggingOptions">Logging preferences.</param>
9597
/// <returns>An object representing the result of the command, if applicable.</returns>
96-
public object ExecuteCdpCommand(string commandName, Dictionary<string, object> commandParameters)
98+
public object ExecuteCdpCommand(string commandName, Dictionary<string, object> commandParameters, DevToolsCommandLoggingOptions loggingOptions = null)
9799
{
98100
if (devToolsProvider is ChromiumDriver driver)
99101
{
100-
LogCommand(commandName, JToken.FromObject(commandParameters));
102+
LogCommand(commandName, JToken.FromObject(commandParameters), loggingOptions);
101103
var result = driver.ExecuteCdpCommand(commandName, commandParameters);
102104
var formattedResult = JToken.FromObject(result);
103-
LogCommandResult(formattedResult);
105+
LogCommandResult(formattedResult, loggingOptions);
104106
return result;
105107
}
106108
else
@@ -117,15 +119,17 @@ public object ExecuteCdpCommand(string commandName, Dictionary<string, object> c
117119
/// <param name="cancellationToken">A CancellationToken object to allow for cancellation of the command.</param>
118120
/// <param name="millisecondsTimeout">The execution timeout of the command in milliseconds.</param>
119121
/// <param name="throwExceptionIfResponseNotReceived"><see langword="true"/> to throw an exception if a response is not received; otherwise, <see langword="false"/>.</param>
122+
/// <param name="loggingOptions">Logging preferences.</param>
120123
/// <returns>A JToken based on a command created with the specified command name and parameters.</returns>
121124
public async Task<JToken> SendCommand(string commandName, JToken commandParameters = null,
122-
CancellationToken cancellationToken = default, int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
125+
CancellationToken cancellationToken = default, int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true,
126+
DevToolsCommandLoggingOptions loggingOptions = null)
123127
{
124128
var parameters = commandParameters ?? new JObject();
125-
LogCommand(commandName, parameters);
129+
LogCommand(commandName, parameters, loggingOptions);
126130
var result = await devToolsProvider.GetDevToolsSession()
127131
.SendCommand(commandName, parameters, cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived);
128-
LogCommandResult(result);
132+
LogCommandResult(result, loggingOptions);
129133
return result;
130134
}
131135

@@ -144,21 +148,33 @@ public async Task<JToken> SendCommand(ICommand commandWithParameters,
144148
cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived);
145149
}
146150

147-
private void LogCommand(string commandName, JToken commandParameters)
151+
private void LogCommand(string commandName, JToken commandParameters, DevToolsCommandLoggingOptions loggingOptions = null)
148152
{
153+
if (loggingOptions == null)
154+
{
155+
loggingOptions = new DevToolsCommandLoggingOptions();
156+
}
157+
if (!loggingOptions.Result.Enabled)
158+
{
159+
return;
160+
}
149161
if (commandParameters.Any())
150162
{
151-
Logger.Info("loc.browser.devtools.command.execute.withparams", commandName, commandParameters.ToString());
163+
Logger.LogByLevel(loggingOptions.Command.LogLevel, "loc.browser.devtools.command.execute.withparams", commandName, commandParameters.ToString());
152164
}
153165
else
154166
{
155-
Logger.Info("loc.browser.devtools.command.execute", commandName);
167+
Logger.LogByLevel(loggingOptions.Command.LogLevel, "loc.browser.devtools.command.execute", commandName);
156168
}
157169
}
158170

159-
private void LogCommandResult(JToken result)
171+
private void LogCommandResult(JToken result, DevToolsCommandLoggingOptions loggingOptions = null)
160172
{
161-
if (result.Any())
173+
if (loggingOptions == null)
174+
{
175+
loggingOptions = new DevToolsCommandLoggingOptions();
176+
}
177+
if (result.Any() && loggingOptions.Result.Enabled)
162178
{
163179
Logger.Info("loc.browser.devtools.command.execute.result", result.ToString());
164180
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Aquality.Selenium.Logging
2+
{
3+
/// <summary>
4+
/// DevTools Command/Result logging options.
5+
/// </summary>
6+
public class DevToolsCommandLoggingOptions
7+
{
8+
/// <summary>
9+
/// Controls logging of command info: name and parameters (if any).
10+
/// </summary>
11+
public LoggingParameters Command { get; set; } = new LoggingParameters { Enabled = true, LogLevel = LogLevel.Info };
12+
13+
/// <summary>
14+
/// Controls logging of command result (if present).
15+
/// </summary>
16+
public LoggingParameters Result { get; set; } = new LoggingParameters { Enabled = true, LogLevel = LogLevel.Info };
17+
}
18+
}

Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/DevToolsEmulationTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Aquality.Selenium.Browsers;
2+
using Aquality.Selenium.Logging;
23
using Aquality.Selenium.Tests.Integration.TestApp.ManyTools.Forms;
34
using Aquality.Selenium.Tests.Integration.TestApp.MyLocation;
45
using Aquality.Selenium.Tests.Integration.TestApp.TheInternet.Forms;
@@ -113,7 +114,8 @@ public void Should_BePossibleTo_SetAndClearGeoLocationOverride_ByExecutingCdpCom
113114
{ "latitude", latitude},
114115
{ "longitude", longitude},
115116
{ "accuracy", accuracy},
116-
}),
117+
},
118+
new DevToolsCommandLoggingOptions { Command = new LoggingParameters { Enabled = false } , Result = new LoggingParameters { Enabled = false } }),
117119
() => DevTools.ExecuteCdpCommand(new ClearGeolocationOverrideCommandSettings().CommandName, new Dictionary<string, object>()));
118120
}
119121

0 commit comments

Comments
 (0)