diff --git a/src/main/java/aquality/selenium/browser/devtools/DevToolsHandling.java b/src/main/java/aquality/selenium/browser/devtools/DevToolsHandling.java index 6187ec5..b9de2d8 100644 --- a/src/main/java/aquality/selenium/browser/devtools/DevToolsHandling.java +++ b/src/main/java/aquality/selenium/browser/devtools/DevToolsHandling.java @@ -2,6 +2,8 @@ import aquality.selenium.browser.AqualityServices; import aquality.selenium.core.localization.ILocalizedLogger; +import aquality.selenium.logging.DevToolsCommandLoggingOptions; +import aquality.selenium.logging.LoggingParameters; import org.openqa.selenium.chromium.ChromiumDriver; import org.openqa.selenium.devtools.Command; import org.openqa.selenium.devtools.DevTools; @@ -16,6 +18,8 @@ import java.util.function.Consumer; import java.util.stream.Collectors; +import static aquality.selenium.logging.LocalizedLoggerUtility.logByLevel; + /** * Wrapper for Selenium {@link DevTools} functionality. */ @@ -53,21 +57,30 @@ private DevTools getDevTools(String handleToLog) { return session; } - private void logCommand(String commandName, Map commandParameters) { + private void logCommand(String commandName, Map commandParameters, + DevToolsCommandLoggingOptions loggingOptions) { + LoggingParameters logging = (loggingOptions == null ? new DevToolsCommandLoggingOptions() : loggingOptions) + .getCommand(); + if (!logging.isEnabled()) + { + return; + } if (!commandParameters.isEmpty()) { - logger.info("loc.browser.devtools.command.execute.withparams", commandName, commandParameters); + logByLevel(logging.getLogLevel(), "loc.browser.devtools.command.execute.withparams", commandName, commandParameters); } else { - logger.info("loc.browser.devtools.command.execute", commandName); + logByLevel(logging.getLogLevel(), "loc.browser.devtools.command.execute", commandName); } } - private void logCommandResult(Object result) { - if (result != null) { + private void logCommandResult(Object result, DevToolsCommandLoggingOptions loggingOptions) { + LoggingParameters logging = (loggingOptions == null ? new DevToolsCommandLoggingOptions() : loggingOptions) + .getCommand(); + if (result != null && logging.isEnabled()) { if (result instanceof Map && ((Map) result).isEmpty()) { return; } - logger.info("loc.browser.devtools.command.execute.result", result); + logByLevel(logging.getLogLevel(), "loc.browser.devtools.command.execute.result", result); } } @@ -123,11 +136,24 @@ public void closeDevToolsSession() { * @return An object representing the result of the command, if applicable. */ public Map executeCdpCommand(String commandName, Map commandParameters) { + return executeCdpCommand(commandName, commandParameters, null); + } + + /** + * Executes a custom Chromium Dev Tools Protocol Command. + * Note: works only if current driver is instance of {@link ChromiumDriver}. + * @param commandName Name of the command to execute. + * @param commandParameters Parameters of the command to execute. + * @param loggingOptions Logging preferences. + * @return An object representing the result of the command, if applicable. + */ + public Map executeCdpCommand(String commandName, Map commandParameters, + DevToolsCommandLoggingOptions loggingOptions) { if (devToolsProvider instanceof ChromiumDriver) { - logCommand(commandName, commandParameters); + logCommand(commandName, commandParameters, loggingOptions); ChromiumDriver driver = (ChromiumDriver) devToolsProvider; Map result = driver.executeCdpCommand(commandName, commandParameters); - logCommandResult(result); + logCommandResult(result, loggingOptions); return result; } else { @@ -142,9 +168,20 @@ public Map executeCdpCommand(String commandName, Map X sendCommand(Command command) { - logCommand(command.getMethod(), command.getParams()); + return sendCommand(command, null); + } + + /** + * Sends the specified command and returns the associated command response. + * @param command An instance of the {@link Command} to send. + * @param The type of the command's result. For most commands it's {@link Void} + * @param loggingOptions Logging preferences. + * @return the result of the command, if applicable + */ + public X sendCommand(Command command, DevToolsCommandLoggingOptions loggingOptions) { + logCommand(command.getMethod(), command.getParams(), loggingOptions); X result = getDevToolsSession().send(command); - logCommandResult(result); + logCommandResult(result, loggingOptions); return result; } @@ -229,10 +266,10 @@ public void enablePerformanceMonitoring() { */ public Map getPerformanceMetrics() { Command> command = Performance.getMetrics(); - logCommand(command.getMethod(), command.getParams()); + logCommand(command.getMethod(), command.getParams(), null); List metrics = getDevToolsSession().send(command); Map result = metrics.stream().collect(Collectors.toMap(Metric::getName, Metric::getValue)); - logCommandResult(result.isEmpty() ? "empty" : result); + logCommandResult(result.isEmpty() ? "empty" : result, null); return result; } } diff --git a/src/main/java/aquality/selenium/logging/DevToolsCommandLoggingOptions.java b/src/main/java/aquality/selenium/logging/DevToolsCommandLoggingOptions.java new file mode 100644 index 0000000..765ba0e --- /dev/null +++ b/src/main/java/aquality/selenium/logging/DevToolsCommandLoggingOptions.java @@ -0,0 +1,39 @@ +package aquality.selenium.logging; + +/** + * DevTools Command/Result logging options. + */ +public class DevToolsCommandLoggingOptions { + private LoggingParameters command = new LoggingParameters(true, LogLevel.INFO); + private LoggingParameters result = new LoggingParameters(true, LogLevel.INFO); + + /** + * Gets logging parameters for command info: name and parameters (if any). + * @return command info logging parameters. + */ + public LoggingParameters getCommand() { + return command; + } + + /** + * Sets logging parameters for command info: name and parameters (if any). + */ + public void setCommand(LoggingParameters command) { + this.command = command; + } + + /** + * Gets logging parameters for command result (when it's present). + * @return logging parameters of command result. + */ + public LoggingParameters getResult() { + return result; + } + + /** + * Sets logging parameters for command result (when it's present). + */ + public void setResult(LoggingParameters result) { + this.result = result; + } +} diff --git a/src/test/java/tests/usecases/devtools/OverrideUserAgentTest.java b/src/test/java/tests/usecases/devtools/OverrideUserAgentTest.java index d8d0c06..19be87a 100644 --- a/src/test/java/tests/usecases/devtools/OverrideUserAgentTest.java +++ b/src/test/java/tests/usecases/devtools/OverrideUserAgentTest.java @@ -2,6 +2,9 @@ import aquality.selenium.browser.AqualityServices; import aquality.selenium.browser.devtools.EmulationHandling; +import aquality.selenium.logging.DevToolsCommandLoggingOptions; +import aquality.selenium.logging.LogLevel; +import aquality.selenium.logging.LoggingParameters; import manytools.BrowserLanguageForm; import manytools.UserAgentForm; import org.openqa.selenium.devtools.idealized.Network; @@ -60,9 +63,12 @@ public void overrideUserAgentByParametersMapTest() { @Test public void overrideUserAgentByVersionSpecificCommandTest() { + DevToolsCommandLoggingOptions loggingOptions = new DevToolsCommandLoggingOptions(); + loggingOptions.setCommand(new LoggingParameters(false, LogLevel.DEBUG)); + loggingOptions.setResult(new LoggingParameters(false, LogLevel.DEBUG)); AqualityServices.getBrowser().devTools().sendCommand(Emulation.setUserAgentOverride(CUSTOM_USER_AGENT, Optional.of(CUSTOM_ACCEPT_LANGUAGE), Optional.empty(), - Optional.empty())); + Optional.empty()), loggingOptions); Assert.assertEquals(new UserAgentForm().open().getValue(), CUSTOM_USER_AGENT, "User agent should match to value set"); }