Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand Down Expand Up @@ -53,21 +57,30 @@ private DevTools getDevTools(String handleToLog) {
return session;
}

private void logCommand(String commandName, Map<String, Object> commandParameters) {
private void logCommand(String commandName, Map<String, Object> 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);
}
}

Expand Down Expand Up @@ -123,11 +136,24 @@ public void closeDevToolsSession() {
* @return An object representing the result of the command, if applicable.
*/
public Map<String, Object> executeCdpCommand(String commandName, Map<String, Object> 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<String, Object> executeCdpCommand(String commandName, Map<String, Object> commandParameters,
DevToolsCommandLoggingOptions loggingOptions) {
if (devToolsProvider instanceof ChromiumDriver) {
logCommand(commandName, commandParameters);
logCommand(commandName, commandParameters, loggingOptions);
ChromiumDriver driver = (ChromiumDriver) devToolsProvider;
Map<String, Object> result = driver.executeCdpCommand(commandName, commandParameters);
logCommandResult(result);
logCommandResult(result, loggingOptions);
return result;
}
else {
Expand All @@ -142,9 +168,20 @@ public Map<String, Object> executeCdpCommand(String commandName, Map<String, Obj
* @return the result of the command, if applicable
*/
public <X> X sendCommand(Command<X> 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 <X> 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> X sendCommand(Command<X> command, DevToolsCommandLoggingOptions loggingOptions) {
logCommand(command.getMethod(), command.getParams(), loggingOptions);
X result = getDevToolsSession().send(command);
logCommandResult(result);
logCommandResult(result, loggingOptions);
return result;
}

Expand Down Expand Up @@ -229,10 +266,10 @@ public void enablePerformanceMonitoring() {
*/
public Map<String, Number> getPerformanceMetrics() {
Command<List<Metric>> command = Performance.getMetrics();
logCommand(command.getMethod(), command.getParams());
logCommand(command.getMethod(), command.getParams(), null);
List<Metric> metrics = getDevToolsSession().send(command);
Map<String, Number> result = metrics.stream().collect(Collectors.toMap(Metric::getName, Metric::getValue));
logCommandResult(result.isEmpty() ? "empty" : result);
logCommandResult(result.isEmpty() ? "empty" : result, null);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}

Expand Down