Skip to content

Commit 9445843

Browse files
authored
[Feature] DevTools command logging options (#135) +semver: feature
Add optional parameter for DevTools Command/Result logging options to SendCommand and ExecuteCdpCommand methods
1 parent 888ec15 commit 9445843

File tree

3 files changed

+95
-13
lines changed

3 files changed

+95
-13
lines changed

src/main/java/aquality/selenium/browser/devtools/DevToolsHandling.java

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import aquality.selenium.browser.AqualityServices;
44
import aquality.selenium.core.localization.ILocalizedLogger;
5+
import aquality.selenium.logging.DevToolsCommandLoggingOptions;
6+
import aquality.selenium.logging.LoggingParameters;
57
import org.openqa.selenium.chromium.ChromiumDriver;
68
import org.openqa.selenium.devtools.Command;
79
import org.openqa.selenium.devtools.DevTools;
@@ -16,6 +18,8 @@
1618
import java.util.function.Consumer;
1719
import java.util.stream.Collectors;
1820

21+
import static aquality.selenium.logging.LocalizedLoggerUtility.logByLevel;
22+
1923
/**
2024
* Wrapper for Selenium {@link DevTools} functionality.
2125
*/
@@ -53,21 +57,30 @@ private DevTools getDevTools(String handleToLog) {
5357
return session;
5458
}
5559

56-
private void logCommand(String commandName, Map<String, Object> commandParameters) {
60+
private void logCommand(String commandName, Map<String, Object> commandParameters,
61+
DevToolsCommandLoggingOptions loggingOptions) {
62+
LoggingParameters logging = (loggingOptions == null ? new DevToolsCommandLoggingOptions() : loggingOptions)
63+
.getCommand();
64+
if (!logging.isEnabled())
65+
{
66+
return;
67+
}
5768
if (!commandParameters.isEmpty()) {
58-
logger.info("loc.browser.devtools.command.execute.withparams", commandName, commandParameters);
69+
logByLevel(logging.getLogLevel(), "loc.browser.devtools.command.execute.withparams", commandName, commandParameters);
5970
}
6071
else {
61-
logger.info("loc.browser.devtools.command.execute", commandName);
72+
logByLevel(logging.getLogLevel(), "loc.browser.devtools.command.execute", commandName);
6273
}
6374
}
6475

65-
private void logCommandResult(Object result) {
66-
if (result != null) {
76+
private void logCommandResult(Object result, DevToolsCommandLoggingOptions loggingOptions) {
77+
LoggingParameters logging = (loggingOptions == null ? new DevToolsCommandLoggingOptions() : loggingOptions)
78+
.getCommand();
79+
if (result != null && logging.isEnabled()) {
6780
if (result instanceof Map && ((Map<?, ?>) result).isEmpty()) {
6881
return;
6982
}
70-
logger.info("loc.browser.devtools.command.execute.result", result);
83+
logByLevel(logging.getLogLevel(), "loc.browser.devtools.command.execute.result", result);
7184
}
7285
}
7386

@@ -123,11 +136,24 @@ public void closeDevToolsSession() {
123136
* @return An object representing the result of the command, if applicable.
124137
*/
125138
public Map<String, Object> executeCdpCommand(String commandName, Map<String, Object> commandParameters) {
139+
return executeCdpCommand(commandName, commandParameters, null);
140+
}
141+
142+
/**
143+
* Executes a custom Chromium Dev Tools Protocol Command.
144+
* Note: works only if current driver is instance of {@link ChromiumDriver}.
145+
* @param commandName Name of the command to execute.
146+
* @param commandParameters Parameters of the command to execute.
147+
* @param loggingOptions Logging preferences.
148+
* @return An object representing the result of the command, if applicable.
149+
*/
150+
public Map<String, Object> executeCdpCommand(String commandName, Map<String, Object> commandParameters,
151+
DevToolsCommandLoggingOptions loggingOptions) {
126152
if (devToolsProvider instanceof ChromiumDriver) {
127-
logCommand(commandName, commandParameters);
153+
logCommand(commandName, commandParameters, loggingOptions);
128154
ChromiumDriver driver = (ChromiumDriver) devToolsProvider;
129155
Map<String, Object> result = driver.executeCdpCommand(commandName, commandParameters);
130-
logCommandResult(result);
156+
logCommandResult(result, loggingOptions);
131157
return result;
132158
}
133159
else {
@@ -142,9 +168,20 @@ public Map<String, Object> executeCdpCommand(String commandName, Map<String, Obj
142168
* @return the result of the command, if applicable
143169
*/
144170
public <X> X sendCommand(Command<X> command) {
145-
logCommand(command.getMethod(), command.getParams());
171+
return sendCommand(command, null);
172+
}
173+
174+
/**
175+
* Sends the specified command and returns the associated command response.
176+
* @param command An instance of the {@link Command} to send.
177+
* @param <X> The type of the command's result. For most commands it's {@link Void}
178+
* @param loggingOptions Logging preferences.
179+
* @return the result of the command, if applicable
180+
*/
181+
public <X> X sendCommand(Command<X> command, DevToolsCommandLoggingOptions loggingOptions) {
182+
logCommand(command.getMethod(), command.getParams(), loggingOptions);
146183
X result = getDevToolsSession().send(command);
147-
logCommandResult(result);
184+
logCommandResult(result, loggingOptions);
148185
return result;
149186
}
150187

@@ -229,10 +266,10 @@ public void enablePerformanceMonitoring() {
229266
*/
230267
public Map<String, Number> getPerformanceMetrics() {
231268
Command<List<Metric>> command = Performance.getMetrics();
232-
logCommand(command.getMethod(), command.getParams());
269+
logCommand(command.getMethod(), command.getParams(), null);
233270
List<Metric> metrics = getDevToolsSession().send(command);
234271
Map<String, Number> result = metrics.stream().collect(Collectors.toMap(Metric::getName, Metric::getValue));
235-
logCommandResult(result.isEmpty() ? "empty" : result);
272+
logCommandResult(result.isEmpty() ? "empty" : result, null);
236273
return result;
237274
}
238275
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package aquality.selenium.logging;
2+
3+
/**
4+
* DevTools Command/Result logging options.
5+
*/
6+
public class DevToolsCommandLoggingOptions {
7+
private LoggingParameters command = new LoggingParameters(true, LogLevel.INFO);
8+
private LoggingParameters result = new LoggingParameters(true, LogLevel.INFO);
9+
10+
/**
11+
* Gets logging parameters for command info: name and parameters (if any).
12+
* @return command info logging parameters.
13+
*/
14+
public LoggingParameters getCommand() {
15+
return command;
16+
}
17+
18+
/**
19+
* Sets logging parameters for command info: name and parameters (if any).
20+
*/
21+
public void setCommand(LoggingParameters command) {
22+
this.command = command;
23+
}
24+
25+
/**
26+
* Gets logging parameters for command result (when it's present).
27+
* @return logging parameters of command result.
28+
*/
29+
public LoggingParameters getResult() {
30+
return result;
31+
}
32+
33+
/**
34+
* Sets logging parameters for command result (when it's present).
35+
*/
36+
public void setResult(LoggingParameters result) {
37+
this.result = result;
38+
}
39+
}

src/test/java/tests/usecases/devtools/OverrideUserAgentTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import aquality.selenium.browser.AqualityServices;
44
import aquality.selenium.browser.devtools.EmulationHandling;
5+
import aquality.selenium.logging.DevToolsCommandLoggingOptions;
6+
import aquality.selenium.logging.LogLevel;
7+
import aquality.selenium.logging.LoggingParameters;
58
import manytools.BrowserLanguageForm;
69
import manytools.UserAgentForm;
710
import org.openqa.selenium.devtools.idealized.Network;
@@ -60,9 +63,12 @@ public void overrideUserAgentByParametersMapTest() {
6063

6164
@Test
6265
public void overrideUserAgentByVersionSpecificCommandTest() {
66+
DevToolsCommandLoggingOptions loggingOptions = new DevToolsCommandLoggingOptions();
67+
loggingOptions.setCommand(new LoggingParameters(false, LogLevel.DEBUG));
68+
loggingOptions.setResult(new LoggingParameters(false, LogLevel.DEBUG));
6369
AqualityServices.getBrowser().devTools().sendCommand(Emulation.setUserAgentOverride(CUSTOM_USER_AGENT,
6470
Optional.of(CUSTOM_ACCEPT_LANGUAGE), Optional.empty(),
65-
Optional.empty()));
71+
Optional.empty()), loggingOptions);
6672
Assert.assertEquals(new UserAgentForm().open().getValue(), CUSTOM_USER_AGENT, "User agent should match to value set");
6773
}
6874

0 commit comments

Comments
 (0)