22
33import aquality .selenium .browser .AqualityServices ;
44import aquality .selenium .core .localization .ILocalizedLogger ;
5+ import aquality .selenium .logging .DevToolsCommandLoggingOptions ;
6+ import aquality .selenium .logging .LoggingParameters ;
57import org .openqa .selenium .chromium .ChromiumDriver ;
68import org .openqa .selenium .devtools .Command ;
79import org .openqa .selenium .devtools .DevTools ;
1618import java .util .function .Consumer ;
1719import 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}
0 commit comments